From afa958b5571407ae2b8cf89594f14de1e3009f6d Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Mon, 5 Feb 2024 02:56:18 +0100 Subject: [PATCH] implement reaction add/remove events --- events/reactionAdd.js | 40 ++++++++++++++++++++++++++++++++++++---- events/reactionRemove.js | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/events/reactionAdd.js b/events/reactionAdd.js index c0b8de3..6207c40 100644 --- a/events/reactionAdd.js +++ b/events/reactionAdd.js @@ -1,11 +1,43 @@ -import { Events } from 'discord.js'; import { config } from 'dotenv'; +import { Events } from 'discord.js'; +import { Message, RoleEmojiPair } from '../database.js'; config(); export const name = Events.MessageReactionAdd; export async function execute(reaction, user) { if (user.id === process.env.CLIENT) return; - const uname = user.username; - const rname = reaction._emoji.name; - console.debug(`[DEBUG] User '${uname}' reacted with emoji '${rname}'.`); + + // Get message + const msgID = reaction.message.id; + const message = await Message.findOne({ + where: { + id: msgID, + } + }); + // Ignore if unregistered + if (message === null) return; + + // Get emoji + const emoji = reaction.emoji.toString(); + const rep = await RoleEmojiPair.findOne({ + where: { + message: msgID, + emoji, + } + }); + // Deny if unregistered + if (rep === null) { + // Remove reaction and quit + await reaction.remove(); + return; + } + + // Fetch role from guild + const guild = reaction.message.guild; + const role = await guild.roles.fetch(rep.role); + if (role === null) return; + + // Add role to user + await guild.members.addRole({ role, user }); + console.info(`[INFO] Added role with id '${role.id}' to user '${user.username}'.`); } diff --git a/events/reactionRemove.js b/events/reactionRemove.js index 58d4c52..2888bf9 100644 --- a/events/reactionRemove.js +++ b/events/reactionRemove.js @@ -1,8 +1,39 @@ +import { config } from 'dotenv'; import { Events } from 'discord.js'; +import { Message, RoleEmojiPair } from '../database.js'; +config(); export const name = Events.MessageReactionRemove; export async function execute(reaction, user) { - const uname = user.username; - const rname = reaction._emoji.name; - console.debug(`[DEBUG] User '${uname}' removed reaction of emoji '${rname}'.`); + if (user.id === process.env.CLIENT) return; + + // Get message + const msgID = reaction.message.id; + const message = await Message.findOne({ + where: { + id: msgID, + } + }); + // Ignore if unregistered + if (message === null) return; + + // Get emoji + const emoji = reaction.emoji.toString(); + const rep = await RoleEmojiPair.findOne({ + where: { + message: msgID, + emoji, + } + }); + // Deny if unregistered + if (rep === null) return; + + // Fetch role from guild + const guild = reaction.message.guild; + const role = await guild.roles.fetch(rep.role); + if (role === null) return; + + // Add role to user + await guild.members.removeRole({ role, user }); + console.info(`[INFO] Removed role with id '${role.id}' from user '${user.username}'.`); }