Discord.js v12“猜数字”游戏

时间:2020-10-07 13:32:43

标签: javascript node.js discord.js

我正在尝试编写自己的数字猜谜游戏。到目前为止,这是我的代码:

const Discord = require('discord.js');
const chalk = require('chalk');
const consolewords = chalk.keyword('white');
const consolecommands = chalk.keyword('cyan');
const consoleerrcmds = chalk.keyword('yellow');

module.exports = {
    name: 'guess',
    description: 'Start the game.',
    guildOnly: true,
    cooldown: 5,
    execute(message) {

        if (message.member.roles.cache.has('737365762926182483')) {

            const guessNum = require('./guess.json');
            const item = guessNum[Math.floor(Math.random() * guessNum.length)];
            const filter = response => {
                return item.answers.some(answer => answer.toLowerCase() === response.content.toLowerCase());
            };
            const eventrole = message.channel.guild.roles.cache.get('734780649394929794');
            const chanellgamr = message.channel.guild.channels.cache.get('756254204800270346');

            console.log(consolewords('[CMD]: ') + consolecommands(`${message.author.username} started the guess-the-number game!`));
            message.delete();
            setTimeout(function() {
                message.channel.send(`Starting in **120** seconds, ${eventrole}!`);
            }, 10000);
            setTimeout(function() {
                message.channel.send('Starting in **60** seconds.');
            }, 60000);
            setTimeout(function() {
                message.channel.send('Starting in **30** seconds.');
            }, 90000);
            setTimeout(function() {
                message.channel.send('Starting in **10** seconds.');
            }, 110000);
            setTimeout(function() {
                message.channel.send('Starting in **5** seconds.');
            }, 115000);
            setTimeout(function() {
                message.channel.send('Starting in **3** seconds.');
            }, 117000);
            setTimeout(function() {
                message.channel.send('Starting in **2** seconds.');
            }, 118000);
            setTimeout(function() {
                message.channel.send('Starting in **1** seconds.');
            }, 119000);
            setTimeout(function() {
                const exampleEmbed = new Discord.MessageEmbed()
                    .setColor('DARK BLUE')
                    .setTitle('Guess the Number Started!')
                    .setDescription(`Guess the right number between **${item.question}** to win!`)
                    .setTimestamp()
                    .setFooter('PlanetMc Development');
                chanellgamr.send({ embed: exampleEmbed }).then(() => {
                    chanellgamr.awaitMessages(filter, { max: 1, time: 360000, errors: ['time'] })
                        .then(collected => {
                            const winnerEmbed = new Discord.MessageEmbed()
                                .setColor('DARK GREEN')
                                .setTitle('Guess the Number Ended!')
                                .setDescription(`**${collected.first().author.username}** won!`)
                                .setTimestamp()
                                .setFooter('PlanetMc Development');
                            chanellgamr.send({ embed: winnerEmbed }).then(winner => {
                                winner.react('?');
                            });
                            console.log(consolewords('[CMD]: ') + consolecommands(`${collected.first().author} won the guess-the-number game!`));
                        })
                    // eslint-disable-next-line no-unused-vars
                        .catch(collected => {
                            const looserEmbed = new Discord.MessageEmbed()
                                .setColor('DARK RED')
                                .setTitle('Guess the Number Ended!')
                                .setDescription('Noone guessed the right number after **10** minutes!')
                                .setTimestamp()
                                .setFooter('PlanetMc Development');
                            chanellgamr.send({ embed: looserEmbed });
                            console.log(consolewords('[CMD]: ') + consolecommands('Noone guessed the right number!'));
                        });
                });
            }, 120000);
        }
        if (!message.member.roles.cache.has('737365762926182483')) {
            message.channel.send('You don\'t have the permission to do that!');
            console.log(consolewords('[CMD]: ') + consoleerrcmds(`${message.author.username} tryed to start the Guess the number game but didn't have the permission!`));
        }
    },
};

这是我的guess.json文件:

[
    {
        "question": "1-2",
        "answers": ["2"]
    },
    {
        "question": "1-3",
        "answers": ["3"]
    }
]

问题是,如果我键入“ 3”(当第二个问题被选中时),则什么也不会发生。该漫游器仅会在10分钟后返回“猜测已结束的数字(没人猜到正确的数字)”嵌入。 我不确定为什么它不起作用! (没有错误!)

提前谢谢!

0 个答案:

没有答案