当提到特定用户时,如何使不和谐的机器人发送消息?

时间:2020-09-17 23:04:55

标签: javascript node.js discord discord.js

我对javascript还是很陌生,并且一直在使用Discord.js制作一个或两个Discord Bot。我正在研究一种功能,当我在不和谐的服务器上ping通时会发送一条消息。我已经尝试了几件事,但都没有成功。

到目前为止,我已经掌握了这个功能,它可以检测到何时对任何用户执行ping操作,而不仅仅是我。

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

1 个答案:

答案 0 :(得分:2)

您可以比较用户ID。 How to get User IDs.

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()将获取集合的第一个元素。这意味着{<1}}语句仅在您提到 first 时返回true。例如:

if

要避免这种情况,您可以使用Collection.has()

User: 'Hello @you' // detected
User: 'Hello @notYou and @you' // not detected