Discord bot still answering multiple times on one

2019-08-26 16:33发布

问题:

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)