javascript - 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
}
}
});
这是结果:截图
解决方案
该事件message
会在所有消息上触发,即使是机器人发送的消息:
每当创建消息时发出。
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-message
因此,您通过发送消息不断触发事件。
解决方案是始终检查作者是否与机器人本身不同(bot-user 属性为bot.user
:https ://discord.js.org/#/docs/main/stable/class/Client?scrollTo=用户)
推荐阅读
- angular - Angular Flex 布局 - 简单布局(页眉、容器、页脚)
- menu - Jira 用户(Core 和 Confluence)的管理员权限
- reactjs - 如何判断 React 中哪些 props 来自哪个库?
- visual-studio-code - Visual Studio Code 中取消注释的快捷方式是什么?
- html - 列在小屏幕上的宽度不正确
- google-sheets - 调试 ImportHTML
- c# - 安装程序有回调时 Moq 抛出 NullReferenceException 异常
- objective-c - NSMutableArrays 和循环中的内存
- pip - pip install 依赖安装旧版本的库
- hadoop - Cloudera NODE_MANAGER_UNEXPECTED_EXITS 每小时