【问题标题】:My bot is responding to all prefixes and I don't know why我的机器人正在响应所有前缀,我不知道为什么
【发布时间】:2021-11-12 21:34:36
【问题描述】:

我正在使用最新的 node.js 和 discord.js 版本,并且我正在 VSC 上进行编码。 我的机器人开始响应所有前缀,我不知道为什么。我目前拥有的代码:

const Discord = require('discord.js');
const client = new Discord.Client();
 
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)
}

const PREFIX = '$';
 
 
client.on('ready', () => {
    console.log('updated');
})

client.on('message', message => {
 
    const args = message.content.substring(PREFIX.length).split(" ");

    switch (args[0]) {
        case 'ping':
            message.channel.send('pong!');
            break;
    }
});
 
        client.login(token);

但是,考虑到这段代码,机器人仍然会响应任何前缀。在下图中,我同时发送了带有各种随机字母和符号的 ping 命令。机器人回复了所有这些人。

不知道出了什么问题,因为我还是 discord.js 的新手,但我无法弄清楚。

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    也许你可以先检查一下它是否是一个命令。

    client.on('message', message => {
      
      const isCommand = message.content.substring(0,PREFIX.length) === PREFIX
      if(isCommand){
        const args = message.content.substring(PREFIX.length).split(" ");
        switch (args[0]) {
            case 'ping':
                message.channel.send('pong!');
                break;
        }
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 2020-07-03
      • 2021-06-15
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多