[英]How can I make an even-odd command for my Discord.js bot?
I currently am making a discord bot.我目前正在制作一个不和谐的机器人。 I'm trying to make a command that determines whether a number is even or odd,
evenodd
.我正在尝试创建一个命令来确定一个数字是偶数还是奇数,
evenodd
。 However, my bot does not send anything when I send "$betterfly evenodd 'number'".但是,当我发送“$betterfly evenodd 'number'”时,我的机器人不发送任何内容。
Here is my code:这是我的代码:
if (msg.content === '$betterfly evenodd') {
if (msg.content.endsWith("0") || msg.content.endsWith("2") || msg.content.endsWith("4") || msg.content.endsWith("6") || msg.content.endsWith("8")) {
msg.channel.send('Even');
} else {
msg.channel.send('Odd')
}
}
Well for starters use modulo '%' to get the remainder of the division by 2 if it's 0 then the number is even if that's not the case then it's odd.那么对于初学者来说,如果它是 0,那么使用模 '%' 来得到除以 2 的余数,那么这个数字是即使不是这样的,那么它是奇数。
The code above returns nothing because it checks whether the message that has been sent is equal to '$betterfly evenodd' or not and then checks again if it ends with numbers and it definitely doesn't so it doesn't return any messages.上面的代码什么都不返回,因为它检查已发送的消息是否等于 '$betterfly evenodd' ,然后再次检查它是否以数字结尾,并且肯定不是,所以它不会返回任何消息。
Do this instead改为这样做
client.on('message', message => {
var div_list = message.content.split(" ");
if (div_list.length == 3) {
if(div_list[0] == "$betterfly" && div_list[1] == 'evenodd')
{
var integer = parseInt(div_list[2], 10);
if (integer % 2) {
message.channel.send('Odd');
}
else {
message.channel.send('Even');
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.