Skip to content

Commit

Permalink
drops should only end with all valid winners set
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico105 committed Mar 19, 2022
1 parent a7b3808 commit e42e482
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e42e482

Please sign in to comment.