Tjtorin
Tjtorin

Reputation: 99

NodeJS discord bot commands getting error Cannot send an empty message

I am making a discord bot and one of my commands is not working. I want the bot to copy what the user said in the command but I am getting the Error: UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): DiscordAPIError: Cannot send empty message and I also tried using console.log() but that was empty too so I know something is wrong but i'm just not sure what is.

case "say":
    if(!args[1] == " "|| !args[1] == ""){
    //  message.channel.sendMessage(args[1]);
      words = [];
      for(i=0;i==args.length-1;i++){
        words.append(args[i]);
      }
      var wordsString = words.join(" and ");
      console.warn("Bot said: "+wordsString);
      message.channel.sendMessage(wordsString);
    }
break;

Upvotes: 0

Views: 23007

Answers (2)

Skemicle
Skemicle

Reputation: 21

Your original for loop for(i=0;i==args.length-1;i++) says to set i equal to 0; then as long as i equals the number of arguments in your array do the following code then increase i by one. i equals 0 so it never equals the number of arguments so therefore the code never gets ran. Just changing for(i=0;i==args.length-1;i++) to for(i=0;i<args.length-1;i++) should fix your issue

Upvotes: 2

23k
23k

Reputation: 1399

I've gotten that same error when sending a non-string message to the channel, like trying to send an Array or Object.

But, the most self-explanatory one, as is the case here, you're trying to send an empty string to the channel.

case "say":
    if (!args[1] == " " && !args[1] == "") {
      // message.channel.sendMessage(args[1]);
      let words = [];
      for (let i = 0; i == args.length - 1; i++) {
        words.append(args[i]);
      }
      var wordsString = words.join(" and ");
      console.warn("Bot said: "+ wordsString);
      message.channel.sendMessage(wordsString);
    }
break;

As another user pointed out, you want to leave out spaces and empty strings from your if statement. So changing || to && will accomplish this.


As a side note

You never declared i as variable, or words (unless you're declaring outside the block).

Upvotes: 3

Related Questions