Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async roll(winnerCount) {
- if (!this.message) return [];
- // Pick the winner
- const reactions = this.message.reactions.cache;
- const reaction = reactions.get(this.reaction) || reactions.find((r) => r.emoji.name === this.reaction);
- if (!reaction) return [];
- const guild = this.channel.guild;
- // Fetch guild members
- if (this.manager.options.hasGuildMembersIntent) await guild.members.fetch();
- const users = (await reaction.users.fetch())
- .filter((u) => !u.bot || u.bot === this.botsCanWin)
- .filter((u) => u.id !== this.message.client.user.id);
- let userArray;
- if (
- this.extraData &&
- this.extraData.bonusEntryRoles &&
- this.extraData.bonusEntryRoles.every(
- ber =>
- Object.keys(ber).every(k => k === 'roleID' || k === 'entries') &&
- typeof ber.roleID === 'string' &&
- typeof ber.entries === 'number'
- )
- ) {
- userArray = users.array().slice(); // Copy all users once
- /**
- * Each user who has a bonus entry role is pushed into the array
- * until they are in the array as many times as has been specified for the bonus entry role with the highest entrie amount which they own
- */
- for (const user of userArray.slice()) {
- const isUserValidEntry = await this.checkWinnerEntry(user);
- if (!isUserValidEntry) continue;
- const validBonusEntryRoles = this.extraData.bonusEntryRoles.filter(ber => Number.isInteger(ber.entries) && ber.entries > 1);
- if (!validBonusEntryRoles.length) continue;
- const memberRoles = this.channel.guild.member(user.id).roles.cache.array();
- const intersectedRoles = validBonusEntryRoles.filter(ber => memberRoles.map(r => r.id).includes(ber.roleID));
- if (!intersectedRoles.length) continue;
- const highestEntries = Math.max.apply(Math, intersectedRoles.map(ber => ber.entries))
- for (var i = 0; i < highestEntries - 1; i++) userArray.push(user)
- }
- }
- let rolledWinners;
- if (!userArray || userArray.length <= (winnerCount || this.winnerCount)) {
- rolledWinners = users.random(Math.min(winnerCount || this.winnerCount, users.size));
- } else {
- /** The same as https://github.com/discordjs/collection/blob/master/src/index.ts#L193
- * because collections/maps do not allow dublicates and so we need our own roll mechanism for the userArray
- */
- const amount = winnerCount || this.winnerCount;
- if (!amount) rolledWinners = userArray[Math.floor(Math.random() * userArray.length)];
- else rolledWinners = Array.from({ length: Math.min(amount, users.size) }, () => userArray.splice(Math.floor(Math.random() * userArray.length), 1)[0]);
- }
- const winners = [];
- for (const u of rolledWinners) {
- const isValidEntry = await this.checkWinnerEntry(u) && !winners.some((winner) => winner.id === u.id);
- if (isValidEntry) winners.push(u);
- else {
- // Find a new winner
- for (const user of (userArray || user.array())) {
- const alreadyRolled = winners.some((winner) => winner.id === user.id);
- if (alreadyRolled) continue;
- const isUserValidEntry = await this.checkWinnerEntry(user);
- if (!isUserValidEntry) continue;
- else {
- winners.push(user);
- break;
- }
- }
- }
- }
- return winners.map((user) => guild.member(user) || user);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement