Search code examples
javascriptnode.jsdiscorddiscord.jsdecimal.js

How do I stop my Discord bot from calling the wrong command?


I've been coding a sort of calculator Discord bot using JavaScript, but I've encountered a very annoying problem. I made a simple ping command that the bot responded with "pong!" from a tutorial, as this is my first Discord bot. I then created an addition command and then a subtraction command, which both worked. However, when I added a multiplication command, some weird things began to happen. When I tested the multiplication command on Discord, the bot took my arguments and acted as if I had asked it to subtract the numbers, even correctly using the embed for subtraction. I waited a couple of days but that didn't work, so I thought maybe the fact that I had copy-pasted the code from the subtraction command and altered it to multiplication was the problem, so I rewrote the code the same way it was and deleted the old multiplication command file. That didn't work. I then found out that literally anything I entered other than the ping and addition commands would trigger the subtraction command. The prefix I'm using is "math." and if I entered "math.multiply," "math.," or "math.dkjf2uhvni," or literally anything (with the arguments of course) the bot would just subtract the numbers. I have no idea what's going on here. I'm using the discord.js and decimal.js libraries. I wrote my bot in Visual Studio Code. Sorry for my long-winded question, I just want to make sure anyone who answers has all the information needed to come up with a solution. If anyone is able to help, I'd greatly appreciate it. I'll include all of the code related to my bot right here for reference:

//-------------
//package.json
//-------------
{
  "name": "math-bot",
  "version": "1.0.0",
  "description": "A bot that does math (and maybe explains it too idk)",
  "main": "mathbot.js",
  "scripts": {
    "test": "echo"
  },
  "author": "JustAnotherMusician7",
  "license": "ISC",
  "dependencies": {
    "decimal.js": "^10.2.1",
    "discord.js": "^12.5.3"
  }
}
//------------------------------------------------------------------------------------------------------
//mathbot.js (detects and calls the commands which are separate files, logs the bot into Discord, etc.)
//------------------------------------------------------------------------------------------------------
const decimal = require('decimal.js');
const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = 'math.';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles) {
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Math Bot is online!');
});

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'ping'){
        client.commands.get('ping').execute(message, args);
    } else if(command === 'add'){
        client.commands.get('add').execute(message, args, Discord);
    } else if(command === 'subtract' || 'sub'){
        client.commands.get('subtract').execute(message, args, Discord);
    } else if(command === 'multiply' || 'mul'){
        client.commands.get('multiply').execute(message, args, Discord);
    } else {
        return;
    }
});

client.login('REAL-TOK3N');
//-----------------------------------------------------------------------------
//ping.js (simple ping command from the tutorial I used, works perfectly fine)
//-----------------------------------------------------------------------------
module.exports = {
    name: 'ping',
    description: "This is a ping command.",
    execute(message, args){
        message.channel.send('pong!');
    }
}
//--------------------------------------------------
//add.js (addition command, also works as intended)
//--------------------------------------------------
const { default: Decimal } = require('decimal.js');

module.exports = {
    name: 'add',
    description: 'This command can add two numbers.',
    execute(message, args, Discord){
        x = new Decimal(args[0]);
        y = new Decimal(args[1]);
        var sum = Decimal.add(x, y);
        const embed = new Discord.MessageEmbed()
        .setColor('#ffffff')
        .setTitle('Addition')
        .addFields(
            {name: String(args[0]) + ' + ' + String(args[1]), value: String(sum)}
        )
        message.channel.send(embed);
    }
}
//-------------------------------------------------------------------------------------------------------
//subtract.js (the command that has gone haywire and is blocking my multiplication command from working)
//-------------------------------------------------------------------------------------------------------
const { default: Decimal } = require('decimal.js');

module.exports = {
    name: 'subtract',
    description: 'This command can subtract two numbers.',
    execute(message, args, Discord){
        x = new Decimal(args[0]);
        y = new Decimal(args[1]);
        var difference = Decimal.sub(x, y);
        const embed = new Discord.MessageEmbed()
        .setColor('#ffffff')
        .setTitle('Subtraction')
        .addFields(
            {name: String(args[0]) + ' - ' + String(args[1]), value: String(difference)}
        )
        message.channel.send(embed);
    }
}
//------------------------------------------------------------------------------------------------
//multiply.js (the command that is seemingly overridden by the subtraction command, doesn't work)
//------------------------------------------------------------------------------------------------
const { default: Decimal } = require('decimal.js');

module.exports = {
    name: 'multiply',
    description: 'This command can multiply two numbers.',
    execute(message, args, Discord){
        x = new Decimal(args[0]);
        y = new Decimal(args[1]);
        var product = Decimal.mul(x, y);
        const embed = new Discord.MessageEmbed()
        .setColor('#ffffff')
        .setTitle('Multiplication')
        .addFields(
            {name: String(args[0]) + ' * ' + String(args[1]), value: String(product)}
        )
        message.channel.send(embed);
    }
}

Solution

  • In the comments you saw that that was not how the or operator works. The reason it does that is because you are telling JavaScript this: "if the command is subtract, or 'sub' returns true, run this code", and numbers that aren’t 0, strings that aren’t empty, and variables that aren’t undefined or null, and of course not false itself, return true. Remember to change it like you see in the comments

    if(command === 'subtract' || command === 'sub') {
    //...
    }
    

    Another way you can this is with array:

    if(['sub', 'subtract'].includes(command)) {
    //...
    }