Skip to content

Commit

Permalink
reafactor: Change my codeblock formatter to Discord.js' built-in one
Browse files Browse the repository at this point in the history
  • Loading branch information
jmiln committed Mar 19, 2024
1 parent 33cd13d commit 17bf0c2
Show file tree
Hide file tree
Showing 24 changed files with 121 additions and 119 deletions.
4 changes: 3 additions & 1 deletion base/slashCommand.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { codeBlock } = require("discord.js");

const defCmdData = {
name: "",
description: "No description provided.",
Expand Down Expand Up @@ -50,7 +52,7 @@ class slashCommand {
options.title = options.title || "Error";
options.color = options.color || this.Bot.constants.colors.red;
if (options.example) {
errMsg += `\n\n**Example:**${this.Bot.codeBlock(options.example)}`;
errMsg += `\n\n**Example:**${codeBlock(options.example)}`;
}
await this.embed(interaction, errMsg, options);
}
Expand Down
11 changes: 2 additions & 9 deletions modules/functions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { WebhookClient, ChannelType, PermissionsBitField } = require("discord.js");
const { WebhookClient, ChannelType, PermissionsBitField, time } = require("discord.js");
const {promisify, inspect} = require("util"); // eslint-disable-line no-unused-vars
const fs = require("fs");
const readdir = promisify(require("fs").readdir);
Expand Down Expand Up @@ -356,13 +356,6 @@ module.exports = (Bot, client) => {
return messages;
};

/* CODE BLOCK MAKER
* Makes a codeblock with the specified lang for highlighting.
*/
Bot.codeBlock = (str, lang="") => {
return `\`\`\`${lang}\n${str}\`\`\``;
};

// Return a duration string
Bot.duration = (time, interaction=null) => {
const lang = interaction ? interaction.language : Bot.languages[Bot.config.defaultSettings.language];
Expand Down Expand Up @@ -487,7 +480,7 @@ module.exports = (Bot, client) => {

const lang = interaction?.language || Bot.languages["eng_us"];

return lang.get("BASE_SWGOH_LAST_UPDATED", `<t:${Math.floor(updated/1000)}:R>`);
return lang.get("BASE_SWGOH_LAST_UPDATED", time(Math.floor(updated/1000)));
};

// Get the current user count
Expand Down
5 changes: 4 additions & 1 deletion modules/swapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,8 @@ module.exports = (opts={}) => {
if (!opts) opts = {};
if (!skillArray) {
throw new Error("You need to have a list of abilities here");
} else if (!Array.isArray(skillArray)) {
}
if (!Array.isArray(skillArray)) {
skillArray = [skillArray];
}

Expand Down Expand Up @@ -685,6 +686,8 @@ module.exports = (opts={}) => {
skill = skill[0];
}
if (!skill) {
console.error("[swapi getCharacter] Missing ability - ");
console.error(s);
throw new Error("Missing character ability");
}
s.isZeta = skill.isZeta;
Expand Down
16 changes: 8 additions & 8 deletions slash/activities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ApplicationCommandOptionType } = require("discord.js");
const { ApplicationCommandOptionType, codeBlock } = require("discord.js");
const Command = require("../base/slashCommand");

class Activites extends Command {
Expand Down Expand Up @@ -60,19 +60,19 @@ class Activites extends Command {

switch (day) {
case "day_Sunday":
return interaction.reply({content: Bot.codeBlock(interaction.language.get("COMMAND_ACTIVITIES_SUNDAY"), "asciidoc")});
return interaction.reply({content: codeBlock("asciiDoc", interaction.language.get("COMMAND_ACTIVITIES_SUNDAY"))});
case "day_Monday":
return interaction.reply({content: Bot.codeBlock(interaction.language.get("COMMAND_ACTIVITIES_MONDAY"), "asciidoc")});
return interaction.reply({content: codeBlock("asciiDoc", interaction.language.get("COMMAND_ACTIVITIES_MONDAY"))});
case "day_Tuesday":
return interaction.reply({content: Bot.codeBlock(interaction.language.get("COMMAND_ACTIVITIES_TUESDAY"), "asciidoc")});
return interaction.reply({content: codeBlock("asciiDoc", interaction.language.get("COMMAND_ACTIVITIES_TUESDAY"))});
case "day_Wednesday":
return interaction.reply({content: Bot.codeBlock(interaction.language.get("COMMAND_ACTIVITIES_WEDNESDAY"), "asciidoc")});
return interaction.reply({content: codeBlock("asciiDoc", interaction.language.get("COMMAND_ACTIVITIES_WEDNESDAY"))});
case "day_Thursday":
return interaction.reply({content: Bot.codeBlock(interaction.language.get("COMMAND_ACTIVITIES_THURSDAY"), "asciidoc")});
return interaction.reply({content: codeBlock("asciiDoc", interaction.language.get("COMMAND_ACTIVITIES_THURSDAY"))});
case "day_Friday":
return interaction.reply({content: Bot.codeBlock(interaction.language.get("COMMAND_ACTIVITIES_FRIDAY"), "asciidoc")});
return interaction.reply({content: codeBlock("asciiDoc", interaction.language.get("COMMAND_ACTIVITIES_FRIDAY"))});
case "day_Saturday":
return interaction.reply({content: Bot.codeBlock(interaction.language.get("COMMAND_ACTIVITIES_SATURDAY"), "asciidoc")});
return interaction.reply({content: codeBlock("asciiDoc", interaction.language.get("COMMAND_ACTIVITIES_SATURDAY"))});
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions slash/botshards.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Status } = require("discord.js");
const { Status, codeBlock } = require("discord.js");
const Command = require("../base/slashCommand");

class BotShards extends Command {
Expand Down Expand Up @@ -28,7 +28,7 @@ class BotShards extends Command {
`${ping} ms`
].join(" | "));
});
interaction.reply({ content: Bot.codeBlock(shardInfoArr.join("\n"), "prolog") });
interaction.reply({ content: codeBlock("prolog", shardInfoArr.join("\n")) });
})
.catch((error) => {
console.error(error);
Expand Down
4 changes: 2 additions & 2 deletions slash/challenges.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Command = require("../base/slashCommand");
const { ApplicationCommandOptionType } = require("discord.js");
const { ApplicationCommandOptionType, codeBlock } = require("discord.js");

class Challenges extends Command {
constructor(Bot) {
Expand Down Expand Up @@ -100,7 +100,7 @@ class Challenges extends Command {
return sendDay("Saturday");
}
function sendDay(day) {
return interaction.reply({content: Bot.codeBlock(dayString(day), "asciidoc")});
return interaction.reply({content: codeBlock("asciiDoc", dayString(day))});
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions slash/character.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ class Character extends Command {
}

const character = chars[0];

const char = await Bot.swgohAPI.getCharacter(character.uniqueName, interaction.guildSettings.swgohLanguage);

let char;
try {
char = await Bot.swgohAPI.getCharacter(character.uniqueName, interaction.guildSettings.swgohLanguage);
} catch (err) {
return super.error(interaction, err.toString());
}
const fields = [];

if (char.factions.length) {
Expand Down
6 changes: 3 additions & 3 deletions slash/charactergear.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Command = require("../base/slashCommand");
const { ApplicationCommandOptionType } = require("discord.js");
const { ApplicationCommandOptionType, codeBlock } = require("discord.js");

class Charactergear extends Command {
constructor(Bot) {
Expand Down Expand Up @@ -112,9 +112,9 @@ class Charactergear extends Command {
const msgArr = Bot.msgArray(interaction.language.get("COMMAND_CHARACTERGEAR_GEAR_ALL", character.name, gearString), "\n", 1900);
for (const [ix, msg] of msgArr.entries()) {
if (ix === 0) {
await interaction.reply({content: Bot.codeBlock(msg, "md")});
await interaction.reply({content: codeBlock("md", msg)});
} else if (ix > 0) {
await interaction.followUp({content: Bot.codeBlock(msg, "md")});
await interaction.followUp({content: codeBlock("md", msg)});
}
}
} else {
Expand Down
8 changes: 4 additions & 4 deletions slash/event.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ApplicationCommandOptionType } = require("discord.js");
const { ApplicationCommandOptionType, codeBlock } = require("discord.js");

const Command = require("../base/slashCommand");
const {getGuildEvents, updateGuildEvent} = require("../modules/guildConfig/events.js");
Expand Down Expand Up @@ -294,7 +294,7 @@ class Event extends Command {
try {
jsonWhole = JSON.parse(match[0]);
} catch (e) {
return super.error(interaction, "**ERROR Parsing the json**" + Bot.codeBlock(e.message));
return super.error(interaction, "**ERROR Parsing the json**" + codeBlock(e.message));
}

// ```[{
Expand All @@ -312,7 +312,7 @@ class Event extends Command {
const guildEvents = await getGuildEvents({cache: Bot.cache, guildId: interaction.guild.id});
const result = validateEvents(jsonWhole, guildEvents);
if (result.filter(e => !e.valid).length) {
return interaction.reply({content: interaction.language.get("COMMAND_EVENT_JSON_ERR_NOT_ADDED", Bot.codeBlock(result.map(e => e.str).join("\n\n")))});
return interaction.reply({content: interaction.language.get("COMMAND_EVENT_JSON_ERR_NOT_ADDED", codeBlock(result.map(e => e.str).join("\n\n")))});
}

// If there were no errors in the setup, go ahead and add all the events in, then tell em as such
Expand Down Expand Up @@ -519,7 +519,7 @@ class Event extends Command {
}
return super.error(interaction,
"**__UPDATED:__**\n" + outLog.map(e => `- ${e}`).join("\n"),
// `Updated event from ${Bot.codeBlock(JSON.stringify(event, null, 2))}to ${Bot.codeBlock(JSON.stringify(validEvent.event, null, 2))}`,
// `Updated event from ${codeBlock(JSON.stringify(event, null, 2))}to ${codeBlock(JSON.stringify(validEvent.event, null, 2))}`,
{title: "Success", color: Bot.constants.colors.green}
);
} else {
Expand Down
36 changes: 18 additions & 18 deletions slash/grandarena.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Command = require("../base/slashCommand");
const { ApplicationCommandOptionType } = require("discord.js");
const { ApplicationCommandOptionType, codeBlock } = require("discord.js");

// Quick mapping of gp to how many teams are needed
const gpMap = {
Expand Down Expand Up @@ -120,7 +120,7 @@ class GrandArena extends Command {
}
if (problemArr.length) {
// Otherwise, spit out the list of issues
return super.error(interaction, Bot.codeBlock(problemArr.map(p => "* " + p).join("\n")));
return super.error(interaction, codeBlock(problemArr.map(p => "* " + p).join("\n")));
}

// If there are no problems, continue
Expand Down Expand Up @@ -211,11 +211,11 @@ class GrandArena extends Command {
});


gpStats = Bot.codeBlock(Bot.makeTable({
gpStats = codeBlock("asciiDoc", Bot.makeTable({
check: {value: "", align: "left", endWith: "::"},
user1: {value: "", endWith: "vs", align: "right"},
user2: {value: "", align: "left"}
}, gpStats, {useHeader: false}).join("\n"), "asciiDoc");
}, gpStats, {useHeader: false}).join("\n"));

fields.push({
name: "GP Stats Overview",
Expand All @@ -240,11 +240,11 @@ class GrandArena extends Command {
user1: u1AvgGear,
user2: u2AvgGear
});
gearOverview = Bot.codeBlock(Bot.makeTable({
gearOverview = codeBlock("asciiDoc", Bot.makeTable({
check: {value: "", align: "left", endWith: "::"},
user1: {value: "", endWith: "vs", align: "right"},
user2: {value: "", align: "left"}
}, gearOverview, {useHeader: false}).join("\n"), "asciiDoc");
}, gearOverview, {useHeader: false}).join("\n"));
fields.push({
name: "Character Gear Counts",
value: "*How many characters at each gear level*" + gearOverview
Expand All @@ -268,11 +268,11 @@ class GrandArena extends Command {
user1: u1AvgRarity,
user2: u2AvgRarity
});
rarityOverview = Bot.codeBlock(Bot.makeTable({
rarityOverview = codeBlock("asciiDoc", Bot.makeTable({
check: {value: "", align: "left", endWith: "::"},
user1: {value: "", endWith: "vs", align: "right"},
user2: {value: "", align: "left"}
}, rarityOverview, {useHeader: false}).join("\n"), "asciiDoc");
}, rarityOverview, {useHeader: false}).join("\n"));

fields.push({
name: "Character Rarity Counts",
Expand Down Expand Up @@ -300,11 +300,11 @@ class GrandArena extends Command {
user2: `${getGearStr(u2Char)}${u2Char?.purchasedAbilityId?.length > 0 ? "U" : ""}`
});
}
glOverview = Bot.codeBlock(Bot.makeTable({
glOverview = codeBlock("asciiDoc", Bot.makeTable({
check: {value: "", align: "left", endWith: "::"},
user1: {value: "", endWith: "vs", align: "right"},
user2: {value: "", align: "left"}
}, glOverview, {useHeader: false}).join("\n"), "asciiDoc");
}, glOverview, {useHeader: false}).join("\n"));
fields.push({
name: "Galactic Legend Overview",
value:glOverview
Expand All @@ -330,11 +330,11 @@ class GrandArena extends Command {
user1: u1AvgRelic,
user2: u2AvgRelic
});
relicOverview = Bot.codeBlock(Bot.makeTable({
relicOverview = codeBlock("asciiDoc", Bot.makeTable({
check: {value: "", align: "left", endWith: "::"},
user1: {value: "", endWith: "vs", align: "right"},
user2: {value: "", align: "left"}
}, relicOverview, {useHeader: false}).join("\n"), "asciiDoc");
}, relicOverview, {useHeader: false}).join("\n"));
fields.push({
name: "Character Relic Counts",
value: "*How many characters at each relic level*" + relicOverview
Expand Down Expand Up @@ -362,11 +362,11 @@ class GrandArena extends Command {

fields.push({
name: "Mod Stats Overview",
value: Bot.codeBlock(Bot.makeTable({
value: codeBlock("asciiDoc", Bot.makeTable({
check: {value: "", align: "left", endWith: "::"},
user1: {value: "", endWith: "vs", align: "right"},
user2: {value: "", align: "left"}
}, modOverview, {useHeader: false}).join("\n"), "asciiDoc")
}, modOverview, {useHeader: false}).join("\n"))

});

Expand Down Expand Up @@ -437,11 +437,11 @@ class GrandArena extends Command {
if (halfLen < 0) halfLen = 0;
fields.push({
name: "=".repeat(halfLen) + " " + c + " " + "=".repeat(halfLen),
value: Bot.codeBlock(Bot.makeTable({
value: codeBlock("asciiDoc", 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"),
}, checkArr[c], {useHeader: false}).map(e => e.replace(" ::", "::")).join("\n")),
inline: true
});
});
Expand Down Expand Up @@ -523,11 +523,11 @@ function getOverview(Bot, user1, user2, labels) {
user1: user1.roster.reduce((a, b) => a + b.skills.filter(s => s.isOmicron && s.tier >= s.tiers).length, 0),
user2: user2.roster.reduce((a, b) => a + b.skills.filter(s => s.isOmicron && s.tier >= s.tiers).length, 0)
});
return Bot.codeBlock(Bot.makeTable({
return codeBlock("asciiDoc", Bot.makeTable({
check: {value: "", align: "left", endWith: "::"},
user1: {value: "", endWith: "vs", align: "right"},
user2: {value: "", align: "left"}
}, overview, {useHeader: false}).join("\n"), "asciiDoc");
}, overview, {useHeader: false}).join("\n"));
}

// Quick little function to add up all the gp frm a given chunk of roster
Expand Down
Loading

0 comments on commit 17bf0c2

Please sign in to comment.