简体   繁体   中英

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 . However, my bot does not send anything when I send "$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.

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.

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');
            }
        } 
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM