Skip to content

Commit

Permalink
Clean and centralize leadingZeros function
Browse files Browse the repository at this point in the history
Also fix usage month bug
  • Loading branch information
Glazelf committed May 13, 2024
1 parent f5edb5f commit 6ecf522
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
18 changes: 8 additions & 10 deletions commands/api/pokemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports.run = async (client, interaction, logger, ephemeral = true) => {
const getPokemon = require('../../util/pokemon/getPokemon');
const getTypeEmotes = require('../../util/pokemon/getTypeEmotes');
const capitalizeString = require('../../util/capitalizeString');
const leadingZeros = require('../../util/leadingZeros');
const learnsets = require('../../node_modules/pokemon-showdown/dist/data/learnsets.js').Learnsets;
const checkBaseSpeciesMoves = require('../../util/pokemon/checkBaseSpeciesMoves');
const isAdmin = require('../../util/isAdmin');
Expand Down Expand Up @@ -252,7 +253,6 @@ exports.run = async (client, interaction, logger, ephemeral = true) => {
} else return sendMessage({ client: client, interaction: interaction, content: `I could not find a learnset for ${pokemon.name}.` });
pokemonEmbed.setTitle(learnAuthor);
if (learnInfo.length > 0) pokemonEmbed.setDescription(learnInfo);
return sendMessage({ client: client, interaction: interaction, embeds: pokemonEmbed, ephemeral: ephemeral });
break;
case "usage":
let formatInput = "gen9vgc2023series1";
Expand All @@ -275,7 +275,8 @@ exports.run = async (client, interaction, logger, ephemeral = true) => {
if (!month) {
month = date.getMonth();
try {
let testMonth = await axios.get(`https://www.smogon.com/stats/${year}-${month}/`);
let testStringMonth = leadingZeros(month, 2);
let testMonth = await axios.get(`https://www.smogon.com/stats/${year}-${testStringMonth}/`);
} catch (e) {
month = month - 1;
};
Expand All @@ -284,8 +285,7 @@ exports.run = async (client, interaction, logger, ephemeral = true) => {
month += 12;
year -= 1;
};
let stringMonth = month;
if (stringMonth < 10) stringMonth = "0" + stringMonth;
let stringMonth = leadingZeros(month, 2);
// Format URL and other variables
let searchURL = `https://www.smogon.com/stats/${year}-${stringMonth}/moveset/${formatInput}-${rating}.txt`;
let response = null;
Expand All @@ -312,8 +312,6 @@ exports.run = async (client, interaction, logger, ephemeral = true) => {
let usageRank = 0;
let genericDataSplitPokemon = null;
let pokemonDataSplitLine = null;
let usageEmbed = new Discord.EmbedBuilder()
.setColor(client.globalVars.embedColor);
if (pokemonName) {
let usagePokemonString = usageArray.find(element => element.startsWith(pokemonName + " ")); // space is to exclude matching more popular subforms
if (!usagePokemonString) return sendMessage({ client: client, interaction: interaction, content: `Could not find any data for ${pokemonName} in ${formatInput} during the specified month.`, components: usageButtons });
Expand All @@ -331,7 +329,7 @@ exports.run = async (client, interaction, logger, ephemeral = true) => {
let movesString = usagePokemonString.split("Moves")[1].split("Teammates")[0].split("%").map(function (x) { return x.trim(); }).join("%\n").replaceAll(" ", "");
let teammatesString = usagePokemonString.split("Teammates")[1].split("Checks and Counters")[0].split("%").map(function (x) { return x.trim(); }).join("%\n").replaceAll(" ", "");
let countersString = usagePokemonString.split("Checks and Counters")[1].split("out)").map(function (x) { return x.trim(); }).join("out)\n").replaceAll(" ", "");
usageEmbed
pokemonEmbed
.setTitle(`${pokemonName} ${formatInput} ${rating}+ (${stringMonth}/${year})`)
.setDescription(`#${usageRank} | ${usagePercentage} | ${rawUsage} uses`)
.addFields([
Expand All @@ -341,7 +339,7 @@ exports.run = async (client, interaction, logger, ephemeral = true) => {
{ name: "Spreads:", value: spreadsString, inline: true },
{ name: "Teammates:", value: teammatesString, inline: true }
]);
if (countersString.length > 0) usageEmbed.addFields([{ name: "Checks and Counters:", value: countersString, inline: true }]);
if (countersString.length > 0) pokemonEmbed.addFields([{ name: "Checks and Counters:", value: countersString, inline: true }]);
} else {
// Format generic data display
let usageList = [];
Expand All @@ -358,14 +356,14 @@ exports.run = async (client, interaction, logger, ephemeral = true) => {
let usageListPart1 = [];
let usageListPart2 = [];
await usageList.forEach(element => { if (usageListPart1.length < 50) usageListPart1.push(element); else if (usageListPart2.length < 50) usageListPart2.push(element) });
usageEmbed
pokemonEmbed
.setTitle(`Usage for ${formatInput} ${rating}+ (${stringMonth}/${year})`)
.addFields([
{ name: "1-50", value: usageListPart1.join("\n"), inline: true },
{ name: "51-100", value: usageListPart2.join("\n"), inline: true }
]);
};
return sendMessage({ client: client, interaction: interaction, embeds: usageEmbed, ephemeral: ephemeral });
break;
};
// Bulbapedia button
if (linkBulbapedia) pokemonButtons.addComponents(new Discord.ButtonBuilder({ label: 'More info', style: Discord.ButtonStyle.Link, url: linkBulbapedia }));
Expand Down
6 changes: 6 additions & 0 deletions util/leadingZeros.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = (str, characters) => {
for (let i = str.toString().length; i < characters; i++) {
str = "0" + str;
};
return str;
};
7 changes: 1 addition & 6 deletions util/pokemon/getPokemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = async ({ client, interaction, pokemon, learnsetBool = false, sh
const imageExists = require('../imageExists');
const isAdmin = require('../isAdmin');
const convertMeterFeet = require('../convertMeterFeet');
const leadingZeros = require('../leadingZeros');
const correctionID = require('../../objects/pokemon/correctionID.json');
const colorHexes = require('../../objects/colorHexes.json');
const typechart = require('../../node_modules/pokemon-showdown/dist/data/typechart.js').TypeChart;
Expand Down Expand Up @@ -462,12 +463,6 @@ module.exports = async ({ client, interaction, pokemon, learnsetBool = false, sh
let StatText = `(${min50}-${max50}) (${min100}-${max100})`;
return StatText;
};
function leadingZeros(str, characters) {
for (let i = str.length; i < characters; i++) {
str = "0" + str;
};
return str;
};
function correctValue(object, key, input) {
key = key.toLowerCase();
if (object[key]) return object[key];
Expand Down

0 comments on commit 6ecf522

Please sign in to comment.