最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS discord bot commands getting error Cannot send an empty message - Stack Overflow

programmeradmin4浏览0评论

I am making a discord bot and one of my mands is not working. I want the bot to copy what the user said in the mand 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;

I am making a discord bot and one of my mands is not working. I want the bot to copy what the user said in the mand 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;
Share Improve this question edited Nov 19, 2017 at 21:56 Quentin 945k132 gold badges1.3k silver badges1.4k bronze badges asked Nov 19, 2017 at 21:55 TjtorinTjtorin 991 gold badge1 silver badge10 bronze badges 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? – user7838288 Commented Nov 19, 2017 at 22:01
  • args[1].trim().length would bine both ifs into one. Also, what does append do? Not a native array function I'm familiar with... – James Commented Nov 19, 2017 at 22:29
Add a ment  | 

2 Answers 2

Reset to default 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 acplish this.


As a side note

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

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

发布评论

评论列表(0)

  1. 暂无评论