Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 第一个命令后,Discord Bot没有响应_Javascript_Arrays_Bots_Discord_Discord.js - Fatal编程技术网

Javascript 第一个命令后,Discord Bot没有响应

Javascript 第一个命令后,Discord Bot没有响应,javascript,arrays,bots,discord,discord.js,Javascript,Arrays,Bots,Discord,Discord.js,所以我在创建我的机器人时遇到了一些问题。我想让它做的只是跟踪一个玩家列表,这些玩家输入命令“+me”加入“等待列表”、“-me”以从列表中删除,以及“?”以显示列表。我计划稍后添加其他命令 我遇到的问题是,命令可以很好地将某人添加到列表中,但是在第一个命令之后,bot停止响应命令。这使我能够将自己添加到队列中,但无法离开,其他人无法加入,无法列出它,等等。 另外,如果你能提到一种方法来移动我正在使用的for循环,将列表显示为一个单独的函数,我将非常感谢。我是Javascript新手,由于某种原因

所以我在创建我的机器人时遇到了一些问题。我想让它做的只是跟踪一个玩家列表,这些玩家输入命令“+me”加入“等待列表”、“-me”以从列表中删除,以及“?”以显示列表。我计划稍后添加其他命令

我遇到的问题是,命令可以很好地将某人添加到列表中,但是在第一个命令之后,bot停止响应命令。这使我能够将自己添加到队列中,但无法离开,其他人无法加入,无法列出它,等等。 另外,如果你能提到一种方法来移动我正在使用的for循环,将列表显示为一个单独的函数,我将非常感谢。我是Javascript新手,由于某种原因,我的尝试正在使它崩溃

const Discord=require('Discord.js');
const{prefix,token}=require('./config.json');
const client=new Discord.client();
变量rankedList=[];//初始化空数组
client.login(令牌);
client.once('ready',()=>{
console.log('Ready!');
})
client.once('message',message=>{
//添加到队列
if(message.content.startsWith(`${prefix}me`){
log(message.author+“添加到队列中”);
message.channel.send(message.author+“添加到队列中”);
var temp=message.author;
rankedList.push(temp);//数组将动态增长
//添加后显示队列
//对于(变量i=0;i`${index+1}-${player}`)。join('n')}`);
}
//从队列中删除
if(message.content.startsWith(`me`)){
log(message.author+“已从队列中删除”);
message.channel.send(message.author+“已从队列中删除”);
对于(var i=0;i
问题可能是您使用了“once”而不是“on”。后者每次触发事件,而前者监听一次

// Add to queue
client.on('message', async (message) => {

   // It's good practice to ignore other bots. This also makes your bot ignore itself and not get into a spam loop
    if(message.author.bot) return;

    if(message.content.startsWith(`${prefix}me`)){
        var temp = message.author;
        rankedList.push(temp);  // the array will dynamically grow
        console.log(message.author + "added to queue.");
        message.channel.send(`${message.author} added to queue.
        ${rankedList.map((player,index) => `${index+1} - ${player}`).join('
n')}`);
    }
});
您基本上是一次发送整个消息。我可以看出send()函数是异步的[],不应该像上面那样链接到for循环中。上面的代码也更简洁


我使用模板文本[]删除了多个连接。

您的控制台是否返回任何错误?不管怎么说,一次也不建议有这么多的事件函数。将所有
客户端移动一次('message,(m)=>{}')
。不建议在
for(let i=…)
中编辑
i
变量。在使用大型discord机器人之前,先使用JavaScript。在这里,我真的希望代码清理。我已经将函数移到了一个消息块中!此外,它不会返回任何错误。我得到了相同的结果。您可以尝试在函数之前添加
async
关键字吗?还可以尝试使用“on”而不是“one”,因为如果使用one,它可能不会再次侦听事件,而这正是问题的开始!!
// Add to queue
client.on('message', async (message) => {

   // It's good practice to ignore other bots. This also makes your bot ignore itself and not get into a spam loop
    if(message.author.bot) return;

    if(message.content.startsWith(`${prefix}me`)){
        var temp = message.author;
        rankedList.push(temp);  // the array will dynamically grow
        console.log(message.author + "added to queue.");
        message.channel.send(`${message.author} added to queue.
        ${rankedList.map((player,index) => `${index+1} - ${player}`).join('
n')}`);
    }
});