Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.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_Discord.js - Fatal编程技术网

Javascript 机器人程序没有响应前缀

Javascript 机器人程序没有响应前缀,javascript,node.js,discord,discord.js,Javascript,Node.js,Discord,Discord.js,好吧,我刚决定做一个全新的机器人。最后一个是buggy,很多功能都不起作用。我决定把它写得更聪明些,但到目前为止效果并不太好。我甚至无法通过我的第一个命令:( 因此,我有以下代码: const Discord = require('discord.js'); const bot = new Discord.Client(); const client = new Discord.Client(); const token = "<my token>" const prefix =

好吧,我刚决定做一个全新的机器人。最后一个是buggy,很多功能都不起作用。我决定把它写得更聪明些,但到目前为止效果并不太好。我甚至无法通过我的第一个命令:(

因此,我有以下代码:

const Discord = require('discord.js');
const bot = new Discord.Client();
const client = new Discord.Client();
const token = "<my token>"

const prefix = 'cb!';

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

    let msg = message.content.toUpperCase();
    let sender = message.author;
    let cont = message.content.slice(prefix.length).split(" ");
    let args = cont.slice(1);
// Commands
    // Ping
    if (msg === prefix + 'PING') {
         message.channel.send('Ping!');
    }

bot.on('ready', () => {
    console.log(`running`)

});


bot.login(token);
const Discord=require('Discord.js');
const bot=new Discord.Client();
const client=new Discord.client();
const token=“”
常量前缀='cb!';
bot.on('message',message=>{
让msg=message.content.toUpperCase();
让发件人=message.author;
让cont=message.content.slice(前缀.length).split(“”);
设args=cont.slice(1);
//命令
//平
如果(msg==前缀+PING){
message.channel.send('Ping!');
}
bot.on('ready',()=>{
console.log(`running`)
});
bot.login(令牌);
我的命令不能与
prefix+“ping”
prefix+purge
一起使用。 我的前缀被定义为
const cb!=prefix
。我也尝试过
let prefix=cb!

如果我将ping的代码设置为:
If(msg==='ping'
,它会工作。所以我知道bot正在工作,它只是没有响应
前缀+ping'
,或者至少我认为是这样。 那我该怎么办


一如既往,感谢您抽出时间阅读此邮件。

当有人写此邮件时(例如cb!ping)节点将其保存到变量中,并将其更改为大写。然后节点将变量与
前缀+PING'
进行比较,最终得到
CB!PING==CB!PING
,返回
false
,因此将
let msg=message.content.toUpperCase();
更改为
let msg=message.content.toLowerCase());
。示例:

const Discord=require('Discord.js');
const bot=new Discord.Client();
const client=new Discord.client();
const token=“”
常量前缀='cb!';
bot.on('message',message=>{
让msg=message.content.toLowerCase();
让发件人=message.author;
让cont=message.content.slice(前缀.length).split(“”);
设args=cont.slice(1);
//命令
//平
如果(msg==前缀+PING){
message.channel.send('Ping!');
}
bot.on('ready',()=>{
console.log(`running`)
});

bot.login(token);
=
的两侧转换为相同的大小写

您正在错误处理
.toUpperCase()
.toLowerCase()

如果要以不区分大小写的方式比较字符串,则必须将
==
的两边都转换为小写或大写

//小写
if(msg.toLowerCase()==(前缀+“ping”).toLowerCase()){
/*做事*/
};
//大写字母
如果(msg.toUpperCase()==(前缀+“ping”).toUpperCase()){
/*做事*/
};
注:

  • 无论你选择哪一个,都要坚持这个决定,这样就不会再发生这种情况了
  • 声明变量时也可以保持一致。如果要使用大写,则应将变量声明为大写
    const prefix=“CB!”;

prefix+'PING'是cb!PING,你确定你在处理空格吗?我不知道你所说的处理空格是什么意思,但我希望我发送“cb!PING”,而bot用“Pong!”我相信
let msg=message.content.toUpperCase
行修复了所有大写的PING,如果这是你的意思。啊,我明白了。我怎样才能保留空格?或者我能够?有同样的问题,没有响应。