From e8ea72a412be868a3f2211bfe8dba62eb4702036 Mon Sep 17 00:00:00 2001 From: Michelle Bergeron Date: Wed, 25 Jun 2025 14:11:29 -0400 Subject: [PATCH 1/2] ignore bot messages --- components/discord_bot/package.json | 2 +- .../sources/{ => common}/common.mjs | 15 +++++++++-- .../new-forum-thread-message.mjs | 25 ++++++++++++++++-- .../new-guild-member/new-guild-member.mjs | 4 +-- .../new-message-in-channel.mjs | 26 ++++++++++++++++--- .../new-tag-added-to-thread.mjs | 4 +-- .../new-thread-message/new-thread-message.mjs | 25 ++++++++++++++++-- 7 files changed, 87 insertions(+), 14 deletions(-) rename components/discord_bot/sources/{ => common}/common.mjs (58%) diff --git a/components/discord_bot/package.json b/components/discord_bot/package.json index 4784042ffdc43..df0d89810e0e5 100644 --- a/components/discord_bot/package.json +++ b/components/discord_bot/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/discord_bot", - "version": "0.6.1", + "version": "0.6.2", "description": "Pipedream Discord_bot Components", "main": "discord_bot.app.mjs", "keywords": [ diff --git a/components/discord_bot/sources/common.mjs b/components/discord_bot/sources/common/common.mjs similarity index 58% rename from components/discord_bot/sources/common.mjs rename to components/discord_bot/sources/common/common.mjs index 4f3ed1d0ae12d..98c84f3aabd9a 100644 --- a/components/discord_bot/sources/common.mjs +++ b/components/discord_bot/sources/common/common.mjs @@ -1,5 +1,5 @@ -import common from "../common.mjs"; -import constants from "../common/constants.mjs"; +import common from "../../common.mjs"; +import constants from "../../common/constants.mjs"; export default { ...common, @@ -17,5 +17,16 @@ export default { _setLastMemberID(memberID) { this.db.set(constants.LAST_MEMBER_ID, memberID); }, + _getBotId() { + return this.db.get("botId"); + }, + _setBotId(botId) { + this.db.set("botId", botId); + }, + getBotProfile() { + return this.discord._makeRequest({ + path: "/users/@me", + }); + }, }, }; diff --git a/components/discord_bot/sources/new-forum-thread-message/new-forum-thread-message.mjs b/components/discord_bot/sources/new-forum-thread-message/new-forum-thread-message.mjs index 5466e5abfe46d..c09fbf5434dae 100644 --- a/components/discord_bot/sources/new-forum-thread-message/new-forum-thread-message.mjs +++ b/components/discord_bot/sources/new-forum-thread-message/new-forum-thread-message.mjs @@ -1,7 +1,7 @@ import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; import maxBy from "lodash.maxby"; import constants from "../../common/constants.mjs"; -import common from "../common.mjs"; +import common from "../common/common.mjs"; import sampleEmit from "./test-event.mjs"; export default { @@ -10,7 +10,7 @@ export default { name: "New Forum Thread Message", 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).", type: "source", - version: "0.0.4", + version: "0.0.5", dedupe: "unique", // Dedupe events based on the Discord message ID props: { ...common.props, @@ -35,6 +35,20 @@ export default { label: "Forum Id", description: "Select the forum you want to watch.", }, + ignoreBotMessages: { + type: "boolean", + label: "Ignore Bot Messages", + description: "Set to `true` to only emit messages NOT from the configured Discord bot", + optional: true, + }, + }, + hooks: { + async deploy() { + if (this.ignoreBotMessages) { + const { id } = await this.getBotProfile(); + this._setBotId(id); + } + }, }, methods: { ...common.methods, @@ -47,6 +61,9 @@ export default { async run({ $ }) { // We store a cursor to the last message ID let lastMessageIDs = this._getLastMessageIDs(); + const botId = this.ignoreBotMessages + ? this._getBotId() + : null; const { threads } = await this.discord.listThreads({ $, @@ -113,6 +130,10 @@ export default { continue; } + if (botId) { + messages = messages.filter((message) => message.author.id !== botId); + } + console.log(`${messages.length} new messages in thread ${channelId}`); messages = await Promise.all(messages.map(async (message) => ({ diff --git a/components/discord_bot/sources/new-guild-member/new-guild-member.mjs b/components/discord_bot/sources/new-guild-member/new-guild-member.mjs index 5e94fd29b1ec3..226b5adf4bd6d 100644 --- a/components/discord_bot/sources/new-guild-member/new-guild-member.mjs +++ b/components/discord_bot/sources/new-guild-member/new-guild-member.mjs @@ -1,5 +1,5 @@ import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; -import common from "../common.mjs"; +import common from "../common/common.mjs"; export default { ...common, @@ -8,7 +8,7 @@ export default { description: "Emit new event for every member added to a guild. [See docs here](https://discord.com/developers/docs/resources/guild#list-guild-members)", type: "source", dedupe: "unique", - version: "0.1.4", + version: "0.1.5", props: { ...common.props, db: "$.service.db", diff --git a/components/discord_bot/sources/new-message-in-channel/new-message-in-channel.mjs b/components/discord_bot/sources/new-message-in-channel/new-message-in-channel.mjs index ffa263725d0a9..f4bbe7efcb20a 100644 --- a/components/discord_bot/sources/new-message-in-channel/new-message-in-channel.mjs +++ b/components/discord_bot/sources/new-message-in-channel/new-message-in-channel.mjs @@ -1,6 +1,6 @@ import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; import maxBy from "lodash.maxby"; -import common from "../common.mjs"; +import common from "../common/common.mjs"; import sampleEmit from "./test-event.mjs"; const { discord } = common.props; @@ -11,8 +11,7 @@ export default { name: "New Message in Channel", description: "Emit new event for each message posted to one or more channels", type: "source", - version: "0.0.18", - + version: "0.0.19", dedupe: "unique", // Dedupe events based on the Discord message ID props: { ...common.props, @@ -37,6 +36,12 @@ export default { optional: true, default: false, }, + ignoreBotMessages: { + type: "boolean", + label: "Ignore Bot Messages", + description: "Set to `true` to only emit messages NOT from the configured Discord bot", + optional: true, + }, timer: { type: "$.interface.timer", default: { @@ -44,9 +49,20 @@ export default { }, }, }, + hooks: { + async deploy() { + if (this.ignoreBotMessages) { + const { id } = await this.getBotProfile(); + this._setBotId(id); + } + }, + }, async run({ $ }) { // We store a cursor to the last message ID let lastMessageIDs = this._getLastMessageIDs(); + const botId = this.ignoreBotMessages + ? this._getBotId() + : null; // If this is our first time running this source, // get the last N messages, emit them, and checkpoint @@ -97,6 +113,10 @@ export default { return; } + if (botId) { + messages = messages.filter((message) => message.author.id !== botId); + } + console.log(`${messages.length} new messages in channel ${channelId}`); // Batched emits do not take advantage of the built-in deduper diff --git a/components/discord_bot/sources/new-tag-added-to-thread/new-tag-added-to-thread.mjs b/components/discord_bot/sources/new-tag-added-to-thread/new-tag-added-to-thread.mjs index cb5b8dc58303f..b226be888cf76 100644 --- a/components/discord_bot/sources/new-tag-added-to-thread/new-tag-added-to-thread.mjs +++ b/components/discord_bot/sources/new-tag-added-to-thread/new-tag-added-to-thread.mjs @@ -1,5 +1,5 @@ import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; -import common from "../common.mjs"; +import common from "../common/common.mjs"; import sampleEmit from "./test-event.mjs"; export default { @@ -8,7 +8,7 @@ export default { name: "New Tag Added to Forum Thread", description: "Emit new event when a new tag is added to a thread", type: "source", - version: "0.0.1", + version: "0.0.2", dedupe: "unique", props: { ...common.props, diff --git a/components/discord_bot/sources/new-thread-message/new-thread-message.mjs b/components/discord_bot/sources/new-thread-message/new-thread-message.mjs index de5ee3bf09763..a2aab20e0d209 100644 --- a/components/discord_bot/sources/new-thread-message/new-thread-message.mjs +++ b/components/discord_bot/sources/new-thread-message/new-thread-message.mjs @@ -1,6 +1,6 @@ import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; import maxBy from "lodash.maxby"; -import common from "../common.mjs"; +import common from "../common/common.mjs"; import sampleEmit from "./test-event.mjs"; export default { @@ -9,7 +9,7 @@ export default { name: "New Thread Message", description: "Emit new event for each thread message posted.", type: "source", - version: "0.0.5", + version: "0.0.6", dedupe: "unique", // Dedupe events based on the Discord message ID props: { ...common.props, @@ -20,10 +20,27 @@ export default { intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, }, }, + ignoreBotMessages: { + type: "boolean", + label: "Ignore Bot Messages", + description: "Set to `true` to only emit messages NOT from the configured Discord bot", + optional: true, + }, + }, + hooks: { + async deploy() { + if (this.ignoreBotMessages) { + const { id } = await this.getBotProfile(); + this._setBotId(id); + } + }, }, async run({ $ }) { // We store a cursor to the last message ID let lastMessageIDs = this._getLastMessageIDs(); + const botId = this.ignoreBotMessages + ? this._getBotId() + : null; const { threads } = await this.discord.listThreads({ $, @@ -81,6 +98,10 @@ export default { continue; } + if (botId) { + messages = messages.filter((message) => message.author.id !== botId); + } + console.log(`${messages.length} new messages in thread ${channelId}`); messages.reverse().forEach((message) => { From e06197aabd45784d716adafb0f7efd5515ba9474 Mon Sep 17 00:00:00 2001 From: Michelle Bergeron Date: Wed, 25 Jun 2025 14:12:11 -0400 Subject: [PATCH 2/2] pnpm-lock.yaml --- pnpm-lock.yaml | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 30a05bb898595..3735cba00fd6c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5396,8 +5396,7 @@ importers: components/godial: {} - components/goformz: - specifiers: {} + components/goformz: {} components/gohighlevel: dependencies: @@ -7288,8 +7287,7 @@ importers: specifier: ^3.0.0 version: 3.0.3 - components/lark: - specifiers: {} + components/lark: {} components/lastpass: dependencies: @@ -8494,8 +8492,7 @@ importers: components/mollie: {} - components/momentum_ams: - specifiers: {} + components/momentum_ams: {} components/monday: dependencies: @@ -9478,8 +9475,7 @@ importers: components/order_sender: {} - components/orderspace: - specifiers: {} + components/orderspace: {} components/originality_ai: dependencies: @@ -11556,8 +11552,7 @@ importers: components/ryver: {} - components/sage_accounting: - specifiers: {} + components/sage_accounting: {} components/sage_intacct: {} @@ -12974,8 +12969,7 @@ importers: components/stealthseminar: {} - components/stiply: - specifiers: {} + components/stiply: {} components/storeganise: dependencies: