Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...