【问题标题】:DiscortJS bot send a message back on the channel mentioning a specific userDiscortJS 机器人在提及特定用户的频道上发回消息
【发布时间】:2020-07-18 05:15:19
【问题描述】:

我已经在 js 上构建了这个消息。如果您将机器人标记为用户以添加消息+标记该人,我想添加一个条件,否则只是发送普通消息。

我遇到的问题是 user_mention 的正确变量是什么。我找到了不同的方法,但无法使其发挥作用。

DiscordClient.on('message', message => {
  const msg = message.content.toLowerCase();
  const mention = message.mentions.users;

  if (msg === "yubnub") {
    if (mention == null){
      message.channel.send('YUB NUB!! YUB NUB!! Grrrrr!!');
    } else {
      message.channel.send('YUB NUB!! YUB NUB!! Grrrrr!! ' + ${@user_mention})
    }
  }

});

【问题讨论】:

    标签: javascript bots discord discord.js mention


    【解决方案1】:

    谢谢@boris 和@Adrian。最终代码如下所示:

    if (msg.startsWith("yubjub")) {
    const mention = message.mentions.members;
    
    if (mention.size === 0){
    
      message.channel.send('YUB NUB!! YUB NUB!! Grrrrr!!);
    
    } else {
        const mentionUser = mention.first().user;
    
        message.channel.send('YUB NUB!! YUB NUB!! Stab Stab Stab <@' + mentionUser.id + '> !!');
    
    }
    

    }

    【讨论】:

      【解决方案2】:

      我认为mentionusers 的数组。所以你可以这样做:

      for (const user of mention) {
          message.channel.send('YUB NUB!! YUB NUB!! Grrrrr!! @' + user.username)
      }
      

      【讨论】:

      • 不谈JS条件。谈论我如何获得@user_mention。我需要正确的变量/参数。我找不到合适的。
      • 很抱歉,这是错误的。 @Creator您永远不应该将错误的答案标记为正确的。这将导致有人稍后找到错误的答案并尝试使用它。正确的答案其实是你自己的答案。
      【解决方案3】:

      试试:

      const mention = message.mentions.users.first();
      

      来源:https://anidiots.guide/first-bot/command-with-arguments#grabbing-mentions

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-10
        • 2021-07-14
        • 1970-01-01
        • 2021-06-19
        • 2020-09-25
        • 1970-01-01
        • 2016-10-16
        • 1970-01-01
        相关资源
        最近更新 更多