如何阻止我的 Discord 机器人调用错误的命令?

问题描述

我一直在使用 JavaScript 编写一种计算器 discord bot,但我遇到了一个非常烦人的问题。我做了一个简单的 ping 命令,机器人回复“pong!”来自教程,因为这是我的第一个 discord 机器人。然后我创建了一个加法命令,然后是一个减法命令,它们都有效。但是,当我添加乘法命令时,一些奇怪的事情开始发生。当我在 discord 上测试乘法命令时,机器人接受了我的参数并表现得好像我已经要求它减去数字,甚至正确地使用嵌入进行减法。我等了几天但那没有用,所以我想也许是因为我从减法命令中复制粘贴了代码并将其更改为乘法是问题所在,所以我以相同的方式重写了代码删除了旧的乘法命令文件。那没有用。然后我发现,除了 ping 和加法命令之外,我输入的任何内容都会触发减法命令。我使用的前缀是“数学”。如果我输入“math.multiply”、“math.”或“math.dkjf2uhvni”,或者任何字面意思(当然有参数),机器人只会减去数字。我不知道这里发生了什么。我正在使用 discord.js 和 decimal.js 库。我用 Visual Studio Code 编写了我的机器人。抱歉,我的问题冗长,我只想确保任何回答的人都拥有提出解决方案所需的所有信息。如果有人能够提供帮助,我将不胜感激。我将在此处包含与我的机器人相关的所有代码以供参考:

//-------------
//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,discord);
    } else if(command === 'multiply' || 'mul'){
        client.commands.get('multiply').execute(message,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.',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.',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.',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);
    }
}

解决方法

在评论中,您看到 or 运算符不是这样工作的。这样做的原因是因为您告诉 JavaScript:“如果命令是减法,或 'sub' 返回 true,则运行此代码”,以及不为 0 的数字、不为空的字符串以及不为空的变量不是 undefined 或 null,当然也不是 false 本身,返回 true。记得像评论中看到的那样更改它

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

另一种方法是使用数组:

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