Skip to content

Commit

Permalink
Implemented more rigorous input validation for the deaths command
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pierce committed Jul 27, 2020
1 parent e9d3304 commit 95bf69b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 40 deletions.
Binary file modified .DS_Store
Binary file not shown.
104 changes: 64 additions & 40 deletions commands/deaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
return message.channel.send(`Globally, ${totalDeaths["deaths"]} people have died due to COVID-19.`);
}
// Data for TODAY
else if (args.length <= 2 && (args[0] === "today" || args[0] === "td")) {
else if ((args[0] === "today" || args[0] === "td")) {
// Global deaths TODAY
if (args.length == 1) {
let getDeaths = async () => {
Expand Down Expand Up @@ -82,6 +82,9 @@ module.exports = {
console.log(totalYTDDeathsCountry);
message.channel.send(`Yesterday, ${totalYTDDeathsCountry} people died due to COVID-19 in ${info["country"]}.`);
}
else {
return message.channel.send(`<@${message.author.id}> - Too many arguments. Please type !covhelp for help with commands.`);
}
}
// Data HISTORICALLY (This argument returns a GRAPH)
else if (args[0] === "historic" || args[0] === "hs") {
Expand Down Expand Up @@ -113,48 +116,63 @@ module.exports = {

return message.channel.send(historicDeathEmbed);
}
// Country specific historic deaths
else if (args.length == 2) {
let countryName = args.slice(1).join(" ");
console.log(countryName);

let getCountryHistoricDeaths = async () => {
let response = await axios.get("https://corona.lmao.ninja/v2/historical/" + countryName + "?lastdays=30").catch(err =>{
if (err.response){
message.channel.send(`<@${message.author.id}> - Please enter a valid country.`)
}
});
return data = response.data;
}
let historicCountryDeaths = await getCountryHistoricDeaths();

let countryDeathData = [];
let xAxisLabels = [];
// Global deaths historically for a specified number of days
else if (args.length == 2 && typeof(parseFloat(args[1])) === 'number') {
let numDays = args[1];

// Format x-axis labels and compile data to be used on graph
for (day in historicCountryDeaths["timeline"]["deaths"]){
xAxisLabels.push("\"" + day + "\"");
countryDeathData.push(historicCountryDeaths["timeline"]["deaths"][`${day}`])
// Input validation - the number of days must be an integer between 2 and 100, inclusive
if (!Number.isInteger(parseFloat(numDays)))
return message.channel.send(`<@${message.author.id}> - Number of days must be a valid integer.`);
else if (numDays > 100)
return message.channel.send(`<@${message.author.id}> - I can only display data from up to the past 100 days.`);
else if (numDays < 2)
return message.channel.send(`<@${message.author.id}> - The number of days specified must be at least 2.`);
else {
let getDayHistoricDeaths = async () => {
let response = await axios.get("https://corona.lmao.ninja/v2/historical/all?lastdays=" + numDays).catch(err =>{
if (err.response){
message.channel.send(`<@${message.author.id}> - error`)
}
});
return data = response.data;
}
let historicDayDeaths = await getDayHistoricDeaths();

let dayDeathData = [];
let xAxisLabels = [];

// // Format x-axis labels and compile data to be used on graph
// for (day in historicCountryDeaths["timeline"]["deaths"]){
// xAxisLabels.push("\"" + day + "\"");
// countryDeathData.push(historicCountryDeaths["timeline"]["deaths"][`${day}`])
// }

// Format x-axis labels and compile data to be used on graph
for (day in historicDayDeaths["deaths"]){
xAxisLabels.push("\"" + day + "\"");
dayDeathData.push(historicDayDeaths["deaths"][`${day}`])
}

// Create a new embedded message for the bot to display the Country-specific historic deaths
const historicDeathEmbed = new Discord.MessageEmbed()
.setColor("#990000")
.setTitle(`Historic Deaths for the Past ${numDays} Days Globally`)
.setImage(`https://quickchart.io/chart?width=500&height=350&c={type:'line',data:{labels:[${xAxisLabels}],datasets:[{label:'Deaths',data:[${dayDeathData}],fill:false,borderColor:"rgb(178,34,34)",pointBackgroundColor:"rgb(178,34,34)"}]},options:{legend:{labels:{fontColor:"white",fontSize:18}},scales:{yAxes:[{ticks:{fontColor:"white",beginAtZero:false,fontSize:16}}],xAxes:[{ticks:{fontColor:"white",fontSize:16}}]}}}`)

return message.channel.send(historicDeathEmbed);
}

// Create a new embedded message for the bot to display the Country-specific historic deaths
const historicDeathEmbed = new Discord.MessageEmbed()
.setColor("#990000")
.setTitle(`Historic Deaths for the Past 30 Days in ${historicCountryDeaths["country"]}`)
.setImage(`https://quickchart.io/chart?width=500&height=350&c={type:'line',data:{labels:[${xAxisLabels}],datasets:[{label:'Deaths',data:[${countryDeathData}],fill:false,borderColor:"rgb(178,34,34)",pointBackgroundColor:"rgb(178,34,34)"}]},options:{legend:{labels:{fontColor:"white",fontSize:18}},scales:{yAxes:[{ticks:{fontColor:"white",beginAtZero:false,fontSize:16}}],xAxes:[{ticks:{fontColor:"white",fontSize:16}}]}}}`)

return message.channel.send(historicDeathEmbed);
}
}
// Country specific historic deaths for certain days
else if (args.length == 3) {
let countryName = args.slice(1,2).join(" ");
let numDays = args.slice(2).join(" ");
console.log(countryName);
console.log(numDays);
else if (args.length >= 3 && typeof(parseFloat(args[1]) === 'number') && /^[a-zA-Z\s]*$/i.test(args.slice(2).join(" "))) {
let countryName = args.slice(2).join(" ");
let numDays = args[1];

if (numDays > 100) {
if (!Number.isInteger(parseFloat(numDays)) && !isNaN(numDays))
return message.channel.send(`<@${message.author.id}> - Number of days must be a valid integer.`);
else if (numDays > 100)
return message.channel.send(`<@${message.author.id}> - I can only display data from up to the past 100 days.`);
}
else if (numDays < 2)
return message.channel.send(`<@${message.author.id}> - The number of days specified must be at least 2.`);
else {
let getCountryHistoricDeaths = async () => {
let response = await axios.get("https://corona.lmao.ninja/v2/historical/" + countryName + "?lastdays=" + numDays).catch(err =>{
Expand Down Expand Up @@ -184,8 +202,14 @@ module.exports = {
return message.channel.send(historicDeathEmbed);
}
}
else {
return message.channel.send(`<@${message.author.id}> - Command syntax is !deaths historic [number of days] [name of country]`);
}
}
else {
return message.channel.send(`<@${message.author.id}> - Please enter a valid argument. Type !covhelp for help with commands.`);
}
}
}
}

let monthArr = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"];
let numberRegex = /^\d+$/;
3 changes: 3 additions & 0 deletions commands/recovered.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,8 @@ module.exports = {
}
}
}
else {
return message.channel.send(`<@${message.author.id}> - Please enter a valid argument. Type !covhelp for help with commands.`);
}
}
}

0 comments on commit 95bf69b

Please sign in to comment.