discord.js bot math not working right Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of discord.js bot math not working right without wasting too much if your time.

The question is published on by Tutorial Guruji team.

So with my bot when someone purges a channel I want to add one to the purge amount they have then split the first value off so it doesn’t purge their command, However when I try to add 1 to the value of the argument passed it just puts the number behind the value… Any help, I’ really crappy at JS so ye…

if (args[1]){
    var messagecount = args[1];
    var messagetotal = Math.floor(messagecount + 1); // Adding 1 to passed argument
    console.log(messagetotal); // Outputting to console, Console is reading the argument with 1 on the end EX: arg: 2 | output: 21
//  message.channel.fetchMessages({limit: messagecount}).then(messages => message.channel.bulkDelete(messages).then(messages => message.channel.send("Purged `" + messages.size + "` messages.")));
} else {
//  message.channel.send("Usage: !purge (Number)");
}

Answer

I don’t know that the exact content of args[1] is, but regarding your explanation of the bug, it seems that the value is a number formatted as a string.

The difference:

String

args[1] = "1";
console.log(args[1] + 1);

Output: "11"

Integer

args[1] = 1;
console.log(args[1] + 1);

Output: 2

If you add a number to a string, it will be appended and not added mathematically.

To format your numeric string to an integer, replace

var messagecount = args[1];

through

var messagecount = parseInt(args[1]);

We are here to answer your question about discord.js bot math not working right - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji