Skip to content

Commit

Permalink
Resolved leaderboard embed - will now correctly display all 3 major s…
Browse files Browse the repository at this point in the history
…tats for each country with formatted display
  • Loading branch information
kevin-pierce committed Jul 26, 2020
1 parent 80b93c1 commit 0da47ff
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions commands/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ module.exports = {
description: "Provides a top-10 ranking for each Country in each respective category",
async execute(message, args){

let leadingDeaths = [];
let leadingRecoveries = [];
let leadingCases = [];

if (!args.length){

let leadingDeaths = [];
let leadingRecoveries = [];
let leadingCases = [];

let getCovData = async () => {
let response = await axios.get("https://corona.lmao.ninja/v2/countries?yesterday&sort");
let data = response.data;
Expand All @@ -29,25 +30,25 @@ module.exports = {
leadingRecoveries.sort(compareRecoveries);
leadingCases.sort(compareCases);

// Create the formatted embedded message
const leadingEmbed = new Discord.MessageEmbed()
.setColor("#990000")
.setTitle("Global COVID-19 Stats")
.addField("Most Deaths", formatData(leadingDeaths), true);

// for (let countryNum = 0; countryNum < 9; countryNum++){
// `${leadingDeaths[countryNum]["country"]}:${leadingDeaths[countryNum]["deaths"]}`, true);
// }
.addField("Most Cases", formatData(leadingCases, "cases"), true)
.addField("Most Deaths", formatData(leadingDeaths, "deaths"), true)
.addField("Most Recoveries", formatData(leadingRecoveries, "recovered"), true);

return message.channel.send(leadingEmbed);
}
}
}

let formatData = (arr) => {
// Compile each value line shown in the Field
let formatData = (arr, typeStr) => {
let msg = ``;

for (let countryNum = 0; countryNum < 10; countryNum++){
msg += `${countryNum + 1}: ${arr[countryNum]["country"]} :flag_${arr[countryNum]["countryInfo"]["iso2"].toLowerCase()}:: ${arr[countryNum]["deaths"]}\n`
msg += `${countryNum + 1}: ${arr[countryNum]["country"]} :flag_${arr[countryNum]["countryInfo"]["iso2"].toLowerCase()}:: ${numberWithCommas(arr[countryNum][typeStr])} \n`
}
return msg;
}
Expand Down Expand Up @@ -92,4 +93,9 @@ let compareCases = (a, b) => {
comparison = 1;
}
return comparison;
}
}

// Helper function that adds commas to large numbers using REGEX
let numberWithCommas = (x) => {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

0 comments on commit 0da47ff

Please sign in to comment.