jeudi 20 juillet 2023

Discord.js Searching for a user in an array is always returning undefined

I've been trying to make a bot choose randomly from a list of players that reacted to a message and send the player it chooses, but it always sends undefined.

Here's the code for the randomizer.

const collector = giveawayMessage.createReactionCollector(filter, { time: timeRemaining });
const reactionUsers = [];
giveawayMessage.react('👍');
collector.on('collect', (reaction, user) => {
    if (reaction.emoji.name === '👍' && !user.bot) {
        for (let i = 0; i < reactionUsers.length; i++) {
            if (user === reactionUsers[i]) return;
        }
        reactionUsers.push(user);
    }
});
let chosenReaction;
for (let i = timeRemaining; i >= 0; i--) {
    delay(1000);
    timeRemaining--;
    numDays = Math.trunc(timeRemaining / 86400);
    numHours = Math.trunc((timeRemaining - (numDays * 86400)) / 3600);
    numMinutes = Math.trunc((timeRemaining - (numDays * 86400) - (numHours * 3600)) / 60);
    numSeconds = timeRemaining - (numDays * 86400) - (numHours * 3600) - (numMinutes * 60);
    timeRemainingFormatted = `${numDays}d ${numHours}h ${numMinutes}m ${numSeconds}s`;
    chosenReaction = Math.ceil(Math.random() * reactionUsers.length);
    embed = EmbedBuilder.from(embed).setFields({ name: 'Prize', value: `${interaction.options.getString('prize')}` }, { name: 'Time Remaining', value: `${timeRemainingFormatted}` });
    giveawayMessage.edit({ embeds: [embed] });
    if (timeRemaining <= 0 && i <= 0) {
        console.log('Done!');
        chosenReaction = Math.ceil(Math.random() * reactionUsers.length);
        embed = EmbedBuilder.from(embed).addFields({ name: 'Winner', value: `${reactionUsers[chosenReaction - 1]}` });
        giveawayMessage.edit({ embeds: [embed] });
    }

}

I tried making it choose on every iteration, expecting that the issue was that it stopped iterating before setting the variable, but it did not cause a noticable difference.




Aucun commentaire:

Enregistrer un commentaire