diff options
| author | Andrew Lee <andrew@alee14.me> | 2026-04-20 17:03:16 -0400 |
|---|---|---|
| committer | Andrew Lee <andrew@alee14.me> | 2026-04-20 17:03:16 -0400 |
| commit | a0bb6f0a3ef75fa46a982384a00a85a9b652d607 (patch) | |
| tree | 1f5395791bffb2f4d6c354f34b4a1629b697b127 /bot | |
| parent | 98805430b78fa24a7d80808c50f3c38a3a748f07 (diff) | |
| download | bnbmc-announcement-api-a0bb6f0a3ef75fa46a982384a00a85a9b652d607.tar.gz bnbmc-announcement-api-a0bb6f0a3ef75fa46a982384a00a85a9b652d607.tar.bz2 bnbmc-announcement-api-a0bb6f0a3ef75fa46a982384a00a85a9b652d607.zip | |
add reaction support
Diffstat (limited to 'bot')
| -rw-r--r-- | bot/index.ts | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/bot/index.ts b/bot/index.ts index 2c8bfd2..fd1f9f5 100644 --- a/bot/index.ts +++ b/bot/index.ts @@ -4,7 +4,7 @@ import { writeFileSync, mkdirSync, rmSync } from 'fs'; import { marked } from 'marked'; import { Database } from "bun:sqlite"; -const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); +const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessageReactions] }); const db = new Database(process.env.DB_LOCATION, { create: true }); function parseMessage(content: string) { @@ -47,6 +47,13 @@ client.on(Events.ClientReady, bot => { id INTEGER PRIMARY KEY, user_id INTERGER )`); + + db.run(`CREATE TABLE IF NOT EXISTS announcements_reactions ( + id INTEGER PRIMARY KEY, + msg_id INTERGER, + user_id INTERGER, + reaction TEXT + )`); } catch (e) { console.error(e); } @@ -156,6 +163,11 @@ client.on(Events.MessageDelete, async (msg) => { $msg_id: msg.id }) + const deleteAnnouncementReactions = db.prepare(`DELETE FROM announcements_reactions WHERE msg_id = ($msg_id)`); + deleteAnnouncementReactions.run({ + $msg_id: msg.id + }) + if (msg.attachments.size > 0) { const deleteAttachment = db.prepare(`DELETE FROM announcements_attachments WHERE msg_id = ($msg_id)`); deleteAttachment.run({ @@ -170,4 +182,40 @@ client.on(Events.MessageDelete, async (msg) => { }); +client.on(Events.MessageReactionAdd, async (reaction, user) => { + if (!reaction.message.author) return; + + if (user.bot) return; + if (reaction.message.channel.id !== process.env.ANNOUNCEMENT_CHANNEL) return; + + try { + const insert = db.prepare(`INSERT INTO announcements_reactions (msg_id, user_id, reaction) VALUES (($msg_id), ($user_id), ($reaction))`); + insert.run({ + $msg_id: reaction.message.id, + $user_id: user.id, + $reaction: reaction.emoji.toString() + }) + } catch (e) { + console.error(e); + } +}) + +client.on(Events.MessageReactionRemove, async (reaction, user) => { + if (!reaction.message.author) return; + + if (user.bot) return; + if (reaction.message.channel.id !== process.env.ANNOUNCEMENT_CHANNEL) return; + + try { + const deleteReaction = db.prepare(`DELETE FROM announcements WHERE msg_id = ($msg_id) AND user_id = ($user_id) AND reaction = ($reaction)`); + deleteReaction.run({ + $msg_id: reaction.message.id, + $user_id: user.id, + $reaction: reaction.emoji.toString() + }) + } catch (e) { + console.error(e); + } +}) + client.login(process.env.TOKEN); |
