Discord bot仍然在一个事件上回答多次 [英] Discord bot still answering multiple times on one event

查看:124
本文介绍了Discord bot仍然在一个事件上回答多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经发现相同的问题,但是没有在那里回答:C

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

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

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

结果如下:屏幕截图

推荐答案

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

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.

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

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)

这篇关于Discord bot仍然在一个事件上回答多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆