Skip to content

Commit 62c7aef

Browse files
authored
Discord Bot - "Ignore Bot Messages" Flag in New Message Triggers (#17290)
* ignore bot messages * pnpm-lock.yaml
1 parent aa58639 commit 62c7aef

File tree

7 files changed

+87
-14
lines changed

7 files changed

+87
-14
lines changed

components/discord_bot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/discord_bot",
3-
"version": "0.6.1",
3+
"version": "0.6.2",
44
"description": "Pipedream Discord_bot Components",
55
"main": "discord_bot.app.mjs",
66
"keywords": [

components/discord_bot/sources/common.mjs renamed to components/discord_bot/sources/common/common.mjs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import common from "../common.mjs";
2-
import constants from "../common/constants.mjs";
1+
import common from "../../common.mjs";
2+
import constants from "../../common/constants.mjs";
33

44
export default {
55
...common,
@@ -17,5 +17,16 @@ export default {
1717
_setLastMemberID(memberID) {
1818
this.db.set(constants.LAST_MEMBER_ID, memberID);
1919
},
20+
_getBotId() {
21+
return this.db.get("botId");
22+
},
23+
_setBotId(botId) {
24+
this.db.set("botId", botId);
25+
},
26+
getBotProfile() {
27+
return this.discord._makeRequest({
28+
path: "/users/@me",
29+
});
30+
},
2031
},
2132
};

components/discord_bot/sources/new-forum-thread-message/new-forum-thread-message.mjs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
22
import maxBy from "lodash.maxby";
33
import constants from "../../common/constants.mjs";
4-
import common from "../common.mjs";
4+
import common from "../common/common.mjs";
55
import sampleEmit from "./test-event.mjs";
66

77
export default {
@@ -10,7 +10,7 @@ export default {
1010
name: "New Forum Thread Message",
1111
description: "Emit new event for each forum thread message posted. Note that your bot must have the `MESSAGE_CONTENT` privilege intent to see the message content. [See the documentation](https://discord.com/developers/docs/topics/gateway#message-content-intent).",
1212
type: "source",
13-
version: "0.0.4",
13+
version: "0.0.5",
1414
dedupe: "unique", // Dedupe events based on the Discord message ID
1515
props: {
1616
...common.props,
@@ -35,6 +35,20 @@ export default {
3535
label: "Forum Id",
3636
description: "Select the forum you want to watch.",
3737
},
38+
ignoreBotMessages: {
39+
type: "boolean",
40+
label: "Ignore Bot Messages",
41+
description: "Set to `true` to only emit messages NOT from the configured Discord bot",
42+
optional: true,
43+
},
44+
},
45+
hooks: {
46+
async deploy() {
47+
if (this.ignoreBotMessages) {
48+
const { id } = await this.getBotProfile();
49+
this._setBotId(id);
50+
}
51+
},
3852
},
3953
methods: {
4054
...common.methods,
@@ -47,6 +61,9 @@ export default {
4761
async run({ $ }) {
4862
// We store a cursor to the last message ID
4963
let lastMessageIDs = this._getLastMessageIDs();
64+
const botId = this.ignoreBotMessages
65+
? this._getBotId()
66+
: null;
5067

5168
const { threads } = await this.discord.listThreads({
5269
$,
@@ -113,6 +130,10 @@ export default {
113130
continue;
114131
}
115132

133+
if (botId) {
134+
messages = messages.filter((message) => message.author.id !== botId);
135+
}
136+
116137
console.log(`${messages.length} new messages in thread ${channelId}`);
117138

118139
messages = await Promise.all(messages.map(async (message) => ({

components/discord_bot/sources/new-guild-member/new-guild-member.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2-
import common from "../common.mjs";
2+
import common from "../common/common.mjs";
33

44
export default {
55
...common,
@@ -8,7 +8,7 @@ export default {
88
description: "Emit new event for every member added to a guild. [See docs here](https://discord.com/developers/docs/resources/guild#list-guild-members)",
99
type: "source",
1010
dedupe: "unique",
11-
version: "0.1.4",
11+
version: "0.1.5",
1212
props: {
1313
...common.props,
1414
db: "$.service.db",

components/discord_bot/sources/new-message-in-channel/new-message-in-channel.mjs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
22
import maxBy from "lodash.maxby";
3-
import common from "../common.mjs";
3+
import common from "../common/common.mjs";
44
import sampleEmit from "./test-event.mjs";
55

66
const { discord } = common.props;
@@ -11,8 +11,7 @@ export default {
1111
name: "New Message in Channel",
1212
description: "Emit new event for each message posted to one or more channels",
1313
type: "source",
14-
version: "0.0.18",
15-
14+
version: "0.0.19",
1615
dedupe: "unique", // Dedupe events based on the Discord message ID
1716
props: {
1817
...common.props,
@@ -37,16 +36,33 @@ export default {
3736
optional: true,
3837
default: false,
3938
},
39+
ignoreBotMessages: {
40+
type: "boolean",
41+
label: "Ignore Bot Messages",
42+
description: "Set to `true` to only emit messages NOT from the configured Discord bot",
43+
optional: true,
44+
},
4045
timer: {
4146
type: "$.interface.timer",
4247
default: {
4348
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
4449
},
4550
},
4651
},
52+
hooks: {
53+
async deploy() {
54+
if (this.ignoreBotMessages) {
55+
const { id } = await this.getBotProfile();
56+
this._setBotId(id);
57+
}
58+
},
59+
},
4760
async run({ $ }) {
4861
// We store a cursor to the last message ID
4962
let lastMessageIDs = this._getLastMessageIDs();
63+
const botId = this.ignoreBotMessages
64+
? this._getBotId()
65+
: null;
5066

5167
// If this is our first time running this source,
5268
// get the last N messages, emit them, and checkpoint
@@ -97,6 +113,10 @@ export default {
97113
return;
98114
}
99115

116+
if (botId) {
117+
messages = messages.filter((message) => message.author.id !== botId);
118+
}
119+
100120
console.log(`${messages.length} new messages in channel ${channelId}`);
101121

102122
// Batched emits do not take advantage of the built-in deduper

components/discord_bot/sources/new-tag-added-to-thread/new-tag-added-to-thread.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2-
import common from "../common.mjs";
2+
import common from "../common/common.mjs";
33
import sampleEmit from "./test-event.mjs";
44

55
export default {
@@ -8,7 +8,7 @@ export default {
88
name: "New Tag Added to Forum Thread",
99
description: "Emit new event when a new tag is added to a thread",
1010
type: "source",
11-
version: "0.0.1",
11+
version: "0.0.2",
1212
dedupe: "unique",
1313
props: {
1414
...common.props,

components/discord_bot/sources/new-thread-message/new-thread-message.mjs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
22
import maxBy from "lodash.maxby";
3-
import common from "../common.mjs";
3+
import common from "../common/common.mjs";
44
import sampleEmit from "./test-event.mjs";
55

66
export default {
@@ -9,7 +9,7 @@ export default {
99
name: "New Thread Message",
1010
description: "Emit new event for each thread message posted.",
1111
type: "source",
12-
version: "0.0.5",
12+
version: "0.0.6",
1313
dedupe: "unique", // Dedupe events based on the Discord message ID
1414
props: {
1515
...common.props,
@@ -20,10 +20,27 @@ export default {
2020
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
2121
},
2222
},
23+
ignoreBotMessages: {
24+
type: "boolean",
25+
label: "Ignore Bot Messages",
26+
description: "Set to `true` to only emit messages NOT from the configured Discord bot",
27+
optional: true,
28+
},
29+
},
30+
hooks: {
31+
async deploy() {
32+
if (this.ignoreBotMessages) {
33+
const { id } = await this.getBotProfile();
34+
this._setBotId(id);
35+
}
36+
},
2337
},
2438
async run({ $ }) {
2539
// We store a cursor to the last message ID
2640
let lastMessageIDs = this._getLastMessageIDs();
41+
const botId = this.ignoreBotMessages
42+
? this._getBotId()
43+
: null;
2744

2845
const { threads } = await this.discord.listThreads({
2946
$,
@@ -81,6 +98,10 @@ export default {
8198
continue;
8299
}
83100

101+
if (botId) {
102+
messages = messages.filter((message) => message.author.id !== botId);
103+
}
104+
84105
console.log(`${messages.length} new messages in thread ${channelId}`);
85106

86107
messages.reverse().forEach((message) => {

0 commit comments

Comments
 (0)