Skip to content

Commit 6856152

Browse files
michelle0927lcaresia
authored andcommitted
Discord Bot - Source updates (#14472)
* updates * pnpm-lock.yaml * update * include tag objects in applied_tags
1 parent d956d5c commit 6856152

File tree

6 files changed

+205
-8
lines changed

6 files changed

+205
-8
lines changed

components/discord_bot/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/discord_bot",
3-
"version": "0.5.5",
3+
"version": "0.6.0",
44
"description": "Pipedream Discord_bot Components",
55
"main": "discord_bot.app.js",
66
"keywords": [
@@ -10,7 +10,7 @@
1010
"homepage": "https://pipedream.com/apps/discord_bot",
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"dependencies": {
13-
"@pipedream/platform": "^1.6.0",
13+
"@pipedream/platform": "^3.0.3",
1414
"form-data": "^4.0.0",
1515
"lodash.maxby": "^4.6.0"
1616
},

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export default {
88
...common,
99
key: "discord_bot-new-forum-thread-message",
1010
name: "New Forum Thread Message",
11-
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).",
11+
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.3",
13+
version: "0.0.4",
1414
dedupe: "unique", // Dedupe events based on the Discord message ID
1515
props: {
1616
...common.props,
@@ -36,6 +36,14 @@ export default {
3636
description: "Select the forum you want to watch.",
3737
},
3838
},
39+
methods: {
40+
...common.methods,
41+
getChannel(id) {
42+
return this.discord._makeRequest({
43+
path: `/channels/${id}`,
44+
});
45+
},
46+
},
3947
async run({ $ }) {
4048
// We store a cursor to the last message ID
4149
let lastMessageIDs = this._getLastMessageIDs();
@@ -107,6 +115,21 @@ export default {
107115

108116
console.log(`${messages.length} new messages in thread ${channelId}`);
109117

118+
messages = await Promise.all(messages.map(async (message) => ({
119+
...message,
120+
thread: await this.getChannel(message.channel_id),
121+
})));
122+
123+
const { available_tags: availableTags = [] } = await this.getChannel(this.forumId);
124+
for (const message of messages) {
125+
if (!message.thread.applied_tags) {
126+
message.thread.applied_tags = [];
127+
}
128+
message.thread.applied_tags = message.thread.applied_tags.map((tagId) => ({
129+
...availableTags.find(({ id }) => id === tagId),
130+
}));
131+
}
132+
110133
messages.reverse().forEach((message) => {
111134
this.$emit(message, {
112135
id: message.id, // dedupes events based on this ID

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,45 @@ export default {
2828
"edited_timestamp": null,
2929
"flags": 0,
3030
"components": [],
31-
"position": 13
32-
}
31+
"position": 13,
32+
"thread": {
33+
"id": "1301256410990116917",
34+
"type": 11,
35+
"last_message_id": "1301256410990116917",
36+
"flags": 0,
37+
"guild_id": "901259362205589565",
38+
"name": "hello world",
39+
"parent_id": "1301256016934994024",
40+
"rate_limit_per_user": 0,
41+
"bitrate": 64000,
42+
"user_limit": 0,
43+
"rtc_region": null,
44+
"owner_id": "867892178135023656",
45+
"thread_metadata": {
46+
"archived": false,
47+
"archive_timestamp": "2024-10-30T18:48:24.555000+00:00",
48+
"auto_archive_duration": 4320,
49+
"locked": false,
50+
"create_timestamp": "2024-10-30T18:48:24.555000+00:00",
51+
},
52+
"message_count": 0,
53+
"member_count": 1,
54+
"total_message_sent": 0,
55+
"applied_tags": [
56+
{
57+
"id": "1301256232052457563",
58+
"name": "tag",
59+
"moderated": false,
60+
"emoji_id": null,
61+
"emoji_name": null,
62+
},
63+
{
64+
"id": "1301281978968178759",
65+
"name": "tag2",
66+
"moderated": false,
67+
"emoji_id": null,
68+
"emoji_name": null,
69+
},
70+
],
71+
},
72+
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import common from "../common.mjs";
3+
import sampleEmit from "./test-event.mjs";
4+
5+
export default {
6+
...common,
7+
key: "discord_bot-new-tag-added-to-thread",
8+
name: "New Tag Added to Forum Thread",
9+
description: "Emit new event when a new tag is added to a thread",
10+
type: "source",
11+
version: "0.0.1",
12+
dedupe: "unique",
13+
props: {
14+
...common.props,
15+
db: "$.service.db",
16+
timer: {
17+
type: "$.interface.timer",
18+
default: {
19+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
20+
},
21+
},
22+
},
23+
hooks: {
24+
async deploy() {
25+
const tags = {};
26+
const { threads } = await this.discord.listThreads({
27+
guildId: this.guildId,
28+
});
29+
threads.forEach((thread) => {
30+
if (thread?.applied_tags) {
31+
tags[thread.id] = thread?.applied_tags;
32+
}
33+
});
34+
this._setTags(tags);
35+
},
36+
},
37+
methods: {
38+
...common.methods,
39+
_getTags() {
40+
return this.db.get("tags") || {};
41+
},
42+
_setTags(tags) {
43+
this.db.set("tags", tags);
44+
},
45+
generateMeta(thread) {
46+
return {
47+
id: thread.id,
48+
summary: `New tag in thread ${thread.id}`,
49+
ts: Date.now(),
50+
};
51+
},
52+
getChannel(id) {
53+
return this.discord._makeRequest({
54+
path: `/channels/${id}`,
55+
});
56+
},
57+
},
58+
async run() {
59+
let tags = this._getTags();
60+
61+
const { threads } = await this.discord.listThreads({
62+
guildId: this.guildId,
63+
});
64+
65+
for (const thread of threads) {
66+
if (!thread.applied_tags) {
67+
continue;
68+
}
69+
if (thread.applied_tags.some((tag) => !tags[thread.id] || !tags[thread.id].includes(tag))) {
70+
tags[thread.id] = thread.applied_tags;
71+
72+
const { available_tags: availableTags = [] } = await this.getChannel(thread.parent_id);
73+
74+
thread.applied_tags = thread.applied_tags.map((tagId) => ({
75+
...availableTags.find(({ id }) => id === tagId),
76+
}));
77+
78+
const meta = this.generateMeta(thread);
79+
this.$emit(thread, meta);
80+
81+
}
82+
}
83+
84+
this._setTags(tags);
85+
},
86+
sampleEmit,
87+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export default {
2+
"id": "1301256410990116917",
3+
"type": 11,
4+
"last_message_id": "1301256410990116917",
5+
"flags": 0,
6+
"guild_id": "901259362205589565",
7+
"name": "hello world",
8+
"parent_id": "1301256016934994024",
9+
"rate_limit_per_user": 0,
10+
"bitrate": 64000,
11+
"user_limit": 0,
12+
"rtc_region": null,
13+
"owner_id": "867892178135023656",
14+
"thread_metadata": {
15+
"archived": false,
16+
"archive_timestamp": "2024-10-30T18:48:24.555000+00:00",
17+
"auto_archive_duration": 4320,
18+
"locked": false,
19+
"create_timestamp": "2024-10-30T18:48:24.555000+00:00"
20+
},
21+
"message_count": 0,
22+
"member_count": 1,
23+
"total_message_sent": 0,
24+
"applied_tags": [
25+
{
26+
"id": "1301256232052457563",
27+
"name": "tag",
28+
"moderated": false,
29+
"emoji_id": null,
30+
"emoji_name": null,
31+
},
32+
{
33+
"id": "1301282004998033428",
34+
"name": "tag3",
35+
"moderated": false,
36+
"emoji_id": null,
37+
"emoji_name": null,
38+
},
39+
{
40+
"id": "1301281978968178759",
41+
"name": "tag2",
42+
"moderated": false,
43+
"emoji_id": null,
44+
"emoji_name": null,
45+
},
46+
],
47+
};

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)