我可能对此视而不见。但是,我正在开发一个DiscordJS机器人--它有一个记分系统,并且可以使用一个!给将点从用户A传递给用户B。
点传递本身很好,所以这不是重点。
问题是,如果我写:
!给@Username 100 --它按预期的方式将100传输到MENIX。
“从@MENIX发送100到@用户名的帐户”
现在,如果我写:呢?
!给@Username Dog -然后它返回;
将狗从@MENIX发送到@Usernames的帐户
在这里,如果用户输入的不是数字-字母、句子等应该返回错误,我希望打印一个错误(Ea,你不能狗!给@用户名狗)
当前的完整脚本如下所示
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),但是我找不到合适的答案来回答我自己的问题。
我希望任何人都有关于如何只允许数字输入的想法,如果用户写了一个单词/句子-它将返回错误消息,输入是无效的。
发布于 2019-12-16 16:28:50
isNaN()函数。
isNaN() --表示“不是数字”,如果变量不是数字,则返回true,否则返回false。
if(isNaN(points)) return message.reply("Error");https://stackoverflow.com/questions/59360139
复制相似问题