From 2956e24684368b84a95210a0bbdb3ee655e32deb Mon Sep 17 00:00:00 2001 From: ashley zomo Date: Tue, 15 Feb 2022 15:50:36 -0600 Subject: [PATCH] comments --- dist/lang.js | 2 +- dist/util/discord.js | 12 +++++++---- dist/util/lang.js | 37 +++++++++++++++++++++++++-------- dist/util/main.js | 3 +++ src/lang.ts | 2 +- src/types/api.d.ts | 3 +++ src/types/lang.d.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/util/discord.ts | 13 +++++++----- src/util/lang.ts | 38 ++++++++++++++++++++++++++-------- src/util/main.ts | 4 +++- 10 files changed, 133 insertions(+), 30 deletions(-) diff --git a/dist/lang.js b/dist/lang.js index ab6104f..78700ae 100644 --- a/dist/lang.js +++ b/dist/lang.js @@ -99,7 +99,7 @@ function get(id, args = {}) { if (typeof found === 'string') return (0, lang_1.template)(found, args); if (found.embed === true) - return (0, lang_1.embedObjStr)(found, id); + return (0, lang_1.embedObjStr)(found, args, id); finding = found; } else diff --git a/dist/util/discord.js b/dist/util/discord.js index 4ceb2ad..ee6998d 100644 --- a/dist/util/discord.js +++ b/dist/util/discord.js @@ -5,9 +5,7 @@ const discord_js_1 = require("discord.js"); const main_1 = require("./main"); /** * get the GuildMember of an interaction - * @param interaction * @throws errorMessage class if it cannot be read - * @returns member */ function getMember(interaction) { const member = interaction.member; @@ -18,9 +16,7 @@ function getMember(interaction) { exports.getMember = getMember; /** * get the TextChannel of an interaction - * @param interaction * @throws errorMessage class if it cannot be read - * @returns member */ function getChannel(interaction) { const channel = interaction.channel; @@ -29,11 +25,19 @@ function getChannel(interaction) { return channel; } exports.getChannel = getChannel; +/** + * get the TextChannel of an interaction + * @throws errorMessage class if the Member cannot be read + */ function memberIsMod(interaction) { const member = getMember(interaction); return member.permissionsIn(interaction.channelId).has('MANAGE_MESSAGES'); } exports.memberIsMod = memberIsMod; +/** + * get the TextChannel of an interaction + * @throws errorMessage class if the Member cannot be read or if Member is not a mod + */ function memberIsModThrow(interaction) { if (!memberIsMod(interaction)) throw (0, main_1.emsg)('discord.notMod'); diff --git a/dist/util/lang.js b/dist/util/lang.js index 5ae0a29..d34f90f 100644 --- a/dist/util/lang.js +++ b/dist/util/lang.js @@ -2,6 +2,12 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.embedObjEmbed = exports.embedObjStr = exports.resolveColor = exports.bigString = exports.template = void 0; const discord_js_1 = require("discord.js"); +/** + * + * @param str + * @param args + * @returns + */ function template(str, args) { return str.replace(/{\w+}/g, str => { const key = str.substring(1, str.length - 1); @@ -11,12 +17,18 @@ function template(str, args) { }); } exports.template = template; -function bigString(str) { - if (typeof str === 'object') - return str.join('\n'); - return str; +/** + * converts bigString to string + */ +function bigString(bigStr) { + if (Array.isArray(bigStr)) + return bigStr.join('\n'); + return bigStr; } exports.bigString = bigString; +/** + * converts Hex Color string to an RGB array + */ function resolveColor(color) { color = color.replace(/[^0-9a-f]/gi, ''); const colorNum = [0, 0, 0]; @@ -30,14 +42,21 @@ function resolveColor(color) { return colorNum; } exports.resolveColor = resolveColor; -function embedObjStr(embedData, fallback = '') { - if (embedData.content !== undefined) - return bigString(embedData.content); - if (embedData.description !== undefined) - return bigString(embedData.description); +/** + * converts embedObj to a string if applicable + * @param fallback the string to use if no valid strings can be found + */ +function embedObjStr(embedObj, args = {}, fallback = '') { + if (embedObj.content !== undefined) + return template(bigString(embedObj.content), args); + if (embedObj.description !== undefined) + return template(bigString(embedObj.description), args); return fallback; } exports.embedObjStr = embedObjStr; +/** + * converts embedObj to Discord.MessageEmbed + */ function embedObjEmbed(embedObj, args = {}) { const embed = new discord_js_1.MessageEmbed(), { author, color, description, fields, footer, image, thumbnail, timestamp, title, url } = embedObj; if (author !== undefined) { diff --git a/dist/util/main.js b/dist/util/main.js index b37d09a..958fdc7 100644 --- a/dist/util/main.js +++ b/dist/util/main.js @@ -43,6 +43,9 @@ function shuffle(array) { return array; } exports.shuffle = shuffle; +/** + * use the emsg() function instead + */ class errorMessage { constructor(msg, ephemeral = true) { this.msg = msg; diff --git a/src/lang.ts b/src/lang.ts index 6752b04..816f792 100644 --- a/src/lang.ts +++ b/src/lang.ts @@ -120,7 +120,7 @@ export function get(id: string, args: basicObjectStr = {}): string { return template(found, args); if (found.embed === true) - return embedObjStr(found as embedObj, id); + return embedObjStr(found as embedObj, args, id); finding = found as LangObj; diff --git a/src/types/api.d.ts b/src/types/api.d.ts index ad6e5df..049cca5 100644 --- a/src/types/api.d.ts +++ b/src/types/api.d.ts @@ -1,3 +1,6 @@ +/** + * data taken from UniteAPI + */ interface uniteApiData { name: string, id: string, diff --git a/src/types/lang.d.ts b/src/types/lang.d.ts index 9f3978e..f6581b7 100644 --- a/src/types/lang.d.ts +++ b/src/types/lang.d.ts @@ -1,28 +1,66 @@ +/** + * any indexable object + */ //this is a generic type, and needs 'any' // eslint-disable-next-line @typescript-eslint/no-explicit-any type basicObject = {[keys: string]: any}; + +/** + * any indexable object with string values + */ type basicObjectStr = {[keys: string]: string}; +/** + * an abstract version of strings + */ type bigString = string | string[]; + +/** + * an object that contains embeds and can be passed directly to methods like `Discord.TextChannel.send()` + */ interface embedData { content?: string, embeds: MessageEmbed[] } + + +/** + * a representation of an author in the LANG object + * + * `LANG > Language > Embed > Field` + */ interface embedField { name: string, value: bigString, inline?: boolean } +/** + * a representation of an author in the LANG object + * + * `LANG > Language > Embed > Author` + */ interface authorData { name: string, url?: string, icon?: string } + +/** + * a representation of a footer in the LANG object + * + * `LANG > Language > Embed > Footer` + */ interface footerData { text: string, icon?: string } + +/** + * a representation of an embed in the LANG object + * + * `LANG > Language > Embed` + */ interface embedObj { embed: true, @@ -51,5 +89,16 @@ interface embedObj { timestamp?: boolean | string | number } +/** + * a specific language in the LANG object + * + * `LANG > Language` + */ type LangObj = { [keys:string]: LangObj | embedObj | string } + +/** + * the entire LANG object + * + * `LANG` + */ type LangObjWhole = { [langid:string]: LangObj } \ No newline at end of file diff --git a/src/util/discord.ts b/src/util/discord.ts index 2379604..0e43c9b 100644 --- a/src/util/discord.ts +++ b/src/util/discord.ts @@ -3,9 +3,7 @@ import { emsg } from './main'; /** * get the GuildMember of an interaction - * @param interaction * @throws errorMessage class if it cannot be read - * @returns member */ export function getMember(interaction: CommandInteraction): GuildMember { const member = interaction.member; @@ -18,9 +16,7 @@ export function getMember(interaction: CommandInteraction): GuildMember { /** * get the TextChannel of an interaction - * @param interaction * @throws errorMessage class if it cannot be read - * @returns member */ export function getChannel(interaction: CommandInteraction): TextChannel { const channel = interaction.channel; @@ -31,12 +27,19 @@ export function getChannel(interaction: CommandInteraction): TextChannel { return channel; } - +/** + * get the TextChannel of an interaction + * @throws errorMessage class if the Member cannot be read + */ export function memberIsMod(interaction: CommandInteraction): boolean { const member = getMember(interaction); return member.permissionsIn(interaction.channelId).has('MANAGE_MESSAGES'); } +/** + * get the TextChannel of an interaction + * @throws errorMessage class if the Member cannot be read or if Member is not a mod + */ export function memberIsModThrow(interaction: CommandInteraction) { if (!memberIsMod(interaction)) throw emsg('discord.notMod'); diff --git a/src/util/lang.ts b/src/util/lang.ts index c09dc3b..b599719 100644 --- a/src/util/lang.ts +++ b/src/util/lang.ts @@ -1,5 +1,11 @@ import { MessageEmbed } from 'discord.js'; +/** + * + * @param str + * @param args + * @returns + */ export function template(str: string, args: basicObject): string { return str.replace(/{\w+}/g, str => { @@ -15,12 +21,19 @@ export function template(str: string, args: basicObject): string { } -export function bigString(str: bigString): string { - if (typeof str === 'object') - return str.join('\n'); - return str; +/** + * converts bigString to string + */ +export function bigString(bigStr: bigString): string { + if (Array.isArray(bigStr)) + return bigStr.join('\n'); + return bigStr; } + +/** + * converts Hex Color string to an RGB array + */ export function resolveColor(color: string): [number, number, number] { color = color.replace(/[^0-9a-f]/gi, ''); @@ -41,17 +54,24 @@ export function resolveColor(color: string): [number, number, number] { return colorNum; } -export function embedObjStr(embedData: embedObj, fallback = ''): string { +/** + * converts embedObj to a string if applicable + * @param fallback the string to use if no valid strings can be found + */ +export function embedObjStr(embedObj: embedObj, args: basicObjectStr = {}, fallback = ''): string { - if (embedData.content !== undefined) - return bigString(embedData.content); + if (embedObj.content !== undefined) + return template(bigString(embedObj.content), args); - if (embedData.description !== undefined) - return bigString(embedData.description); + if (embedObj.description !== undefined) + return template(bigString(embedObj.description), args); return fallback; } +/** + * converts embedObj to Discord.MessageEmbed + */ export function embedObjEmbed(embedObj: embedObj, args: basicObjectStr = {}): MessageEmbed { const embed = new MessageEmbed(), { author, color, description, fields, footer, image, thumbnail, timestamp, title, url } = embedObj; diff --git a/src/util/main.ts b/src/util/main.ts index 3b66c3d..883188a 100644 --- a/src/util/main.ts +++ b/src/util/main.ts @@ -26,7 +26,9 @@ export function shuffle(array: any[]) { } - +/** + * use the emsg() function instead + */ export class errorMessage { public msg: string; public ephemeral: boolean;