首页 > 解决方案 > 当提到特定用户时,如何让不和谐机器人发送消息?

问题描述

我对 javascript 还很陌生,并且一直在使用 Discord.js 制作一两个 Discord Bot。我正在开发一项功能,当我在我的不和谐服务器中被 ping 时发送消息。我已经尝试了几件事,但没有一个奏效。

到目前为止,我有这个,它检测到任何用户何时被 ping,而不仅仅是我。

client.on('message', (message) => {
 if (message.mentions.members.first()) {
  message.channel.send('Do not ping this user.');
 }
});

标签: javascriptnode.jsdiscorddiscord.js

解决方案


您可以比较用户 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'); 

推荐阅读