From e42e48251f3ac69376fa06fcdc03381db4cf89f5 Mon Sep 17 00:00:00 2001 From: Nico105 <63612668+Nico105@users.noreply.github.com> Date: Sat, 19 Mar 2022 15:39:34 +0100 Subject: [PATCH] drops should only end with all valid winners set --- src/Manager.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Manager.js b/src/Manager.js index c58c6b54..ac7b0c10 100644 --- a/src/Manager.js +++ b/src/Manager.js @@ -278,10 +278,12 @@ class GiveawaysManager extends EventEmitter { if (giveaway.isDrop) { reaction.message .awaitReactions({ - filter: (r, u) => + filter: async (r, u) => [r.emoji.name, r.emoji.id] .filter(Boolean) - .includes(reaction.emoji.id ?? reaction.emoji.name) && u.id !== this.client.user.id, + .includes(reaction.emoji.id ?? reaction.emoji.name) && + u.id !== this.client.user.id && + (await giveaway.checkWinnerEntry(u)), maxUsers: giveaway.winnerCount }) .then(() => this.end(giveaway.messageId)) @@ -592,31 +594,42 @@ class GiveawaysManager extends EventEmitter { */ async _handleRawPacket(packet) { if (!['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(packet.t)) return; + if (packet.d.user_id === this.client.user.id) return; + const giveaway = this.giveaways.find((g) => g.messageId === packet.d.message_id); - if (!giveaway) return; - if (giveaway.ended && packet.t === 'MESSAGE_REACTION_REMOVE') return; + if (!giveaway || giveaway.ended && packet.t === 'MESSAGE_REACTION_REMOVE') return; + const guild = this.client.guilds.cache.get(packet.d.guild_id) || (await this.client.guilds.fetch(packet.d.guild_id).catch(() => {})); if (!guild || !guild.available) return; - if (packet.d.user_id === this.client.user.id) return; + const member = await guild.members.fetch(packet.d.user_id).catch(() => {}); if (!member) return; + const channel = await this.client.channels.fetch(packet.d.channel_id).catch(() => {}); if (!channel) return; + const message = await channel.messages.fetch(packet.d.message_id).catch(() => {}); if (!message) return; + const emoji = Discord.Util.resolvePartialEmoji(giveaway.reaction); const reaction = message.reactions.cache.find((r) => [r.emoji.name, r.emoji.id].filter(Boolean).includes(emoji?.id ?? emoji?.name) ); - if (!reaction) return; - if (reaction.emoji.name !== packet.d.emoji.name) return; + if (!reaction || reaction.emoji.name !== packet.d.emoji.name) return; if (reaction.emoji.id && reaction.emoji.id !== packet.d.emoji.id) return; + if (packet.t === 'MESSAGE_REACTION_ADD') { if (giveaway.ended) return this.emit('endedGiveawayReactionAdded', giveaway, member, reaction); this.emit('giveawayReactionAdded', giveaway, member, reaction); + if (giveaway.isDrop && reaction.count - 1 >= giveaway.winnerCount) { + const users = (await reaction.users.fetch().catch(() => {})) + ?.filter((u) => !u.bot || u.bot === giveaway.botsCanWin) + .filter((u) => u.id !== this.client.user.id) + .filter(async (u) => await giveaway.checkWinnerEntry(u)); + if (users.size < giveaway.WinnerCount) return; this.end(giveaway.messageId).catch(() => {}); } } else this.emit('giveawayReactionRemoved', giveaway, member, reaction);