为什么我的 discord 机器人不知道它的@Mention?

Why doesn't my discord bot know its @Mention?

我正在开发一个 discord 机器人(这只是 AI 模块,还有另一个连接到同一个机器人用于命令和其他东西),但它不知道自己的@Mention。

const token = process.env.PixelBot_token;
const keep_alive = require('./keep_alive.js');
const sendReply = require('./sendReply.js');
const Discord = require('discord.js');
const client = new Discord.Client();

// Set the client user's presence
client.on('ready', () => {
  var prefix = client.user.tag;
  console.log('PixelBot AI Loaded!');
  console.log(prefix);
});

client.on('message', message => {

  if (message.author.bot) return;
  var prefix = + client.user.tag;
  console.log(prefix);

  if (message.content.substring(0, prefix.length) === prefix) {

    //useful variables
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    if (command === "help") {
      console.info("Help Message Sent!\n");
    };

  };

});

client.login(token);

以上代码打印出来:

PixelBot AI Loaded!
PixelBot#9188

但是当我发送@PixelBot 帮助时什么也没做。但是,如果我将前缀更改为字符串,例如:"pb",它就可以工作。它显然在 "client.on('ready')" 中知道它,因为它被打印出来了。有什么想法吗?

编辑:这是带有@Discord 专家建议的代码:

const token = process.env.PixelBot_token;
const keep_alive = require('./keep_alive.js');
const sendReply = require('./sendReply.js');
const Discord = require('discord.js');
const client = new Discord.Client();

// Set the client user's presence
client.on('ready', () => {
  console.log('PixelBot AI Loaded!');
});

client.on('message', message => {

  if (message.author.bot) return;

  if (message.mentions.users.first() === client.user) {

    //useful variables
    const command = message.content.slice(message.mentions.users.first().length).trim().split(/ +/g);
    const identifier = command.shift().toLowerCase();
    console.log('identifier = "' + identifier + '"');
    console.log('command = "' + command +'"');
    console.log('message content = "' + message.content + '"');

    if (command === "help") {
      console.info("Help Message Sent!\n");
    };

  };

});

client.login(token);

打印出来:

PixelBot AI Loaded!
identifier = "<@569971848322744320>"
command = "help"
message content = "<@569971848322744320> help"

所以它知道command === "help",那为什么不执行if语句呢?

当人们在 discord 上 ping 你时,一个 ID 就像所有 discord 上的一样, 所以实际上是:

<@USERID>

我建议探索 message.mentions.users.first() 并将其与您的 client.user

进行比较

或者message.mentions.users.first().idclient.user.id

此致 Discord 专家

好的,我在这里回答我自己的 post 因为我发现了问题。出于某种原因,使用 === 而不是 == 会阻止它工作。不知道为什么。