DiscordJS-Template/events/reactionAdd.js

44 lines
1.0 KiB
JavaScript
Raw Normal View History

2024-02-04 22:41:04 +00:00
import { config } from 'dotenv';
2024-02-05 01:56:18 +00:00
import { Events } from 'discord.js';
import { Message, RoleEmojiPair } from '../database.js';
2024-02-04 22:41:04 +00:00
config();
2024-01-26 19:23:45 +00:00
export const name = Events.MessageReactionAdd;
2024-01-28 17:33:03 +00:00
export async function execute(reaction, user) {
2024-02-04 22:41:04 +00:00
if (user.id === process.env.CLIENT) return;
2024-02-05 01:56:18 +00:00
// 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}'.`);
2024-01-26 19:23:45 +00:00
}