简体   繁体   English

Discord.js bot 错误,控制台:DiscordAPIError:无法发送空消息

[英]Discord.js bot error, console: DiscordAPIError: Cannot send an empty message

I am currently making a discord joke bot with discord.js.我目前正在使用 discord.js 制作一个不和谐的笑话机器人。 Upon startup, the bot works jut fine.启动后,机器人工作正常。 One of the only two commands works just fine, but my joke command does not go so well.仅有的两个命令之一工作得很好,但我的笑话命令不太好。 For my testing, I have it set up like this:对于我的测试,我将其设置如下:

My main "bot.js" file:我的主要“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');

My "joke.js" command file:我的“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);
        
    }
}

When I do my joke command, or *joke, I get this, twice:当我执行我的笑话命令或 *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
}

There is no output on discord, and I have tried taking off the console error catch blocks, but this just gave me a different error message. discord 上没有输出,我曾尝试取消控制台错误捕获块,但这只是给了我不同的错误消息。 I do not know what to do.我不知道该怎么办。 If anyone could help me, that would be wonderful.如果有人可以帮助我,那就太好了。

Thanks!谢谢!

var messagenumber = [Math.floor(Math.random() * 50)]; You are trying to get a random number from 0 to 49 from this.您正试图从中获得一个从 0 到 49 的随机数。 The length of your jokes is too small to account for the random max.你的笑话长度太小,无法解释随机最大值。

Try尝试

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

So you account for every joke and no more.所以你解释了每一个笑话,仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Discord.js UnhandledPromiseRejectionWarning:DiscordAPIError:无法发送空消息 - Discord.js UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message Discord bot 的 NodeJS 错误:UnhandledPromiseRejectionWarning:DiscordAPIError:无法发送空消息 - NodeJS error with discord bot: UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message Discord.js some-random-on-discord meme API 不工作:DiscordAPIError:无法发送空消息 - Discord.js something-random-on-discord meme API not working: DiscordAPIError: Cannot send an empty message Discord.js 嵌入错误 无法发送空消息 - Discord.js Embed error Cannot send an empty message DiscordAPIError:无法发送带有 Discord 嵌入的空消息 - DiscordAPIError: Cannot send an empty message with Discord embed JS Discord bot出现错误:无法发送空消息 - JS discord bot is getting error: Cannot send an empty message Discord.Js - 未捕获的 DiscordAPIError:无法向该用户发送消息 - Discord.Js - Uncaught DiscordAPIError: Cannot send messages to this user discord 机器人中的错误消息? (discord.js) - error message in discord bot? (discord.js) 无法在 discord.js v13 上发送空消息 - Cannot send an empty message on discord.js v13 尝试在 Discord.js 中发送 Axios 响应时无法发送空消息错误 - Cannot send an empty message error when trying to send Axios response in Discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM