discord.js v12 的问题

问题描述 投票:0回答:2

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

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);
      }
    );
  }
});
javascript node.js discord discord.js
2个回答
0
投票

这对我有用。我添加的内容的解释在代码中。

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);
          })

  }
});


0
投票

URL
中的空格替换为
%20

您需要像这样写

URL

`https://corona.lmao.ninja/v2/states/${country.replace(/\s/, '%20')}`
© www.soinside.com 2019 - 2024. All rights reserved.