Discord.js赠品命令

问题描述 投票:0回答:1

我试图使赠送的命令适应我的discord.js机器人,但是代码出现错误。

这是代码

const {RichEmbed, Collection} = require("discord.js");
class Giveaway {
    constructor(client, message, match, data) {
        const [full, channel, winnerCount, timeInSeconds, days, hours, minutes, seconds, title] = match;
        this.giveawayCache = new Collection();
        this.lastGiveawayCache = new Collection();
        this.client = client;

        this.message = message;

        this.channel = data ? this.client.guilds.get(data.guild).channels.get(data.channel) : message.guild.channels.get(channel) || message.channel;

        this.winnerCount = winnerCount || 1;

        this.length = timeInSeconds ? timeInSeconds : (60 * 60 * 24 * (days ? Number(days) : 0)) + (60 * 60 * (hours ? Number(hours) : 0)) + (60 * (minutes ? Number(minutes) : 0)) + (seconds ? Number(seconds) : 0);

        this.remaining = data ? data.remaining : this.length;

        this.endTime = Date.now() + this.length * 1000;

        this.title = title;

        this.winners = [];

        this.msg;

        this.interval;

        this.suffix = this.giveawayCache.filter(giveaway => giveaway.channel.id === this.channel.id).size+1;

        this.match = match;

        this.valid = true;

        if (data) { this.continue(data); } else { this.init(); }
    }

    async init() {
        this.msg = new RichEmbed()
            .setColor(0xcc8822)
            .setTitle(this.title)
            .setDescription(`Nombre de gagnants : ${this.winnerCount}\nRéagis avec :tada: pour participer !`)
            .setFooter(`${this.suffix} | Termine à`)
            .setTimestamp(new Date(this.endTime));
            this.channel.send(this.msg)
                .then(async function (message) {
                    await message.react('🎉')
                });
        this.interval = setInterval(() => this.intervalFunction(), 1000);

        this.giveawayCache.set(`${this.channel.id}-${this.suffix}`, this);
    }

    async continue(data) {
        this.msg = await this.client.channels.get(data.channel).messages.fetch(data.msg).catch(() => {
            clearInterval(this.interval);
            this.lastGiveawayCache.set(`${this.channel.id}-${this.suffix}`, this);
            this.giveawayCache.delete(`${this.channel.id}-${this.suffix}`);
        });
        this.winnerCount = await data.winnerCount;
        this.title = await data.title;
        this.interval = setInterval(() => this.intervalFunction(), 1000);

        this.giveawayCache.set(`${this.channel.id}-${this.suffix}`, this);
    }

    async intervalFunction() {
        this.remaining--;

        this.channel.fetchMessages(this.msg.id).catch(() => {
            clearInterval(this.interval);
            this.lastGiveawayCache.set(`${this.channel.id}-${this.suffix}`, this);
            this.giveawayCache.delete(`${this.channel.id}-${this.suffix}`);
        });

        if(Date.now() >= this.endTime){
            clearInterval(this.interval);
            this.lastGiveawayCache.set(`${this.channel.id}-${this.suffix}`, this);
            this.giveawayCache.delete(`${this.channel.id}-${this.suffix}`);

            const embed = this.msg.embeds[0];
            embed.color = 0x171c23;

            const users = await this.msg.reactions.get("🎉").users.fetch();
            const list = users.array().filter(u => u.id !== this.msg.author.id);

            if (!list.length) {
                embed.description = `Gagnant : Personne.`;
                embed.footer.text = `Giveaway terminé !`;

                return this.msg.edit({ embed });
            }

            for (let i = 0; i < this.winnerCount; i++) {
                const x = this.draw(list);

                if (!this.winners.includes(x)) this.winners.push(x);
            }

            embed.description = `Gagnant : ${this.winners.filter(u => u !== undefined && u !== null).map(u => u.toString()).join(", ")}`;
            embed.footer.text = `${this.suffix} | Giveaway terminé !`;

            this.msg.edit({ embed });

            if (this.winners.length) this.channel.send(`Bravo, ${this.winners.map(u => u.toString()).join(", ")}! Tu as gagné le giveaway pour **${this.title}**!`);
        }

    }

    shuffle(arr) {
        for (let i = arr.length; i; i--) {
            const j = Math.floor(Math.random() * i);
            [arr[i - 1], arr[j]] = [arr[j], arr[i - 1]];
        }
        return arr;
    }

    draw(list) {
        const shuffled = this.shuffle(list);
        return shuffled[Math.floor(Math.random() * shuffled.length)];
    }
}

module.exports = Giveaway;

这是错误:

unhandledRejection错误:TypeError:无法读取未定义的属性'0'

在Giveaway.intervalFunction(/home/mef/structure/giveaway.js:82:40)

超时。 (/home/mef/structure/giveaway.js:50:48)

在listOnTimeout(internal / timers.js:531:17)

在processTimers(internal / timers.js:475:7)

问题所在的行是:const embed = this.msg.embeds [0];但我不知道该如何解决。

感谢您的帮助

javascript command bots discord discord.js
1个回答
0
投票

我建议您安装此软件包,它可以完成所有工作并节省大量代码行。

https://yarnpkg.com/en/package/discord.js-giveaway

© www.soinside.com 2019 - 2024. All rights reserved.