2024-02-11 01:04:12 +00:00
|
|
|
import { Events, MessageReaction, User } from 'discord.js';
|
2024-04-04 06:59:37 +00:00
|
|
|
import { Messages, RoleEmojiPairs } from '../../database.js';
|
2024-02-11 01:04:12 +00:00
|
|
|
import { config } from 'dotenv';
|
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-02-11 01:04:12 +00:00
|
|
|
/**
|
|
|
|
* @param {MessageReaction} reaction
|
|
|
|
* @param {User} user
|
|
|
|
*/
|
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;
|
2024-04-04 07:55:14 +00:00
|
|
|
/** @type {import('../../models/messages.js').Message|null} */
|
2024-04-04 06:59:37 +00:00
|
|
|
const message = await Messages.findOne({
|
2024-02-05 01:56:18 +00:00
|
|
|
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();
|
2024-04-04 07:55:14 +00:00
|
|
|
/** @type {import('../../models/roleEmojiPairs.js').RoleEmojiPair|null} */
|
2024-04-04 06:59:37 +00:00
|
|
|
const rep = await RoleEmojiPairs.findOne({
|
2024-02-05 01:56:18 +00:00
|
|
|
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
|
|
|
}
|