【问题标题】:Discord.js bot error, console: DiscordAPIError: Cannot send an empty messageDiscord.js 机器人错误,控制台:DiscordAPIError:无法发送空消息
【发布时间】:2021-03-03 11:11:12
【问题描述】:

我目前正在使用 discord.js 制作一个不和谐的笑话机器人。启动后,机器人工作正常。仅有的两个命令之一工作得很好,但我的笑话命令并不顺利。对于我的测试,我将其设置如下:

我的主要“bot.js”文件:

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '*';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))
for(const file of commandFiles){
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('JokeBot is Online!');
    console.log(`Logged in as ${client.user.tag}!`);
    client.user.setPresence({
        status: "online",  //You can show online, idle....
    });
});

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

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

    if(command == 'pun'){
        client.commands.get('pun').execute(message, args);
    } else if (command == 'joke'){
        client.commands.get('joke').execute(message, args);
    }
});

client.login('NzY4MjcyOTE5NDQ0NDU1NDg3.X4-D6Q.UTLMv192yOfpACfQ6Vm2882OLc0');

我的“joke.js”命令文件:

var messages1 = ["What’s the best thing about Switzerland?", "I invented a new word!"];
var messages2 = ["I don’t know, but the flag is a big plus.", "Plagiarism!"]
module.exports = {
    name: 'joke',
    description: "this is a joke command!",
    execute(message, args){
        var messagenumber = [Math.floor(Math.random() * 50)];

        var message1 = messages1[messagenumber];

        var message2 = messages2[messagenumber];

        message.channel.send(message1).then().catch(console.error);

        setTimeout(function(){
            message.channel.send(message2).then().catch(console.error);
        }, 3000);
        
    }
}

当我执行我的笑话命令或 *joke 时,我得到了两次:

DiscordAPIError: Cannot send an empty message
    at RequestHandler.execute (C:\Users\shery\Desktop\Stuff\JokeBot\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async RequestHandler.push (C:\Users\shery\Desktop\Stuff\JokeBot\node_modules\discord.js\src\rest\RequestHandler.js:39:14) {
  method: 'post',
  path: '/channels/773228754003558430/messages',
  code: 50006,
  httpStatus: 400
}

discord 上没有输出,我尝试关闭控制台错误捕获块,但这只是给了我不同的错误消息。我不知道该怎么办。如果有人可以帮助我,那就太好了。

谢谢!

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    var messagenumber = [Math.floor(Math.random() * 50)]; 您正试图从中获取 0 到 49 之间的随机数。您的笑话长度太小,无法计算随机最大值。

    试试

    function getRandomValue(arr) {
        return arr[(Math.floor(Math.random() * arr.length))];
    }
    
    console.log(getRandomValue([2,3]))

    所以你解释了每一个笑话,仅此而已。

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 2021-12-01
      • 2020-04-07
      • 2022-01-01
      • 2021-09-15
      • 2020-10-09
      • 2021-06-25
      • 2021-06-10
      相关资源
      最近更新 更多