Discord 机器人仍然在一个事件中多次回答

Discord bot still answering multiple times on one event

已找到 same issue,但那里没有答案 :C

所以我的问题是一样的,使用 Discord.js 库,这是我的代码:

 client.on('message', msg => {
     var splittedMessage = msg.content.split("#");
     if (msg.channel.type == "dm") {
         if (msg.content === "booya") {
             msg.channel.send('Hello there, ' + msg.author.username)
                 .then(msg => console.log('Sent #' + msg.id + ': ' + msg.content))
                 .catch(console.error);
             return
         } else {
             msg.channel.send('No query found')
                 .then(msg => console.log('Sent #' + msg.id + ': ' + msg.content))
                 .catch(console.error);
             return
         }
     }
 });

结果如下:Screenshot

事件 message 在所有消息上触发,甚至是机器人发送的消息:

Emitted whenever a message is created.

https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-message

所以你通过发送消息不断触发事件。

解决方案是始终检查作者是否与机器人本身不同(机器人用户 属性 是 bot.user : https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=user

首先,如果您使用的是 Visual Studio 代码,您可以 运行 同一 program/bot 的多个实例。 Visual Studio 代码中的终端:

所以每次编辑然后启动脚本时,它都会创建一个 "new" 终端(所以是另一个实例)。这些实例所在的位置:

然后在所有实例上按回收按钮(在下拉菜单旁边)。然后再次启动脚本 - 应该没问题。