diff options
| -rw-r--r-- | api/index.ts | 10 | ||||
| -rw-r--r-- | bot/index.ts | 50 |
2 files changed, 59 insertions, 1 deletions
diff --git a/api/index.ts b/api/index.ts index 9435bb1..a2a1378 100644 --- a/api/index.ts +++ b/api/index.ts @@ -25,6 +25,16 @@ app.get('/attachments/:slug', (req, res) => { } }); +app.get('/reactions/:slug', (req, res) => { + if (req.params.slug) { + const query = db.prepare(`SELECT * FROM announcements_reactions WHERE msg_id = (?)`); + const result = query.all(req.params.slug); + res.send(result); + } else { + res.send('Unknown message id.'); + } +}); + app.listen(port, () => { console.log(`Server is running on port ${port}`); }); 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); |
