DiscordJS-Template/events/reactionRemove.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-02-05 01:56:18 +00:00
import { config } from 'dotenv';
2024-01-26 19:23:45 +00:00
import { Events } from 'discord.js';
2024-02-05 01:56:18 +00:00
import { Message, RoleEmojiPair } from '../database.js';
config();
2024-01-26 19:23:45 +00:00
export const name = Events.MessageReactionRemove;
2024-01-28 17:33:03 +00:00
export async function execute(reaction, user) {
2024-02-05 01:56:18 +00:00
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;
2024-02-05 20:11:59 +00:00
try {
// Remove role from user
await guild.members.removeRole({ role, user });
console.info(`[INFO] Removed role with id '${role.id}' from user '${user.username}'.`);
} catch (error) {
// Missing permissions
console.error(error);
await user.send('Unable to retract role. Please contact server staff.');
}
2024-01-26 19:23:45 +00:00
}