This commit is contained in:
2022-02-15 15:50:36 -06:00
parent c2447b180e
commit 2956e24684
10 changed files with 133 additions and 30 deletions

2
dist/lang.js vendored
View File

@@ -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

12
dist/util/discord.js vendored
View File

@@ -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');

37
dist/util/lang.js vendored
View File

@@ -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) {

3
dist/util/main.js vendored
View File

@@ -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;

View File

@@ -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;

3
src/types/api.d.ts vendored
View File

@@ -1,3 +1,6 @@
/**
* data taken from UniteAPI
*/
interface uniteApiData {
name: string,
id: string,

49
src/types/lang.d.ts vendored
View File

@@ -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 }

View File

@@ -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');

View File

@@ -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;

View File

@@ -26,7 +26,9 @@ export function shuffle(array: any[]) {
}
/**
* use the emsg() function instead
*/
export class errorMessage {
public msg: string;
public ephemeral: boolean;