From 95bf69b0286829045f49dacd688824ee101187b9 Mon Sep 17 00:00:00 2001 From: Kevin Pierce Date: Mon, 27 Jul 2020 14:09:18 -0400 Subject: [PATCH] Implemented more rigorous input validation for the deaths command --- .DS_Store | Bin 6148 -> 6148 bytes commands/deaths.js | 104 ++++++++++++++++++++++++++---------------- commands/recovered.js | 3 ++ 3 files changed, 67 insertions(+), 40 deletions(-) diff --git a/.DS_Store b/.DS_Store index 787c0e24b3cfd646bdb8d5b66e1b2cfacb8274a6..0f3ed4ea589da2481355a8ec7716b7713dead5f9 100644 GIT binary patch delta 21 ccmZoMXffDupM}H1#8^kc$jo5#a~4@)089`DRR910 delta 21 ccmZoMXffDupM}HH$V5lM$jo^2a~4@)08AnVSO5S3 diff --git a/commands/deaths.js b/commands/deaths.js index 40aae6c..76acc51 100644 --- a/commands/deaths.js +++ b/commands/deaths.js @@ -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 () => { @@ -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") { @@ -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 =>{ @@ -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"]; \ No newline at end of file + let numberRegex = /^\d+$/; \ No newline at end of file diff --git a/commands/recovered.js b/commands/recovered.js index 778960a..be90e33 100644 --- a/commands/recovered.js +++ b/commands/recovered.js @@ -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.`); + } } } \ No newline at end of file