почему там написано .setDescription не является функцией? (RichEmbed в JavaScript)

#javascript #node.js #discord.js

#javascript #node.js #discord.js

Вопрос:

Я создаю embed для своей команды /quotes , которая будет использоваться для моего бота discord.

Вот код:

 if(cmd === `${prefix}quotes`){

  quotes = new Discord.RichEmbed();
  q1 = "  -- Andres Iniesta";
  q2 = "  -- French Poverb";
  q3 = "  -- www.quotesgate.com"

  .setDescription("**__Here are some inspirational quotes!__**")
  .setColor("#319786")
  .addField("**Some people like you, some people don't. In the end, you just have to be yourself.**". q1)
  .addField("**Wherever life plants you, bloom with grace**", q2)
  .addField("Don’t forget you’re human. It’s okay to have a meltdown. Just don’t unpack and live there. Cry it out. Then refocus on where you’re headed", q3)

  return message.channel.send(quotes);
}
  

Я продолжаю получать сообщение об ошибке на моей консоли, что .setDescription не является функцией

 / | TomatoHeadIdiot is now active in 4 servers!
(node:7448) UnhandledPromiseRejectionWarning: TypeError: "  -- www.quotesgate.com".setDescription is not a function
  

Комментарии:

1. пожалуйста, уточните: перед .setDescription должен быть объект, не так ли? Или это какие-то методы, добавленные в String.prototype ?

Ответ №1:

Проблема в том, что у вас нет точки с запятой после строки q3 = " -- www.quotesgate.com" , поэтому она вставляет следующую строку сразу после нее. Таким образом, это в основном становится

 q3 = "  -- www.quotesgate.com".setDescription("**__Here are some inspirational quotes!__**")
  

что, конечно, не сработает. Я предполагаю, что вы хотите вызвать setDescription и addField при внедрении, поэтому вам нужно изменить свой код на следующий:

 if(cmd === `${prefix}quotes`){

  q1 = "  -- Andres Iniesta";
  q2 = "  -- French Poverb";
  q3 = "  -- www.quotesgate.com";

  quotes = new Discord.RichEmbed()
    .setDescription("**__Here are some inspirational quotes!__**")
    .setColor("#319786")
    .addField("**Some people like you, some people don't. In the end, you just have to be yourself.**", q1)
    .addField("**Wherever life plants you, bloom with grace**", q2)
    .addField("Don’t forget you’re human. It’s okay to have a meltdown. Just don’t unpack and live there. Cry it out. Then refocus on where you’re headed", q3);
  
  return message.channel.send(quotes);
}
  

Ответ №2:

 if(cmd === `${prefix}quotes`){

  quotes = new Discord.RichEmbed();
  q1 = "  -- Andres Iniesta";
  q2 = "  -- French Poverb";
  q3 = "  -- www.quotesgate.com";

  quotes.setDescription("**__Here are some inspirational quotes!__**");
  quotes.setColor("#319786");
  quotes.addField("**Some people like you, some people don't. In the end, you just have to be yourself.**". q1);
  quotes.addField("**Wherever life plants you, bloom with grace**", q2);
  quotes.addField("Don’t forget you’re human. It’s okay to have a meltdown. Just don’t unpack and live there. Cry it out. Then refocus on where you’re headed", q3);

  return message.channel.send(quotes);

}
  

Готово! 🙂