现在我正在开发一个小型滚动机器人,该机器人将允许用户发送“ Roll”,然后提示他们输入数字。它使用该数字,并在0到输入的数字之间选择一个随机数。现在,我试图让某人键入停止后而不是输入要输入的数字,而是说“滚动已停止”,但是它始终带有“ You Rolled NaN”或“请提供有效的数字!” 。如果输入了止损,有什么方法可以阻止它出现?
代码
const Discord = require('discord.js');
const client = new Discord.Client();
function getRandomInt(max){
return Math.floor(Math.random() * Math.floor(max));
}
const prefix = 'roll';
client.once('ready', () => {
console.log('your bot name is online!');
client.user.setActivity("Type 'Role Help'", {type: 3});
});
client.on('message', message =>{
if(!message.content.toLowerCase().startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === ''){
message.channel.send("What's the **Max** role number you want?");
message.channel.awaitMessages(m => m.author.id == message.author.id,
{max: 1, time: 10000}).then(collected => {
if(collected.first().content == 'stop' || 'Stop' || 'STOP'){
message.channel.send('*Roll has been stopped.*');
};
console.log('collected :' + collected.first().content);
if (isNaN(collected.first().content) != (collected.first().content == 'stop')){
return message.channel.send("Please provide a valid number!")};
var n = (getRandomInt(collected.first().content));
message.channel.send("You Rolled a " + (n + 1));
}).catch(() => {
message.channel.send("*Roll has been cancelled.*");
})} else if(command === 'Help' || 'help'){
message.channel.send("**How to use Role Bot**");
message.channel.send("*1. Type Role to begin*");
message.channel.send("*2. Input a number or type stop to end the roll.*");
message.channel.send("**Have Fun!**");
}
});
答案 0 :(得分:1)
代码继续,因为如果收集了“停止”,则您从未实现文件的结尾。使用return
结束/退出功能。
在下面的行中,return
退出该函数,并将消息作为最后的输出发送。
提示:要检测大小写不同的字符串,只需执行.toLowerCase()
并将其与小写字符串进行比较,以便无论大写哪个字母都将返回true
< / p>
if (collected.first().content.toLowerCase() === 'stop') return message.channel.send('*Roll Has Been Stopped*');