如何让我的机器人在初始响应后响应某人?

时间:2021-04-25 22:24:04

标签: javascript discord.js

我正在创建一个机器人命令,所以当你输入 Nco.ncks(input='input_file.nc', output='output_file.nc', options=['-7 -L 1']) 时机器人会说 ,num-guess 这有效,但它也紧接着说 '[username] guess a number between 1 and 10, you have 3 guesses.' 3 次。相反,我想要它,所以当你猜错时它会说 '[username] you have not correctly guessed it, you have 3 guesses left' 然后剩下 1 次猜测,然后再猜错 1 次它会说 '[username] you have not correctly guessed it, you have 2 guesses left,',但它不起作用

这是我的代码:

'Sorry [username] you did not guesses they number'

这是机器人说的:

module.exports = {
    name: 'num-guess',
    description: 'User has to guess a number between 1 - 10',
    async execute(Client, message, args, Discord) {
        NumberToChoose = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        const Number = NumberToChoose[Math.floor(Math.random() * NumberToChoose.length)];
        message.channel.send(`${message.author.username} guess a number from 1 - 10`);

        var MaxGuess = 3
        var CorrectAnswer = Number
        var GuessesLeft = MaxGuess
        var Guess = message.author.message

        for (i=0; i<MaxGuess; i++){
            if(CorrectAnswer == Guess){
                message.channel.send(`${message.author.username} you have guessed the number correctly`);
                break;
            }
            else {
                var GuessesLeft = GuessesLeft--
                message.channel.send(`${message.author.username} you have not correctly guessed it, you have ${GuessesLeft} left`)
            }
        }
    }
}

我想说的是:

[username] guess a number from 1 - 10
[username] you have not correctly guessed it, you have 3 left
[username] you have not correctly guessed it, you have 3 left
[username] you have not correctly guessed it, you have 3 left

1 个答案:

答案 0 :(得分:0)

postfix decrement operator (n--) 有一些令人困惑的功能,这使它比仅使用 subtraction assignment operator (n -= x) 更令人困惑。使用此运算符时,它将增加变量,但返回原始值。举个例子:

{
    "private": true,
    "scripts": {
        "dev": "mix",
        "development": "mix",
        "watch": "mix watch",
        "watch-poll": "mix watch -- --watch-options-poll=1000",
        "hot": "mix watch --hot",
        "prod": "npm run production",
        "production": "mix --production"
    },
    "devDependencies": {
        "axios": "^0.21.1",
        "css-loader": "^5.2.1",
        "laravel-mix": "^6.0.16",
        "lodash": "^4.17.21",
        "postcss": "^8.2.10",
        "resolve-url-loader": "^3.1.2",
        "sass": "^1.32.10",
        "sass-loader": "^11.0.1"
    },
    "dependencies": {
        "autoprefixer": "^10.2.5",
        "bootstrap": "^4.5.3",
        "bootstrap-datetimepicker": "0.0.7",
        "bootstrap-select": "^1.13.18",
        "chartjs": "^0.3.24",
        "datatables.net": "^1.10.24",
        "font-awesome": "^4.7.0",
        "fullcalendar-scheduler": "5.6.0",
        "jquery": "^3.6.0",
        "moment": "^2.29.1",
        "popper.js": "^1.16.1",
        "style-loader": "^2.0.0",
        "waypoints": "^4.0.1",
        "webpack": "^5.35.0",
        "webpack-cli": "^4.6.0"
    }
}

由于您将 let number = 1; const increment = number--; // number was decremented correctly and is now zero console.log(`Number: ${number}`); // but increment takes the value of the original number console.log(`Increment: ${increment}`); 分配给变量并使用它,因此数字实际上不会改变,因为它每次都使用原始值。有很多方法可以防止这种情况:

  • 目前最简单的方法就是不要将 n-- 重新分配给另一个同名变量。此运算符将就地编辑值,因此除了使用运算符之外无需执行任何操作。
GuessesLeft
  • 使用pre修复递减运算符 (// original: // var GuessesLeft = GuessesLeft-- GuessesLeft-- )。它做同样的事情,但这次实际上返回了修改后的数字。

  • 使用减法赋值运算符。 --n。它可能不是那么简短和甜蜜,但它更容易理解,并且没有任何令人困惑的“后”/“前”曲线球生效。


此外,我很确定您永远无法正确猜出数字。 GuessesLeft -= 1 不是一个东西,所以 message.author.message 将是未定义的,而且它看起来并没有真正提示您提供进一步的答案。