Reputation: 31
I am currently making a discord joke bot with 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:
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:
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:
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. I do not know what to do. If anyone could help me, that would be wonderful.
Thanks!
Upvotes: 2
Views: 152
Reputation: 352
var messagenumber = [Math.floor(Math.random() * 50)];
You are trying to get a random number from 0 to 49 from this. 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.
Upvotes: 2