Discord.js |如何读取消息中的数字?

Discord.js | How to read a number in a message?

我正在尝试为我的 discord 机器人制作游戏:机器人选择一个随机数,然后用户尝试在机器人的指导下找到数字。我试过这个东西但是当我发送一条消息(我发送一个数字)时它变成了 NaN。为什么会发生这种情况,我该如何解决?

        let randomNumber = (Math.floor( Math.random() * (100)))
        let usersAnswer = parseInt(message.content)

        if (!message.author.bot && message.content === "!pick number"){
                message.reply("I kept! :check: ").then(j => {
                    message.channel.awaitMessages(( message, user) => (user.id === message.author.id && randomNumber < usersAnswer || randomNumber > usersAnswer),
                        {max: 1, time: 10000}).then(j => {
                        if (usersAnswer < randomNumber) {
                            message.reply("Increase! :arrow_up:" + usersAnswer) //I add usersAnswer to the bot's message because I want to see what it reads my message like in Discord
                        }
                        else {
                            message.reply("Decrease! :arrow_down:" + usersAnswer)
                        }
                    })
            })
        }

如果你使用parseInt()你需要在使用这个方法之前设置一个基数。我以10为例

let usersAnswer = parseInt(message.content, 10)

        if (!message.author.bot && message.content === "!pick number"){
                message.reply("I kept! :check: ").then(j => {
                    message.channel.awaitMessages(( message, user) => (user.id === message.author.id && randomNumber < usersAnswer || randomNumber > usersAnswer),
                        {max: 1, time: 10000}).then(j => {
                        if (usersAnswer < randomNumber) {
                            message.reply("Increase! :arrow_up:" + usersAnswer) //I add usersAnswer to the bot's message because I want to see what it reads my message like in Discord
                        }
                        else {
                            message.reply("Decrease! :arrow_down:" + usersAnswer)
                        }
                    })
            })
        }

或者您也可以简单地使用 Number():

let usersAnswer = Number(message.content)

        if (!message.author.bot && message.content === "!pick number"){
                message.reply("I kept! :check: ").then(j => {
                    message.channel.awaitMessages(( message, user) => (user.id === message.author.id && randomNumber < usersAnswer || randomNumber > usersAnswer),
                        {max: 1, time: 10000}).then(j => {
                        if (usersAnswer < randomNumber) {
                            message.reply("Increase! :arrow_up:" + usersAnswer) //I add usersAnswer to the bot's message because I want to see what it reads my message like in Discord
                        }
                        else {
                            message.reply("Decrease! :arrow_down:" + usersAnswer)
                        }
                    })
            })
        }

如果我是你,我会检查 usersAnswer 是否为数字,否则输出也可能是 NaN。) 您只需添加以下行即可完成此操作:

if (isNaN(usersAnswer)) return message.reply('This is not a number');