I'm making a bot to give information about covid 19, everything is fine but I don't know why when I search for a country with spaces I get "undefined" instead of the data ... My code:
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);
}
);
}
});
This worked for me. The explanation of what i added is in the code.
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);
})
}
});