discord.js v12的问题

时间:2020-09-03 15:17:26

标签: javascript node.js discord discord.js

我正在制作一个机器人来提供有关covid 19的信息,一切都很好,但是我不知道为什么当我搜索带有空格的国家/地区时会得到“未定义”而不是数据...我的代码:< / p>

client.on("message", async (message) => {
  const args = message.content.trim().split(/ + /g);
  if (message.content.startsWith(prefix + "states")) {
    const country = args[1];
    var request = require("request");
    return request(
      `https://corona.lmao.ninja/v2/states/$ {country}`,
      (err, response, body) => {
        if (err) throw err;
        var data = JSON.parse(body);

        let spain = new Discord.MessageEmbed()
          .setColor("# ff0000")
          .setTitle(`Sars-Cov-2 Disease In $ {country}`)
          .addField(": o: • Cases •: o:", data.cases)
          .addField(": o: • Active Cases •: o:", data.active)
          .addField(": test_tube: • Recovered •: test_tube:", data.recovered)
          .addField(": skull: • Deaths: •: skull:", data.deaths)
          .addField(": skull: • Today's deaths •: skull:', data.todayDeaths")
          .addField("?️ • People in critical situation • ?️", data.critical)
          .addField(
            ": skull: • Deaths per million population •: skull:",
            data.deathsPerOneMillion
          )
          .addField(
            "?️ • Critics per million inhabitants • ?️",
            data.criticalPerOneMillion
          )

          .addField("   • Tests Performed •   ", data.tests)
          .addField("⌛ • Update • ⌛  n * Live *", ".")
          .setDescription(
            "If 0 is displayed, it is because the data of the last 24 H have not been given yet, if there is an error, please contact us through the administration contact tools. If you want more countries , we encourage you to suggest it and we will very surely add them."
          )
          .setImage(
            "https://cdn.pixabay.com/photo/2012/04/12/13/49/biohazard-symbol-30106_640.png"
          );

        message.channel.send(Spain);
      }
    );
  }
});

2 个答案:

答案 0 :(得分:0)

imBlocked is not defined中的空格替换为URL

您将要这样写%20

URL

答案 1 :(得分:0)

这对我有用。我添加的内容的解释在代码中。 如果您仍然有问题或需要帮助,请告诉我,我们将很乐意为您提供帮助。 请尝试理解我在此代码中使用的每种方法,如果您不这样做,则可以在Discord(піс#1311)上加我,我会帮您(我也说西班牙语)。

client.on("message", async (message) => {
  const prefix = "t-"; // Added this to test in my bot. You should remove this line if you defined prefix before.
  const args = message.content.substring(prefix.length).split(" "); // Defined args as i always do for my bots
  const command = args.shift().toLowerCase(); // Defined this variable to make args easier to use (i always use this way)

  if(command === "states") {
    const country = args.join(' ') // You should use the array.join(...) function since a member might be looking for a country with spaces such as "New York".
    // (If you use args[0] the bot will search just "New" instead of "New York")

    if(!country) return; // Check the country (Args) isn't undefined. Added this return In case someone uses the command but doesn't select any country

    var request = require("request");
    return request(
      `https://corona.lmao.ninja/v2/states/${country}`,
      (err, response, body) => {
        if (err) throw err;
        var data = JSON.parse(body);
        
        if(data && data.message === "State not found or doesn't have any cases") return message.reply("State not found or doesn't have any cases") // The bot will return this if the link doesn't find the selected country

        let Spain = new Discord.MessageEmbed()
          .setColor("# ff0000")
          .setTitle(`Sars-Cov-2 Disease In ${country}`)
          .addField(": o: • Cases •: o:", data.cases)
          .addField(": o: • Active Cases •: o:", data.active)
          .addField(": test_tube: • Recovered •: test_tube:", data.recovered)
          .addField(": skull: • Deaths: •: skull:", data.deaths)
          .addField(": skull: • Today's deaths •: skull:', data.todayDeaths")
          .addField("?️ • People in critical situation • ?️", data.critical)
          .addField(
            ": skull: • Deaths per million population •: skull:",
            data.deathsPerOneMillion
          )
          .addField(
            "?️ • Critics per million inhabitants • ?️",
            data.criticalPerOneMillion
          )

          .addField("   • Tests Performed •   ", data.tests)
          .addField("⌛ • Update • ⌛  n * Live *", ".")
          .setDescription(
            "If 0 is displayed, it is because the data of the last 24 H have not been given yet, if there is an error, please contact us through the administration contact tools. If you want more countries , we encourage you to suggest it and we will very surely add them."
          )
          .setImage(
            "https://cdn.pixabay.com/photo/2012/04/12/13/49/biohazard-symbol-30106_640.png"
          );

        message.channel.send(Spain);
          })

  }
});