简体   繁体   中英

Discord.js v12 Covid Stats command

So basically I have a covid command which shows the stats and works perfectly., all though. whenever u say something that is not a real text for example "covid ejdetj" it will give errors non-stop. I came here because I don't know how to catch the error so if a person says "covid kfgk" it'll send back a message "Can't do this" heres the code.

client.on('message', async message =>{
  if(message.content.toLowerCase() === prefix + "covid all") {
    const coronaEmbed = new Discord.MessageEmbed()
    const data = await api.all()
    coronaEmbed.setColor('#00B2B2')
    .setTitle("🌐 Global Cases")
    .setDescription("Number of cases may differ from other sources")
    .addField("Cases", data.cases, true)
    .addField("Active", data.active, true)
    .addField("Cases Today", data.todayCases, true)
    .addField("Critical Cases", data.critical, true)
    .addField("Deaths", data.deaths, true)
    .addField("Recovered", data.recovered, true)
    .setFooter(`Requested by ${message.author.tag}`, message.author.displayAvatarURL())
    .setTimestamp()
    message.channel.send(coronaEmbed);
    
  } else if(message.content.toLowerCase().startsWith(prefix + "covid") && message.content.toLowerCase() !== prefix + "covid all") {
    const countrycovid = message.content.slice(prefix.length).split(' ')
    const countrydata = await api.countries({country: countrycovid})
    const countryEmbed = new Discord.MessageEmbed()
    .setColor('#00B2B2')
    .setTitle(`${countrycovid[1]} cases`).setThumbnail(countrydata.countryInfo.flag)
    .setDescription("Number of cases may differ from other sources")
    .addField("Cases", countrydata.cases, true)
    .addField("Active", countrydata.active, true)
    .addField("Cases Today", countrydata.todayCases, true)
    .addField("Critical Cases", countrydata.critical, true)
    .addField("Deaths", countrydata.deaths, true)
    .addField("Recovered", countrydata.recovered, true)
    .setFooter(`Requested by ${message.author.tag}`, message.author.displayAvatarURL())
    .setTimestamp()
    message.channel.send(countryEmbed);
} else {
  if(message.content.toLowerCase() === prefix + "help covid all") {
    const newEmbed = new Discord.MessageEmbed()
    .setColor('#00B2B2')
    .setTitle('**Covid All Help**')
    newEmbed.setDescription('This command sends you an executor of the global corona stats.')
    .setFooter(`Requested by ${message.author.tag}`, message.author.displayAvatarURL())
    .setTimestamp();
    message.channel.send(newEmbed);
  
} else {
  if(message.content.toLowerCase() === prefix + "help covid country") {
    const newEmbed = new Discord.MessageEmbed()
    .setColor('#00B2B2')
    .setTitle('**Covid Country Help**')
    newEmbed.setDescription('This command sends you an executor of the corona stats in countries around the world.')
    .setFooter(`Requested by ${message.author.tag}`, message.author.displayAvatarURL())
    .setTimestamp();
    message.channel.send(newEmbed)
  }
}}})

Use a trycatch statement on the const countrydata = await api.countries({country: countrycovid}) !

It will look something like this:

try {
  const countrydata = await api.countries({country: countrycovid})
} catch (err) {
  message.reply("Can't do this");
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM