javascript - JS 命令不允许字母,只允许数字(disc bot)

标签 javascript bots discord.js

我可能对此视而不见。但是,我正在开发一个 DiscordJS 机器人 - 它有一个积分系统,可以使用 !give 将积分从用户 A 传递给用户 B

通过点本身很好,所以这不是这里的重点。


问题是,如果我写:

!give @Username 100 - it does as expected, it transfers 100 to MENIX.

“从@MENIX 向@Usernames 的帐户发送了 100”


那么现在,如果我写:

!give @Username Dog - Then it returns;

“将狗从@MENIX 发送到@Usernames 的帐户”


情况在这里,如果用户输入数字以外的任何内容,我想要打印一个错误 - 字母、句子等应该返回错误(Ea,你不能狗!给@username dog)


当前完整脚本如下

const Discord = require("discord.js");
const functions = require('.././functions.js')
module.exports.run = async (bot, message, args, con, canUseCommand, config) => {

    if (!message.member.roles.some(r => ["Admin"].includes(r.name)))
        return message.reply("Sorry, you don't have permissions to use this!");
    let target = message.mentions.users.first()
    let user = message.author
    let points = args[1]

    if (args[1]) {
            await con.query(`SELECT * FROM pts WHERE id = '${message.author.id}'`, async (err, rows) => {
                functions.addCounter();
                let points2 = Math.round(rows[0].points);

                if (!target) {
        return message.channel.send(`Couldn't find user. Please remember to @ Tag the user & Try again!`)
    }

     if (!args[0]) {
        return message.channel.send(`Woops, looks like you forgot to specify an amount to give`)
    }

     if (message.content.includes('-')) { //tries to remove negative point giving
        return message.channel.send(`Negative points can not be given.`)
    }

        if (points2 < points) {
        return message.channel.send(`Oops trying to give too much points ${message.author}. You currently have :moneybag: ${functions.formatNumber(points2)} .`)
    }

    await functions.removeMoney(user, points,"remove-points", con)
    await functions.addMoney(target, points,"addpoints", con)

    message.channel.send(`Sent ${points} from ${message.author} to ${target.toString()}'s account`)



    })

}
}

module.exports.help = {
    name: "give"
}

我尝试使用 (message.content.includes),但无法为我自己的问题找到合适的答案。

我希望任何人都知道如何只允许输入数字,如果用户写了一个词/句子 - 它会返回一条错误消息,表明输入无效。

最佳答案

isNaN() 函数。

isNaN() – 代表“不是数字”,如果变量不是数字,则返回 true,否则返回 false。

 if(isNaN(points)) return message.reply("Error");

关于javascript - JS 命令不允许字母,只允许数字(disc bot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59360139/

相关文章:

javascript - 无法在 DiscordJS 中通过不同作用域传递变量

javascript - 如何禁用多个滚动功能?

javascript - 未从 Angular Directive(指令)触发输入的更改事件

javascript - 使用选择框编辑表格

javascript - 如何在不和谐机器人上发出命令来随机生成报价?

node.js - guildScheduledEventCreate 的问题discordjs

javascript - 检查提到的个人资料是否是机器人

javascript - jQuery Mobile 嵌套 ListView 中的奇怪行为

python - 为什么我安装的软件包无法被识别

c# - 在更大的位图中查找位图(Pixel bot)-如何使其/for循环更快?