【问题标题】:Discord Bot not responding after first commandDiscord Bot 在第一个命令后没有响应
【发布时间】:2020-02-03 22:15:16
【问题描述】:

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

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

const Discord = require('discord.js');
const {prefix, token} = require('./config.json');
const client = new Discord.Client();
var rankedList = []; // Initialise an empty array

client.login(token);

client.once('ready', () =>{
    console.log('Ready!');
})

client.once('message', message =>{
    // Add to queue
    if(message.content.startsWith(`${prefix}me`)){
        console.log(message.author + "added to queue.");
        message.channel.send(message.author + " added to queue.");

        var temp = message.author;
        rankedList.push(temp);  // the array will dynamically grow

        // Show queue after adding
        //for (var i = 0; i < rankedList.length; i++) {
        //  message.channel.send(i+1 + "- " + rankedList[i]);
        //}
        message.channel.send(`${rankedList.map((player,index) => `${index+1} - ${player}`).join('n')}`);
    }

    // Remove from queue
    if(message.content.startsWith(`-me`)){
        console.log(message.author + "removed from queue.");
        message.channel.send(message.author + " removed from queue.");

        for( var i = 0; i < rankedList.length; i++){ 
            if ( rankedList[i] === message.author) {
                rankedList.splice(i, 1); 
                i--;
            }
        }

        // Show queue after removing
        for (var i = 0; i < rankedList.length; i++) {
            message.channel.send(i+1 + "- " + rankedList[i]);
        }
    }

    if(message.content.startsWith(`?list`)){
        console.log(message.author + "displayed the queue.");

        // Show list
        for (var i = 0; i < rankedList.length; i++) {
            message.channel.send(i+1 + "- " + rankedList[i]);
        }
    }
})

【问题讨论】:

  • 您的控制台是否返回任何错误?无论如何,真的不建议一个甚至有这么多的事件函数。将您的所有client.once(‘message, (m)=&gt;{}’) 移至一个功能下。不建议在for(let i = ...) 中编辑i 变量。在使用大型不和谐机器人之前,请先使用 JavaScript。真的希望在这里进行代码清理。
  • 我已将函数移到一个消息块中!它也没有返回错误

标签: javascript arrays bots discord discord.js


【解决方案1】:

问题可能是您使用了“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() 函数是异步的 [https://discord.js.org/#/docs/main/stable/class/DMChannel?scrollTo=send] 并且不应该像上面那样链接在 for 循环中。上面的代码也比较简洁

我使用了模板文字 [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals] 来删除多个串联。

【讨论】:

  • 我得到了同样的结果
  • 可以试试在函数前加async关键字吗?
  • 也尝试使用'on'而不是'once'
  • 因为如果你使用一次,它可能不会再次监听事件,这可能是开始的问题!
猜你喜欢
  • 2022-01-04
  • 2020-11-03
  • 2020-12-15
  • 2021-08-28
  • 2021-02-17
  • 2023-03-09
  • 2021-04-17
相关资源
最近更新 更多