Skip to content

Commit

Permalink
chore: Buncha cleanup/ de-nesting stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jmiln committed Mar 5, 2024
1 parent 0f093e4 commit 9976d3d
Show file tree
Hide file tree
Showing 20 changed files with 358 additions and 464 deletions.
6 changes: 4 additions & 2 deletions slash/acronyms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const Command = require("../base/slashCommand");
const { ApplicationCommandOptionType } = require("discord.js");

const usageExample = "/acronyms acronym:CLS";

/**
* The list of acronyms defined in data/acronym.json was transposed from the SWGoH forum. Any update there will need to make it's way into here.
* //https://forums.galaxy-of-heroes.starwars.ea.com/discussion/154048/guide-to-the-acronyms-and-terms-of-star-wars-galaxy-of-heroes-swgoh
Expand Down Expand Up @@ -29,7 +31,7 @@ class Acronyms extends Command {

if (!acronym?.length) {
// Apparently this should never happen because it's set as a required argument, but who knows/ just in case
return super.error(interaction, interaction.language.get("COMMAND_ACRONYMS_INVALID"), {example: "acronym cls"});
return super.error(interaction, interaction.language.get("COMMAND_ACRONYMS_INVALID"), {example: usageExample});
}

// Split it up in case they're looking for more than one
Expand All @@ -40,7 +42,7 @@ class Acronyms extends Command {

if (!matchingItems.length) {
// If there were no matches, go ahead and let the user know
return super.error(interaction, interaction.language.get("COMMAND_ACRONYMS_NOT_FOUND"), {example: "acronym cls"});
return super.error(interaction, interaction.language.get("COMMAND_ACRONYMS_NOT_FOUND"), {example: usageExample});
}

let acronymMeaningMessage = "";
Expand Down
23 changes: 10 additions & 13 deletions slash/arenawatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,12 +763,11 @@ class ArenaWatch extends Command {
for (let code of codesIn) {
code = code.replace(/[^\d]/g, "");
code = parseInt(code, 10);
if (aw.allycodes.find(ac => ac.allyCode === code)) {
aw.allycodes = aw.allycodes.filter(ac => ac.allyCode !== code);
outLog.push(code + " has been removed");
} else {
return super.error(interaction, "That ally code was not available to be removed");
}
const codes = aw.allycodes.filter(ac => ac.allyCode !== code);
if (!codes?.length) return super.error(interaction, "That ally code was not available to be removed");

aw.allycodes = codes;
outLog.push(code + " has been removed");
}
}
break;
Expand Down Expand Up @@ -867,15 +866,13 @@ class ArenaWatch extends Command {
})
// Then format the output strings
.map(a => {
if (!view_by) {
const isWarn = a?.warn?.min && a.warn?.arena ? "W": "";
const isRes = a.result ? "R" : "";
const tags = isWarn.length || isRes.length ? `\`[${isWarn}${isRes}]\`` : "";
return `\`${a.allyCode}\` ${tags} ${a.mark ? a.mark + " " : ""}**${a.mention ? `<@${a.mention}>` : a.name}**`;
} else {
// Current rank
if (view_by) {
return `\`${((view_by === "char_rank" ? a.lastChar : a.lastShip) || "N/A").toString().padStart(3)}\` | ${a.mark ? a.mark + " " : ""}**${a.mention ? `<@${a.mention}>` : a.name}**`;
}
const isWarn = a?.warn?.min && a.warn?.arena ? "W": "";
const isRes = a.result ? "R" : "";
const tags = isWarn.length || isRes.length ? `\`[${isWarn}${isRes}]\`` : "";
return `\`${a.allyCode}\` ${tags} ${a.mark ? a.mark + " " : ""}**${a.mention ? `<@${a.mention}>` : a.name}**`;
});

const fields = [];
Expand Down
373 changes: 143 additions & 230 deletions slash/event.js

Large diffs are not rendered by default.

121 changes: 60 additions & 61 deletions slash/faction.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,76 +110,75 @@ class Faction extends Command {
});
}
}
if (allycode) {
if (chars.length) {
chars = chars.map(c => c.baseId);
const cooldown = await Bot.getPlayerCooldown(interaction.user.id, interaction?.guild?.id);
let player;
try {
player = await Bot.swgohAPI.unitStats(allycode, cooldown);
if (Array.isArray(player)) player = player[0];
} catch (e) {
return super.error(interaction, e.message);
}
if (!player?.roster?.length) {
return super.error(interaction, "I couldn't get that player's roster. Please try again later.");
}
const playerChars = [];
for (const c of chars) {
let found = player.roster.find(char => char.defId === c);
if (found) {
found = await Bot.swgohAPI.langChar(found, interaction.guildSettings.swgohLanguage);
found.gp = found.gp.toLocaleString();
playerChars.push(found);
}
if (!allycode) {
return interaction.reply({embeds: [{
author: {
name: "Matches for " + Bot.toProperCase(searchName) + extra
},
description: chars.map(c => c.nameKey).join("\n")
}]});
}
if (chars.length) {
chars = chars.map(c => c.baseId);
const cooldown = await Bot.getPlayerCooldown(interaction.user.id, interaction?.guild?.id);
let player;
try {
player = await Bot.swgohAPI.unitStats(allycode, cooldown);
if (Array.isArray(player)) player = player[0];
} catch (e) {
return super.error(interaction, e.message);
}
if (!player?.roster?.length) {
return super.error(interaction, "I couldn't get that player's roster. Please try again later.");
}
const playerChars = [];
for (const c of chars) {
let found = player.roster.find(char => char.defId === c);
if (found) {
found = await Bot.swgohAPI.langChar(found, interaction.guildSettings.swgohLanguage);
found.gp = found.gp.toLocaleString();
playerChars.push(found);
}
}

const gpMax = Math.max(...playerChars.map(c => c.gp.length));
const gearMax = Math.max(...playerChars.map(c => c.gear.toString().length));
const lvlMax = Math.max(...playerChars.map(c => c.level.toString().length));

factionChars.push(`**\`[ * | Lvl${" ".repeat(lvlMax)}| GP ${" ".repeat((gpMax > 5 ? 6 : gpMax) -5)}| ⚙${" ".repeat(gearMax)}]\`**`);
factionChars.push("**`=================" + "=".repeat(lvlMax + gpMax + gearMax) + "`**");

playerChars.forEach(c => {
const lvlStr = " ".repeat(lvlMax - c.level.toString().length) + c.level;
const gpStr = " ".repeat(gpMax - c.gp.length) + c.gp;
const gearStr = " ".repeat(gearMax - c.gear.toString().length) + c.gear;
const zetas = "z".repeat(c.skills.filter(s => (s.isZeta && s.tier === s.tiers) || (s.isOmicron && s.tier >= s.tiers-1)).length);
factionChars.push(`**\`[ ${c.rarity} | ${lvlStr} | ${gpStr} | ${gearStr} ]\` ${zetas}${c.nameKey}**`);
});
const msgArr = Bot.msgArray(factionChars, "\n", 1000);
const fields = [];
let desc;
if (msgArr.length > 1) {
msgArr.forEach((m, ix) => {
fields.push({
name: `${ix+1}`,
value: m
});
const gpMax = Math.max(...playerChars.map(c => c.gp.length));
const gearMax = Math.max(...playerChars.map(c => c.gear.toString().length));
const lvlMax = Math.max(...playerChars.map(c => c.level.toString().length));

factionChars.push(`**\`[ * | Lvl${" ".repeat(lvlMax)}| GP ${" ".repeat((gpMax > 5 ? 6 : gpMax) -5)}| ⚙${" ".repeat(gearMax)}]\`**`);
factionChars.push("**`=================" + "=".repeat(lvlMax + gpMax + gearMax) + "`**");

playerChars.forEach(c => {
const lvlStr = " ".repeat(lvlMax - c.level.toString().length) + c.level;
const gpStr = " ".repeat(gpMax - c.gp.length) + c.gp;
const gearStr = " ".repeat(gearMax - c.gear.toString().length) + c.gear;
const zetas = "z".repeat(c.skills.filter(s => (s.isZeta && s.tier === s.tiers) || (s.isOmicron && s.tier >= s.tiers-1)).length);
factionChars.push(`**\`[ ${c.rarity} | ${lvlStr} | ${gpStr} | ${gearStr} ]\` ${zetas}${c.nameKey}**`);
});
const msgArr = Bot.msgArray(factionChars, "\n", 1000);
const fields = [];
let desc;
if (msgArr.length > 1) {
msgArr.forEach((m, ix) => {
fields.push({
name: `${ix+1}`,
value: m
});
} else {
desc = msgArr[0];
}

const footerStr = Bot.updatedFooterStr(player.updated, interaction);
return interaction.reply({embeds: [{
author: {
name: player.name + "'s matches for " + Bot.toProperCase(searchName) + extra
},
description: desc,
fields: [...fields, {name: Bot.constants.zws, value: footerStr}]
}]});
});
} else {
return super.error(interaction, interaction.language.get("COMMAND_FACTION_USAGE"), {title: interaction.language.get("COMMAND_FACTION_INVALID_FACTION"), example: "faction sith"});
desc = msgArr[0];
}
} else {

const footerStr = Bot.updatedFooterStr(player.updated, interaction);
return interaction.reply({embeds: [{
author: {
name: "Matches for " + Bot.toProperCase(searchName) + extra
name: player.name + "'s matches for " + Bot.toProperCase(searchName) + extra
},
description: chars.map(c => c.nameKey).join("\n")
description: desc,
fields: [...fields, {name: Bot.constants.zws, value: footerStr}]
}]});
} else {
return super.error(interaction, interaction.language.get("COMMAND_FACTION_USAGE"), {title: interaction.language.get("COMMAND_FACTION_INVALID_FACTION"), example: "faction sith"});
}
}
}
Expand Down
67 changes: 34 additions & 33 deletions slash/farm.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,44 @@ class Farm extends Command {
}


if (unitLocs) {
for (const loc of unitLocs.locations) {
if (loc.cost) {
// This will be anything in a store
outList.push( `${loc.type} \n * ${loc.cost.split("\n").map(cost => cost.replace("/", " per ")).join(" shards\n * ")} shards`);
} else if (loc.level) {
// It's a node, fleet, cantina, light/ dark side
if (loc.locId) {
const langLoc = await Bot.cache.getOne(Bot.config.mongodb.swapidb, "locations", {id: loc.locId, language: interaction.swgohLanguage.toLowerCase()});
if (!unitLocs) {
return super.error(interaction, `I couldn't get the location data for *${character.name}*`);
}
for (const loc of unitLocs.locations) {
if (loc.cost) {
// This will be anything in a store
outList.push( `${loc.type} \n * ${loc.cost.split("\n").map(cost => cost.replace("/", " per ")).join(" shards\n * ")} shards`);
} else if (loc.level) {
// It's a node, fleet, cantina, light/ dark side
if (loc.locId) {
const langLoc = await Bot.cache.getOne(Bot.config.mongodb.swapidb, "locations", {id: loc.locId, language: interaction.swgohLanguage.toLowerCase()});

// If it's a proving grounds event, stick the unit name after
if (loc.locId === "EVENT_CONQUEST_UNIT_TRIALS_NAME") {
outList.push(Bot.toProperCase(langLoc.langKey) + " - " + character.name);
} else {
outList.push(Bot.toProperCase(langLoc?.langKey) + " " + loc.level);
}
// If it's a proving grounds event, stick the unit name after
if (loc.locId === "EVENT_CONQUEST_UNIT_TRIALS_NAME") {
outList.push(Bot.toProperCase(langLoc.langKey) + " - " + character.name);
} else {
loc.type = loc.type.replace("Hard Modes (", "").replace(")", "");
if (loc.type === "L") {
outList.push(`Light Side Hard ${loc.level}`);
} else if (loc.type === "D") {
outList.push(`Dark Side Hard ${loc.level}`);
} else if (loc.type === "Fleet") {
outList.push(`Fleet Hard ${loc.level}`);
} else if (loc.type === "Cantina") {
outList.push(`Cantina ${loc.level}`);
}
outList.push(Bot.toProperCase(langLoc?.langKey) + " " + loc.level);
}
} else {
loc.type = loc.type.replace("Hard Modes (", "").replace(")", "");
if (loc.type === "L") {
outList.push(`Light Side Hard ${loc.level}`);
} else if (loc.type === "D") {
outList.push(`Dark Side Hard ${loc.level}`);
} else if (loc.type === "Fleet") {
outList.push(`Fleet Hard ${loc.level}`);
} else if (loc.type === "Cantina") {
outList.push(`Cantina ${loc.level}`);
}
} else if (loc.name) {
// This will be any of the events
outList.push(Bot.expandSpaces(`__${loc.type}__: ${loc.name}`));
} else if (loc.locId) {
// Just has the location id, so probably a marquee
const langLoc = await Bot.cache.getOne(Bot.config.mongodb.swapidb, "locations", {id: loc.locId, language: interaction.swgohLanguage.toLowerCase()});
if (!langLoc) continue;
outList.push(Bot.toProperCase(langLoc?.langKey));
}
} else if (loc.name) {
// This will be any of the events
outList.push(Bot.expandSpaces(`__${loc.type}__: ${loc.name}`));
} else if (loc.locId) {
// Just has the location id, so probably a marquee
const langLoc = await Bot.cache.getOne(Bot.config.mongodb.swapidb, "locations", {id: loc.locId, language: interaction.swgohLanguage.toLowerCase()});
if (!langLoc) continue;
outList.push(Bot.toProperCase(langLoc?.langKey));
}
}
if (!outList.length) {
Expand Down
28 changes: 13 additions & 15 deletions slash/grandarena.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,21 +431,19 @@ class GrandArena extends Command {
const len = 18;
const checkLen = Object.keys(checkArr).length;
Object.keys(checkArr).forEach((c, ix) => {
if (checkLen <= 21 || ix < 21) {
let halfLen = (len - c.length) / 2;
if (halfLen < 0) halfLen = 0;
fields.push({
name: "=".repeat(halfLen) + " " + c + " " + "=".repeat(halfLen),
value: Bot.codeBlock(Bot.makeTable({
check: {value: "", align: "left", endWith: "::"},
user1: {value: "", align: "right"},
user2: {value: "", align: "left"}
}, checkArr[c], {useHeader: false}).map(e => e.replace(" ::", "::")).join("\n"), "asciiDoc"),
inline: true
});
} else {
extra++;
}
if (checkLen > 21 && ix >= 21) extra++;

let halfLen = (len - c.length) / 2;
if (halfLen < 0) halfLen = 0;
fields.push({
name: "=".repeat(halfLen) + " " + c + " " + "=".repeat(halfLen),
value: Bot.codeBlock(Bot.makeTable({
check: {value: "", align: "left", endWith: "::"},
user1: {value: "", align: "right"},
user2: {value: "", align: "left"}
}, checkArr[c], {useHeader: false}).map(e => e.replace(" ::", "::")).join("\n"), "asciiDoc"),
inline: true
});
});
if (extra > 0) {
fields.push({
Expand Down
3 changes: 2 additions & 1 deletion slash/guilds.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ class Guilds extends Command {
mods.spd20 += 1;
} else {
mods.spd15 += 1;
} }
}
}
if (off) mods.off100 += 1;
});
}
Expand Down
3 changes: 2 additions & 1 deletion slash/myprofile.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class MyProfile extends Command {
mods.spd20 += 1;
} else {
mods.spd15 += 1;
} }
}
}
if (off) mods.off100 += 1;
});
}
Expand Down
6 changes: 3 additions & 3 deletions slash/need.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ class Need extends Command {
return super.error(interaction, "I could not find a valid ally code for you. Please make sure to supply one.");
}

const battle = interaction.options.getString("battle");
const battle = interaction.options.getString("battle");
const faction1 = interaction.options.getString("faction_group_1");
const faction2 = interaction.options.getString("faction_group_2");
const keyword = interaction.options.getString("keyword");
const shop = interaction.options.getString("shop");
const keyword = interaction.options.getString("keyword");
const shop = interaction.options.getString("shop");

if (!battle && !faction1 && !faction2 && !keyword && !shop) {
return super.error(interaction, "You need to specify a location or faction.");
Expand Down
9 changes: 4 additions & 5 deletions slash/poll.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,11 @@ class Poll extends Command {
content: interaction.language.get("COMMAND_POLL_CHANGED_OPT", oldPoll.options[voted], oldPoll.options[opt]),
ephemeral: true
});
} else {
return interaction.reply({
content: interaction.language.get("COMMAND_POLL_REGISTERED", oldPoll.options[opt]),
ephemeral: true
});
}
return interaction.reply({
content: interaction.language.get("COMMAND_POLL_REGISTERED", oldPoll.options[opt]),
ephemeral: true
});
} catch (err) {
return console.error(`[/poll vote] Error voting: ${err}`);
}
Expand Down
Loading

0 comments on commit 9976d3d

Please sign in to comment.