I’m working on a Discord bot using Node.js and having trouble with a specific command. The bot should repeat back whatever text the user types after the command, but instead I keep getting this error: DiscordAPIError: Cannot send empty message. When I try to debug it with console.log(), nothing shows up which means the variable is empty somehow. I can’t figure out what’s going wrong with my logic.
case "repeat":
if(!parameters[1] == " "|| !parameters[1] == ""){
// channel.send(parameters[1]);
textArray = [];
for(j=0;j==parameters.length-1;j++){
textArray.push(parameters[j]);
}
var finalText = textArray.join(" and ");
console.log("Bot output: "+finalText);
channel.send(finalText);
}
break;
Your problem is with how you’re handling the parameters array. Using parameters[1] only grabs the first word after the command, not the full message. If someone just types the command without text, parameters[1] becomes undefined - that’s why you’re getting empty messages. Your for loop is also broken. The condition j==parameters.length-1 only runs when j exactly equals that value, but j starts at 0, so it never executes. Here’s a fix: const message = parameters.slice(1).join(' '); if(message.trim()) { channel.send(message); } This grabs everything after the command and checks there’s actual content before sending.
Your for loop condition is wrong - you’re using j==parameters.length-1 which checks if j equals that value, not if it’s less than or equal to it. Since j starts at 0, the loop never runs because 0 doesn’t equal the array length. Your if statement with the double negation is also messy. Just scrap the loop entirely and use parameters.slice(1).join(" ") instead. This grabs everything after the command and joins it with spaces - way cleaner. The slice method handles empty parameters fine, and you can just check if the result’s empty before sending.
your conditional logic is broken - !parameters[1] == "" doesn’t work like you think it does. use if(parameters.length > 1 && parameters[1]) instead. also, your loop starts at j=0 but pushes parameters[j], which includes the command itself. either start the loop at j=1 or just use slice like the other person suggested.