DiscortJS 机器人在频道上发回一条消息,提及特定用户

DiscortJS bot send a message back on the channel mentioning a specific user

我在 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})
    }
  }

});

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

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

尝试:

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

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

谢谢@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 + '> !!');

}

}