199 lines
8.3 KiB
JavaScript
199 lines
8.3 KiB
JavaScript
const path = require("path");
|
||
const Embed = require(path.join(__dirname, "../functions/embed"));
|
||
const Giveaways = require(path.join(__dirname, "../models/giveaways"));
|
||
|
||
// Constants
|
||
const EMOJIS = [
|
||
{ name: "1️⃣", id: 0 }, { name: "2️⃣", id: 1 }, { name: "3️⃣", id: 2 },
|
||
{ name: "4️⃣", id: 3 }, { name: "5️⃣", id: 4 }, { name: "6️⃣", id: 5 },
|
||
{ name: "7️⃣", id: 6 }, { name: "8️⃣", id: 7 }, { name: "9️⃣", id: 8 },
|
||
{ name: "🔟", id: 9 }, { name: "🛑", id: "stop" }
|
||
];
|
||
const COLORS_REGEX = /^([A-Z0-9]+)/;
|
||
|
||
/**
|
||
* Handles message collector reaction removal
|
||
* @param {Object} client - The client instance
|
||
* @param {Object} message - The message object
|
||
* @param {string} emojiId - The emoji ID
|
||
* @param {Object} collector - The message collector
|
||
* @returns {Promise<void>}
|
||
*/
|
||
async function handleMessageCollector(client, message, emojiId, collector) {
|
||
const emoji = collector.rolesDone.find(e => e.emoji === emojiId);
|
||
if (!emoji) return;
|
||
|
||
collector.rolesDone = collector.rolesDone.filter(object => object.emoji !== emojiId);
|
||
collector.roles.push([emoji.role, { name: emoji.name, colour: emoji.color }]);
|
||
collector.regex.push(emoji.name);
|
||
|
||
const emote = COLORS_REGEX.test(emojiId) ? `:${emojiId}:` : emojiId;
|
||
const content = collector.type === "content"
|
||
? { content: message.content.replace(`${emote} $\\text{\\textcolor{${emoji.color}}{${emoji.name}}}$`, `{role:${emoji.name}}`) }
|
||
: { embeds: [new Embed().setColor("#A52F05").setDescription(client.messages.get(message.id).embeds[0].description.replace(`{role:${collector.regex[0]}}`, `:${emojiId}: $\\text{\\textcolor{${collector.roles[0][1].colour?.includes("linear-gradient") ? '#000000' : collector.roles[0][1].colour}}{${collector.roles[0][1].name}}}$`))] };
|
||
|
||
return message.edit(content).catch(() => {});
|
||
}
|
||
|
||
/**
|
||
* Handles poll reaction removal
|
||
* @param {Object} client - The client instance
|
||
* @param {Object} message - The message object
|
||
* @param {Object} pollCheck - The poll check object
|
||
* @param {string} userId - The user ID
|
||
* @param {string} emojiId - The emoji ID
|
||
* @returns {Promise<void>}
|
||
*/
|
||
async function handlePoll(client, message, pollCheck, userId, emojiId) {
|
||
if (client.reactions.get(userId)) {
|
||
return client.users.get(userId)?.openDM()
|
||
.then(dm => dm.sendMessage(client.translate.get(pollCheck.language, "Events.messageReactionRemove.tooFast")))
|
||
.catch(() => {});
|
||
}
|
||
|
||
const convert = EMOJIS.findIndex(e => e.name === emojiId);
|
||
if (convert !== 0 && convert === 10 || convert === -1) return;
|
||
|
||
if (!pollCheck.users.includes(userId)) return;
|
||
|
||
const tooMuch = [];
|
||
if (pollCheck.poll.options.description.length > 80) {
|
||
tooMuch.push(`**${client.translate.get(pollCheck.language, "Events.messageReactionRemove.title")}**: ${pollCheck.poll.options.description}`);
|
||
}
|
||
pollCheck.poll.voteOptions.name.filter(e => e).forEach((e, i) => {
|
||
if (e.length > 70) {
|
||
tooMuch.push(`**${i + 1}.** ${e}`);
|
||
}
|
||
});
|
||
|
||
pollCheck.users = pollCheck.users.filter(object => object !== userId);
|
||
const user = client.users.get(userId) || await client.users.fetch(userId);
|
||
await pollCheck.poll.removeVote(convert, userId, 'https://chat.f95.io/api/users/01HATCWS7XZ7KEHW64AV20SMKR/default_avatar', message.id);
|
||
|
||
await message.edit({
|
||
embeds: [new Embed()
|
||
.setDescription(tooMuch.length > 0 ? tooMuch.join("\n") : null)
|
||
.setMedia(await client.Uploader.upload(pollCheck.poll.canvas.toBuffer(), `Poll.png`))
|
||
.setColor("#A52F05")]
|
||
}).catch(() => {});
|
||
|
||
client.reactions.set(userId, Date.now() + 3000);
|
||
setTimeout(() => client.reactions.delete(userId), 3000);
|
||
}
|
||
|
||
/**
|
||
* Handles giveaway reaction removal
|
||
* @param {Object} client - The client instance
|
||
* @param {Object} message - The message object
|
||
* @param {Object} db - The giveaway database object
|
||
* @param {string} userId - The user ID
|
||
* @param {string} emojiId - The emoji ID
|
||
* @returns {Promise<void>}
|
||
*/
|
||
async function handleGiveaway(client, message, db, userId, emojiId) {
|
||
if (client.reactions.get(userId)) return;
|
||
|
||
if (emojiId === client.config.emojis.confetti && !db.ended) {
|
||
if (!db.users.find(u => u.userID === userId)) return;
|
||
|
||
db.users = db.users.filter(object => object.userID !== userId);
|
||
db.picking = db.picking.filter(object => object.userID !== userId);
|
||
await db.save();
|
||
|
||
client.reactions.set(userId, Date.now() + 3000);
|
||
setTimeout(() => client.reactions.delete(userId), 3000);
|
||
|
||
await client.users.get(userId)?.openDM()
|
||
.then(dm => dm.sendMessage(
|
||
`${client.translate.get(db.language, "Events.messageReactionRemove.left")} [${db.prize}](https://chat.f95.io/server/${db.serverId}/channel/${db.channelId}/${db.messageId})!\n` +
|
||
`${client.translate.get(db.language, "Events.messageReactionRemove.left2")} **${db.users.length}** ${client.translate.get(db.language, "Events.messageReactionRemove.left")}!`
|
||
))
|
||
.catch(() => {});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Handles role reaction removal
|
||
* @param {Object} client - The client instance
|
||
* @param {Object} message - The message object
|
||
* @param {Object} db - The guild database object
|
||
* @param {string} userId - The user ID
|
||
* @param {string} emojiId - The emoji ID
|
||
* @returns {Promise<void>}
|
||
*/
|
||
async function handleRoleReaction(client, message, db, userId, emojiId) {
|
||
if (client.reactions.get(userId)) return;
|
||
|
||
const roles = [];
|
||
db.roles.find(e => e.msgId === message.id).roles.map(e => roles.push(e));
|
||
const role = roles.find(e => e.emoji === emojiId);
|
||
|
||
const member = await (client.servers.get(message.server.id) || await client.servers.fetch(message.server.id))?.fetchMember(userId);
|
||
if (!member) return;
|
||
|
||
let error = false;
|
||
let dataRoles = [];
|
||
if (member.roles) member.roles.map(e => dataRoles.push(e));
|
||
if (!dataRoles.includes(role.role)) return;
|
||
|
||
client.reactions.set(userId, Date.now() + 3000);
|
||
setTimeout(() => client.reactions.delete(userId), 3000);
|
||
|
||
dataRoles = dataRoles.filter(object => object !== role.role);
|
||
await member.edit({ roles: dataRoles }).catch(() => { error = true });
|
||
|
||
if (db.dm) {
|
||
const dmMessage = error
|
||
? client.translate.get(db.language, "Events.messageReactionRemove.noPerms").replace("{role}", `**${role.name}**`)
|
||
: client.translate.get(db.language, "Events.messageReactionRemove.success").replace("{role}", `**${role.name}**`);
|
||
|
||
await member?.user?.openDM()
|
||
.then(dm => dm.sendMessage(dmMessage))
|
||
.catch(() => {});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Main event handler for message reaction removal
|
||
* @param {Object} client - The client instance
|
||
* @param {Object} message - The message object
|
||
* @param {string} userId - The user ID
|
||
* @param {string} emojiId - The emoji ID
|
||
* @returns {Promise<void>}
|
||
*/
|
||
module.exports = async (client, message, userId, emojiId) => {
|
||
try {
|
||
const pollCheck = client.polls.get(message.id);
|
||
const collector = client.messageCollector.get(userId);
|
||
const editCollector = client.messageEdit.get(userId);
|
||
|
||
// Handle message collector
|
||
if (collector && collector.messageId === message.id && collector.channelId === message.channelId) {
|
||
return await handleMessageCollector(client, message, emojiId, collector);
|
||
}
|
||
|
||
// Handle edit collector
|
||
if (editCollector && editCollector.messageId === message.id && editCollector.channelId === message.channelId) {
|
||
return await handleMessageCollector(client, message, emojiId, editCollector);
|
||
}
|
||
|
||
// Handle poll
|
||
if (pollCheck) {
|
||
return await handlePoll(client, message, pollCheck, userId, emojiId);
|
||
}
|
||
|
||
// Handle giveaway
|
||
const giveaway = await Giveaways.findOne({ messageId: message.id });
|
||
if (giveaway && !giveaway.ended) {
|
||
return await handleGiveaway(client, message, giveaway, userId, emojiId);
|
||
}
|
||
|
||
// Handle role reaction
|
||
const db = await client.database.getGuild(message.server.id, true);
|
||
if (db?.roles.find(e => e.msgId === message.id)?.roles.find(e => e.emoji === emojiId)) {
|
||
return await handleRoleReaction(client, message, db, userId, emojiId);
|
||
}
|
||
} catch (error) {
|
||
console.error('Error in messageReactionRemove:', error);
|
||
}
|
||
}; |