【问题标题】:Issues with Discord.js Number Guessing GameDiscord.js 猜数字游戏的问题
【发布时间】:2021-05-29 00:54:04
【问题描述】:

所以我正在尝试在 Discord.js 中制作一个基本的数字猜谜游戏。你知道,机器人选择一个随机数 1-100,你猜它,机器人会告诉你你是太高还是太低。由于我是编码初学者,因此我在网上查看了一些示例代码,并尝试对其进行一些调整以适应我的机器人设置(是的,我要归功于原始制造商)。这是我的命令处理程序,我之前已经成功地与其他命令一起使用过:

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'guess') {
        client.commands.get('guess').execute(message, args);
    }
    });

这是我guess.js文件中的代码:

module.exports = {
    name: 'guess',
    description: "guess a number 1-100",
    execute(message, args) {
        var mes = message.content.split(" ");
        message.reply('Picking a random number between 1 and 100');
        num = Math.floor((Math.random() * 100) + 1);
        guesses = 0;

        if (mes[0] == '!guess') {
            if (num == 0) 
            {
                message.reply('Picking a random number between 1 and 100');
                num = Math.floor((Math.random() * 100) + 1);
                guesses = 0;
            }
            else if (mes[1] == num) 
            {
                guesses++;
                message.reply('You got it! Only took ' + guesses + ' tries.');
                message.reply('Picking a random number between 1 and 100');
                num = Math.floor((Math.random() * 100) + 1);
                guesses = 0;
            }
            else if (mes[1] < num) {
                message.reply(mes[1] + ' is too low');
                guesses++;
            }
            else if (mes[1] > num) {
                message.reply(mes[1] + ' is too high');
                guesses++;
            }
        }
    }
}

是的。基本上应该发生的是,当你运行 ?pick 时,机器人会选择 1-100 的数字——开始游戏。然后,您通过运行 ?guess (number) 来猜测数字。例如,要猜测 50,您将运行 ?guess 50。代码中没有错误。但是,当我尝试运行 ?pick 命令时,什么也没有发生。当我运行 ?guess 或 ?guess (number) 时,机器人会响应“选择 1 到 100 之间的随机数”。谁能告诉我问题是什么?谢谢!

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    此代码添加检查消息内容是否以!guess 开头,但您使用的是?guess。您需要创建一个当前正在猜测的用户数组,并将if (... === "!guess") 更改为if (usersGuessing.includes(message.author.id))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 2020-11-22
      • 2013-10-20
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多