Discord.js bot两次执行所有操作

时间:2020-05-23 04:06:28

标签: node.js discord.js

我正在为我的私有不和谐服务器对该机器人进行编码,并且我发现了这个错误/故障,导致该机器人两次执行了所有操作。在我的控制台中,它还会记录两次,两次回答。这是我的代码:

(我不知道这是否重要,但是您可以看到最后两个命令不存在,因为我当前正在制作命令处理程序,并且我只转换了一些命令)。

更新:第二天,现在它可以做三遍了... 更新#2:现在,当我执行命令时,它会回答另一个命令...请帮助...

您知道是什么原因/如何解决?

const bot = new Discord.Client();
const token = '---my token---';
var PREFIX = 'bb!';
var member



const fs = require('fs');
bot.commands = new Discord.Collection();

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

bot.on('ready', () =>{
    console.log('Discord Bot Is Online.');
    bot.user.setActivity('In BitBots FunHouse', { type: 'PLAYING'}).catch(console.error);
})


bot.on('message', msg=>{


    var args = msg.content.substring(PREFIX.length).split(" ");
    switch(args[0]){
        case 'Test':
            msg.channel.send('Bot Online.');
        break;





       case 'Help':
        const exampleEmbed = new Discord.MessageEmbed()
            .setColor('#FF8C00')
            .setTitle('BitBot Help Menu')
            .setAuthor('MrFishy', )
            .setDescription('BitBot Help & Commands Menu')
            .addFields(
                { name: 'Test', value: 'More test' },
                  { name: '\u200B', value: '\u200B' },
                  { name: 'Inline field title', value: 'Some value here', inline: true },
                  { name: 'Inline field title', value: 'Some value here', inline: true },
              )
              .addField('Test subtitle', 'Test value', true)
              .setTimestamp()
             .setFooter('All commands start with bb!', 'https://i.imgur.com/Naujnff.png');

        msg.channel.send(exampleEmbed);
        break;


       case 'Delete':
        if(!msg.member.hasPermission("MANAGE_MESSAGES")) return msg.reply("You cannot use this command since you do not have a role with `MANAGE_MESSAGES` enabled.");
        if(!args[1]) return msg.reply("Please specify the number of messages you want to delete.");
        if(parseInt(args[1]) > 100) return msg.reply("You cannot delete more than 100 messages at once!");

        msg.channel.bulkDelete(parseInt(args[1]) + 1).then(() => {
            msg.channel.send(`Deleted ${args[1]} messages!`).then(msg.channel.delete);
        }).catch((err) => {
            return msg.reply("An error occured!")
        })
        break;

        case '8ball':
            bot.commands.get('8ball').execute(msg, args);
        break;

        case 'GetRekt':
                bot.commands.get('GetRekt').execute(msg, args);
        break;    

    }


}
)
bot.login(token);




}```

Code in one of the different command files (Just in case it helps):

```module.exports = {
    name: 'GetRekt'
    ,description: "Sends the GetRekt image.",
    execute(message, args){
        message.channel.send('Get rekt, bro. https://i.imgur.com/3qvm3d0.png');

    }
}```


Sorry if that's too much to read through, I just did not finish moving all the commands to separate files.

3 个答案:

答案 0 :(得分:0)

通过添加来确保您的机器人没有对自己的消息做出反应

if (message.author.bot) return;

首先进入bot.on(“ message”)

答案 1 :(得分:0)

更新: 我已经找到问题的原因并解决了。 原来,我错误地使了命令处理程序并使它多次运行我的机器人。我已使用D.JS官方网站上的this指南对其进行了修复。

答案 2 :(得分:0)

我知道这个线程很旧,但是我刚遇到这个问题,我意识到我在vscode中运行多个终端。我可能是唯一一个足以犯此错误的笨蛋,但以防万一。