From 3678c3171f42371860e9478e4c4a7f64521fdfe5 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Wed, 30 Oct 2024 16:36:23 -0400 Subject: [PATCH 1/4] updates --- components/discord_bot/package.json | 4 +- .../new-forum-thread-message.mjs | 15 +++- .../new-tag-added-to-thread.mjs | 75 +++++++++++++++++++ .../new-tag-added-to-thread/test-event.mjs | 28 +++++++ 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 components/discord_bot/sources/new-tag-added-to-thread/new-tag-added-to-thread.mjs create mode 100644 components/discord_bot/sources/new-tag-added-to-thread/test-event.mjs diff --git a/components/discord_bot/package.json b/components/discord_bot/package.json index 49ec80054ecdb..357cc70699011 100644 --- a/components/discord_bot/package.json +++ b/components/discord_bot/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/discord_bot", - "version": "0.5.5", + "version": "0.6.0", "description": "Pipedream Discord_bot Components", "main": "discord_bot.app.js", "keywords": [ @@ -10,7 +10,7 @@ "homepage": "https://pipedream.com/apps/discord_bot", "author": "Pipedream (https://pipedream.com/)", "dependencies": { - "@pipedream/platform": "^1.6.0", + "@pipedream/platform": "^3.0.3", "form-data": "^4.0.0", "lodash.maxby": "^4.6.0" }, 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 604978fde790b..7a5b403b5e102 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 @@ -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 docs here](https://discord.com/developers/docs/topics/gateway#message-content-intent).", type: "source", - version: "0.0.3", + version: "0.0.4", dedupe: "unique", // Dedupe events based on the Discord message ID props: { ...common.props, @@ -36,6 +36,14 @@ export default { description: "Select the forum you want to watch.", }, }, + methods: { + ...common.methods, + getChannel(id) { + return this.discord._makeRequest({ + path: `/channels/${id}`, + }); + }, + }, async run({ $ }) { // We store a cursor to the last message ID let lastMessageIDs = this._getLastMessageIDs(); @@ -107,6 +115,11 @@ export default { console.log(`${messages.length} new messages in thread ${channelId}`); + messages = await Promise.all(messages.map(async (message) => ({ + ...message, + thread: await this.getChannel(message.channel_id), + }))); + messages.reverse().forEach((message) => { this.$emit(message, { id: message.id, // dedupes events based on this ID 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 new file mode 100644 index 0000000000000..5d9152d709f45 --- /dev/null +++ b/components/discord_bot/sources/new-tag-added-to-thread/new-tag-added-to-thread.mjs @@ -0,0 +1,75 @@ +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; +import common from "../common.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "discord_bot-new-tag-added-to-thread", + name: "New Tag Added to Thread", + description: "Emit new event when a new tag is added to a thread", + type: "source", + version: "0.0.1", + dedupe: "unique", + props: { + ...common.props, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + }, + hooks: { + async deploy() { + const tags = {}; + const { threads } = await this.discord.listThreads({ + guildId: this.guildId, + }); + threads.forEach((thread) => { + if (thread?.applied_tags) { + tags[thread.id] = thread?.applied_tags; + } + }); + this._setTags(tags); + }, + }, + methods: { + ...common.methods, + _getTags() { + return this.db.get("tags") || {}; + }, + _setTags(tags) { + this.db.set("tags", tags); + }, + generateMeta(thread) { + return { + id: thread.id, + summary: `New tag in thread ${thread.id}`, + ts: Date.now(), + }; + }, + }, + async run() { + let tags = this._getTags(); + + const { threads } = await this.discord.listThreads({ + guildId: this.guildId, + }); + + for (const thread of threads) { + if (!thread.applied_tags) { + continue; + } + if (thread.applied_tags.some((tag) => !tags[thread.id].includes(tag))) { + const meta = this.generateMeta(thread); + this.$emit(thread, meta); + + tags[thread.id] = thread.applied_tags; + } + } + + this._setTags(tags); + }, + sampleEmit, +}; diff --git a/components/discord_bot/sources/new-tag-added-to-thread/test-event.mjs b/components/discord_bot/sources/new-tag-added-to-thread/test-event.mjs new file mode 100644 index 0000000000000..79a2334dff656 --- /dev/null +++ b/components/discord_bot/sources/new-tag-added-to-thread/test-event.mjs @@ -0,0 +1,28 @@ +export default { + "id": "1301256410990116917", + "type": 11, + "last_message_id": "1301256410990116917", + "flags": 0, + "guild_id": "901259362205589565", + "name": "hello world", + "parent_id": "1301256016934994024", + "rate_limit_per_user": 0, + "bitrate": 64000, + "user_limit": 0, + "rtc_region": null, + "owner_id": "867892178135023656", + "thread_metadata": { + "archived": false, + "archive_timestamp": "2024-10-30T18:48:24.555000+00:00", + "auto_archive_duration": 4320, + "locked": false, + "create_timestamp": "2024-10-30T18:48:24.555000+00:00" + }, + "message_count": 0, + "member_count": 1, + "total_message_sent": 0, + "applied_tags": [ + "1301256232052457563", + "1301281978968178759" + ] +} \ No newline at end of file From 3a3aaa047e7e00244b60ace06d72207cc149c81b Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Wed, 30 Oct 2024 16:37:27 -0400 Subject: [PATCH 2/4] pnpm-lock.yaml --- pnpm-lock.yaml | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 423a7143d9934..e3d63058a6458 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2644,11 +2644,11 @@ importers: components/discord_bot: specifiers: - '@pipedream/platform': ^1.6.0 + '@pipedream/platform': ^3.0.3 form-data: ^4.0.0 lodash.maxby: ^4.6.0 dependencies: - '@pipedream/platform': 1.6.2 + '@pipedream/platform': 3.0.3 form-data: 4.0.0 lodash.maxby: 4.6.0 @@ -21540,12 +21540,6 @@ packages: dependencies: undici-types: 5.26.5 - /@types/node/22.7.9: - resolution: {integrity: sha512-jrTfRC7FM6nChvU7X2KqcrgquofrWLFDeYC1hKfwNWomVvrn7JIksqf344WN2X/y8xrgqBd2dJATZV4GbatBfg==} - dependencies: - undici-types: 6.19.8 - dev: false - /@types/normalize-package-data/2.4.2: resolution: {integrity: sha512-lqa4UEhhv/2sjjIQgjX8B+RBjj47eo0mzGasklVJ78UKGQY1r0VpB9XHDaZZO9qzEFDdy4MrXLuEaSmPrPSe/A==} dev: true @@ -21679,7 +21673,7 @@ packages: /@types/ws/8.5.12: resolution: {integrity: sha512-3tPRkv1EtkDpzlgyKyI8pGsGZAGPEaXeu0DOj5DI25Ja91bdAYddYHbADRYVrZMRbfW+1l5YwXVDKohDJNQxkQ==} dependencies: - '@types/node': 22.7.9 + '@types/node': 20.16.1 dev: false /@types/ws/8.5.3: From ec0819bca6e33823543e8eb6680efe133e90ffcc Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Wed, 30 Oct 2024 16:41:23 -0400 Subject: [PATCH 3/4] update --- .../sources/new-tag-added-to-thread/new-tag-added-to-thread.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5d9152d709f45..0c24e5d3f82a6 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 @@ -61,7 +61,7 @@ export default { if (!thread.applied_tags) { continue; } - if (thread.applied_tags.some((tag) => !tags[thread.id].includes(tag))) { + if (thread.applied_tags.some((tag) => !tags[thread.id] || !tags[thread.id].includes(tag))) { const meta = this.generateMeta(thread); this.$emit(thread, meta); From 8aca7e9bc58baba095caab077d0cd35ca6437da1 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Tue, 5 Nov 2024 12:20:12 -0500 Subject: [PATCH 4/4] include tag objects in applied_tags --- .../new-forum-thread-message.mjs | 12 ++++- .../new-forum-thread-message/test-event.mjs | 44 ++++++++++++++++++- .../new-tag-added-to-thread.mjs | 16 ++++++- .../new-tag-added-to-thread/test-event.mjs | 27 ++++++++++-- 4 files changed, 90 insertions(+), 9 deletions(-) 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 7a5b403b5e102..5466e5abfe46d 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 @@ -8,7 +8,7 @@ export default { ...common, key: "discord_bot-new-forum-thread-message", 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 docs here](https://discord.com/developers/docs/topics/gateway#message-content-intent).", + 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", dedupe: "unique", // Dedupe events based on the Discord message ID @@ -120,6 +120,16 @@ export default { thread: await this.getChannel(message.channel_id), }))); + const { available_tags: availableTags = [] } = await this.getChannel(this.forumId); + for (const message of messages) { + if (!message.thread.applied_tags) { + message.thread.applied_tags = []; + } + message.thread.applied_tags = message.thread.applied_tags.map((tagId) => ({ + ...availableTags.find(({ id }) => id === tagId), + })); + } + messages.reverse().forEach((message) => { this.$emit(message, { id: message.id, // dedupes events based on this ID diff --git a/components/discord_bot/sources/new-forum-thread-message/test-event.mjs b/components/discord_bot/sources/new-forum-thread-message/test-event.mjs index 71fae6e2a5ef5..1a8687e5f2fae 100644 --- a/components/discord_bot/sources/new-forum-thread-message/test-event.mjs +++ b/components/discord_bot/sources/new-forum-thread-message/test-event.mjs @@ -28,5 +28,45 @@ export default { "edited_timestamp": null, "flags": 0, "components": [], - "position": 13 -} \ No newline at end of file + "position": 13, + "thread": { + "id": "1301256410990116917", + "type": 11, + "last_message_id": "1301256410990116917", + "flags": 0, + "guild_id": "901259362205589565", + "name": "hello world", + "parent_id": "1301256016934994024", + "rate_limit_per_user": 0, + "bitrate": 64000, + "user_limit": 0, + "rtc_region": null, + "owner_id": "867892178135023656", + "thread_metadata": { + "archived": false, + "archive_timestamp": "2024-10-30T18:48:24.555000+00:00", + "auto_archive_duration": 4320, + "locked": false, + "create_timestamp": "2024-10-30T18:48:24.555000+00:00", + }, + "message_count": 0, + "member_count": 1, + "total_message_sent": 0, + "applied_tags": [ + { + "id": "1301256232052457563", + "name": "tag", + "moderated": false, + "emoji_id": null, + "emoji_name": null, + }, + { + "id": "1301281978968178759", + "name": "tag2", + "moderated": false, + "emoji_id": null, + "emoji_name": null, + }, + ], + }, +}; 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 0c24e5d3f82a6..cb5b8dc58303f 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 @@ -5,7 +5,7 @@ import sampleEmit from "./test-event.mjs"; export default { ...common, key: "discord_bot-new-tag-added-to-thread", - name: "New Tag Added to Thread", + 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", @@ -49,6 +49,11 @@ export default { ts: Date.now(), }; }, + getChannel(id) { + return this.discord._makeRequest({ + path: `/channels/${id}`, + }); + }, }, async run() { let tags = this._getTags(); @@ -62,10 +67,17 @@ export default { continue; } if (thread.applied_tags.some((tag) => !tags[thread.id] || !tags[thread.id].includes(tag))) { + tags[thread.id] = thread.applied_tags; + + const { available_tags: availableTags = [] } = await this.getChannel(thread.parent_id); + + thread.applied_tags = thread.applied_tags.map((tagId) => ({ + ...availableTags.find(({ id }) => id === tagId), + })); + const meta = this.generateMeta(thread); this.$emit(thread, meta); - tags[thread.id] = thread.applied_tags; } } diff --git a/components/discord_bot/sources/new-tag-added-to-thread/test-event.mjs b/components/discord_bot/sources/new-tag-added-to-thread/test-event.mjs index 79a2334dff656..f6d7bcabf1448 100644 --- a/components/discord_bot/sources/new-tag-added-to-thread/test-event.mjs +++ b/components/discord_bot/sources/new-tag-added-to-thread/test-event.mjs @@ -22,7 +22,26 @@ export default { "member_count": 1, "total_message_sent": 0, "applied_tags": [ - "1301256232052457563", - "1301281978968178759" - ] -} \ No newline at end of file + { + "id": "1301256232052457563", + "name": "tag", + "moderated": false, + "emoji_id": null, + "emoji_name": null, + }, + { + "id": "1301282004998033428", + "name": "tag3", + "moderated": false, + "emoji_id": null, + "emoji_name": null, + }, + { + "id": "1301281978968178759", + "name": "tag2", + "moderated": false, + "emoji_id": null, + "emoji_name": null, + }, + ], +};