Discord bot still answering multiple times on one event

倾然丶 夕夏残阳落幕 提交于 2020-01-06 14:33:30

问题


Already found same issue, but there are no answer there :C

So my problem is same, using Discord.js lib, and this is my code:

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

And here is result: Screenshot


回答1:


The event message is triggered on all messages, even the ones the bot sends :

Emitted whenever a message is created.

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

So you are continually triggering the event by sending a message.

The solution is to always check for the author, if it is different than the bot itself (the bot-user property is bot.user : https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=user)



来源:https://stackoverflow.com/questions/51064042/discord-bot-still-answering-multiple-times-on-one-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!