Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

.array is not a function #8

Open
anandMohanan opened this issue Aug 16, 2021 · 2 comments
Open

.array is not a function #8

anandMohanan opened this issue Aug 16, 2021 · 2 comments

Comments

@anandMohanan
Copy link

anandMohanan commented Aug 16, 2021

node_modules/discord-giveaway/src/functions.js:37
const entries = users.filter(user => !user.bot).array();

TypeError: users.filter(...).array is not a function

@Nguyenwasd72
Copy link

i may fix the .array is not a function but the user that the bot pinged is now [object Map]

@Nguyenwasd72
Copy link

ok finally fixed it
here is function.js

const scheduler = require('node-schedule');
const GiveawayModel = require('../models/GiveawayModel');

function getWinner(users, max) {
    if (users.length < 1) return false;
    if (users.length <= max) return users;

    const numbers = new Set();
    const array = [];
    let i = 0;
    while(i < max) {
        const random = Math.floor(Math.random() * users.length);

        const selected = users[random];
        if (!numbers.has(random)) {
            array.push(selected);
            i++
        }
    }
    return array;
}

async function schedule(stuff, giveawayArray) {
    for(let i = 0; i < giveawayArray.length; i++) {
        let { messageId, channelId, endsOn, prize, winners } = giveawayArray[i];

        scheduler.scheduleJob(`${messageId}`, endsOn, async () => {
            const channel = stuff.client.channels.cache.get(channelId);

            if (channel) {
                const message = await channel.messages.fetch(messageId);
                
                if (message) {
                    const { embeds, reactions } = message;
                    const reaction = reactions.cache.get('🎉');
                    const users = await reaction.users.fetch();
                    const entries = Array.from(users.filter((user) => !user.bot).keys());

                    if (embeds.length === 1) {
                        const embed = embeds[0];
                        const winner = getWinner(entries, winners);
                        let finalWinners;
                        if (!winner) {
                            finalWinners = 'Nobody Reacted';
                        }
                        else {
                            finalWinners = winner.map(user => user.toString()).join(', ');
                        }
                        embed.setDescription(`Winner(s): <@${finalWinners}>`);
                        embed.setFooter({ text: "Giveaway Ended" });
                        await message.edit({ embeds: [embed] });
                        if (!winner) {
                            message.channel.send(`Nobody reacted to the **${prize}** giveaway\n${message.url}`);
                        }
                        else {
                            message.channel.send(`Congratulations <@${finalWinners}>, you won the **${prize}**!\n${message.url}`);
                        }
                        const ended = await endGiveaway(messageId);
                        stuff.emit('giveawayEnd', ended);
                    }
                }
            }
        });
    }
}

async function endGiveaway(messageId) {
    let data = await GiveawayModel.findOne({ messageId: messageId });

    data.hasEnded = 'True';

    await data.save();

    return data;
}

module.exports = {
    getWinner,
    schedule,
    endGiveaway
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants