0

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;
2
  • (!args[1] == " "|| !args[1] == "") should be (!args[1] == " "&& !args[1] == "") so that args can't be containing just a space nor be empty, right? Commented Nov 19, 2017 at 22:01
  • args[1].trim().length would combine both ifs into one. Also, what does append do? Not a native array function I'm familiar with... Commented Nov 19, 2017 at 22:29

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

10 Comments

@Tjtorin can you console.log(wordsString) and tell me the results
it just has an empty space in the console
So right there you can tell that's your problem. Your bot is trying to send back and blank message, and discord is saying "hey, you can't do that". It's hard to see what exactly you're trying to do. But obviously, when you do words.append(args[i]) nothing is being added to your words array
The best advice I can give you - go step by step through your block of code you posted and console.log everything. First, what does words contain after your for-loop
Ty I figured it out. I had to change the == to and <= in the for loop and take away " and " in the words.join and a few other things and now it works!
|
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.