首页 > 解决方案 > Discord 机器人仍然在一个事件上多次回答

问题描述

已经发现同样的问题,但那里没有答案:C

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

 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
         }
     }
 });

这是结果:截图

标签: javascriptnode.jsdiscord.js

解决方案


该事件message会在所有消息上触发,即使是机器人发送的消息:

每当创建消息时发出。

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

因此,您通过发送消息不断触发事件。

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


推荐阅读