DiscordJS-Template/events/reactions/reactionRemove.js

51 lines
1.2 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-06 18:12:34 +00:00
import { Message, RoleEmojiPair } from '../../database.js';
2024-02-06 20:25:47 +00:00
2024-02-05 01:56:18 +00:00
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: {
2024-02-06 15:18:06 +00:00
id: msgID
2024-02-05 01:56:18 +00:00
}
});
// Ignore if unregistered
if (message === null) return;
// Get emoji
const emoji = reaction.emoji.toString();
const rep = await RoleEmojiPair.findOne({
where: {
message: msgID,
2024-02-06 15:18:06 +00:00
emoji
2024-02-05 01:56:18 +00:00
}
});
// Deny if unregistered
if (rep === null) return;
// Fetch role from guild
const guild = reaction.message.guild;
const role = await guild.roles.fetch(rep.role);
2024-02-05 20:19:47 +00:00
// Role not found
if (role === null) {
await user.send('Could not fetch role! Please contact server staff.');
return;
}
2024-02-05 01:56:18 +00:00
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
}