Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么';难道我的不和机器人不知道它的名字吗?_Javascript_Node.js_Discord.js - Fatal编程技术网

Javascript 为什么';难道我的不和机器人不知道它的名字吗?

Javascript 为什么';难道我的不和机器人不知道它的名字吗?,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,我正在开发一个discord机器人(这只是一个AI模块,还有一个连接到同一个机器人来执行命令和其他东西),但它不知道自己的@Antice const token = process.env.PixelBot_token; const keep_alive = require('./keep_alive.js'); const sendReply = require('./sendReply.js'); const Discord = require('discord.js'); const cl

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

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 AI Loaded!
identifier = "<@569971848322744320>"
command = "help"
message content = "<@569971848322744320> help"
但当我发送@PixelBot帮助时,什么也不做。但是,如果我将前缀更改为字符串,例如:“pb”,它就可以工作。它显然在“client.on('ready')”中知道它,因为它被打印出来了。有什么想法吗

编辑:以下是带有@Discord Expert建议的代码:

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!
PixelBot#9188
PixelBot AI Loaded!
identifier = "<@569971848322744320>"
command = "help"
message content = "<@569971848322744320> help"
PixelBot AI已加载!
identifier=“”
command=“帮助”
消息内容=“帮助”

它知道command==“help”,那么为什么不执行if语句呢?

当人们在discord上ping你时,一个ID跟在discord上的所有东西后面, 所以实际上是:

我建议探索
消息.提及.用户.first()
并将其与您的
客户端进行比较。用户

或者
message.indications.users.first().id
client.user.id

致意
Discord Expert

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

嗯,它还是不起作用。。。我将编辑帖子,让你的建议出现在帖子中。而不是
消息.提及.用户。首先
,你可以尝试
message.ismetentied(client.user)
查看这两个操作符之间的差异。要回答为什么它不适用于
==
:在定义
命令
变量的行尾,您有
.split(+/++/g)
.split
创建一个段数组,因此您的
命令
变量是一个数组。在本例中,它只包含1个字符串“help”。因此,当您尝试
==
时,您将数组与字符串进行比较,但失败了。