【问题标题】:How do i make a discord bot send a message when a specific user is mentioned?当提到特定用户时,如何让不和谐机器人发送消息?
【发布时间】:2021-01-04 21:17:47
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    您可以比较用户 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() 将获取集合的第一个元素。这意味着if 语句只有在 first 提及的是您时才会返回 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'); 
    

    【讨论】:

      猜你喜欢
      • 2019-07-11
      • 1970-01-01
      • 2021-08-14
      • 2021-06-17
      • 2021-04-25
      • 2020-12-26
      • 2020-12-18
      • 2020-11-12
      • 2019-12-10
      相关资源
      最近更新 更多