javascript - 当提到特定用户时,如何让不和谐机器人发送消息?
问题描述
我对 javascript 还很陌生,并且一直在使用 Discord.js 制作一两个 Discord Bot。我正在开发一项功能,当我在我的不和谐服务器中被 ping 时发送消息。我已经尝试了几件事,但没有一个奏效。
到目前为止,我有这个,它检测到任何用户何时被 ping,而不仅仅是我。
client.on('message', (message) => {
if (message.mentions.members.first()) {
message.channel.send('Do not ping this user.');
}
});
解决方案
您可以比较用户 ID。如何获取用户 ID。
if (message.mentions.users.first().id === 'Your ID') // if the person mentioned was you
return message.channel.send('Do not mention this user');
此外,顾名思义,Collection.first()
将获取集合的第一个元素。这意味着只有在第一次提及您时,该if
语句才会返回 true 。例如:
User: 'Hello @you' // detected
User: 'Hello @notYou and @you' // not detected
为了避免这种情况,您可以使用Collection.has()
:
// will return true if *any* of the mentions were you
if (message.mentions.users.has('Your ID'))
return message.channel.send('Do not mention this user');
推荐阅读
- android - RecyclerView 测试滚动到位置
- python - 我可以运行多个 DataFrame 并将一个函数应用于每个列表吗?
- python - Cffi 和 char** 作为参数
- python - 从另一个文件导入 Python 会引发错误
- java - 将抽象类作为参数类型传递,无法覆盖方法。我对java中的多态性有误解吗?
- flutter - 如何在另一个类中使用吸气剂?
- json - 如何在 perl(cgi) 中读取 json 帖子
- php - 为什么我的 PHP 脚本在生成缩略图时会停止?
- php - 未定义常量 http/2
- inheritance - Automapper 和属性映射不适用于映射继承