gpt4 book ai didi

javascript - Discord.js bot 错误,控制台 : DiscordAPIError: Cannot send an empty message

转载 作者:行者123 更新时间:2023-12-04 08:33:23 24 4
gpt4 key购买 nike

我目前正在使用 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 上没有输出,我曾尝试取消控制台错误捕获块,但这只是给了我不同的错误消息。我不知道该怎么办。如果有人可以帮助我,那就太好了。
谢谢!

最佳答案

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]))

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

关于javascript - Discord.js bot 错误,控制台 : DiscordAPIError: Cannot send an empty message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64922640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com