init
This commit is contained in:
99
src/discord/main.ts
Normal file
99
src/discord/main.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { MessageEmbed } from 'discord.js';
|
||||
import { template, resolveColor, bigString } from '../main';
|
||||
import { embedObject, basicObjectString, authorData, footerData } from '../types';
|
||||
|
||||
export * from "../main";
|
||||
|
||||
/**
|
||||
* converts embedObj to Discord.MessageEmbed
|
||||
*/
|
||||
export function embedObjEmbed(embedObj: embedObject, args: basicObjectString = {}): MessageEmbed {
|
||||
const embed = new MessageEmbed(),
|
||||
{ author, color, description, fields, footer, image, thumbnail, timestamp, title, url } = embedObj;
|
||||
|
||||
if (author !== undefined) {
|
||||
|
||||
let authorFix: authorData;
|
||||
|
||||
if (typeof author === 'string')
|
||||
authorFix = {
|
||||
name: template(author, args)
|
||||
};
|
||||
else {
|
||||
|
||||
const {name, iconURL, url} = author;
|
||||
|
||||
authorFix = {
|
||||
name: template(name, args)
|
||||
};
|
||||
|
||||
if (iconURL !== undefined)
|
||||
authorFix.iconURL = template(iconURL, args);
|
||||
|
||||
if (url !== undefined)
|
||||
authorFix.url = template(url, args);
|
||||
|
||||
}
|
||||
|
||||
embed.setAuthor(authorFix);
|
||||
|
||||
}
|
||||
|
||||
if (footer !== undefined) {
|
||||
|
||||
let footerFix: footerData;
|
||||
|
||||
if (typeof footer === 'string') {
|
||||
|
||||
footerFix = {
|
||||
text: template(footer, args)
|
||||
};
|
||||
|
||||
} else {
|
||||
|
||||
const {text, iconURL} = footer;
|
||||
|
||||
footerFix = {
|
||||
text: template(text, args)
|
||||
};
|
||||
|
||||
if (iconURL !== undefined)
|
||||
footerFix.iconURL = template(iconURL, args);
|
||||
|
||||
}
|
||||
|
||||
embed.setFooter(footerFix);
|
||||
|
||||
}
|
||||
|
||||
if (color !== undefined)
|
||||
embed.setColor(resolveColor(template(color, args)));
|
||||
|
||||
if (description !== undefined)
|
||||
embed.setDescription(template(bigString(description), args));
|
||||
|
||||
if (image !== undefined)
|
||||
embed.setImage(template(image, args));
|
||||
|
||||
if (thumbnail !== undefined)
|
||||
embed.setThumbnail(template(thumbnail, args));
|
||||
|
||||
if (title !== undefined)
|
||||
embed.setTitle(template(title, args));
|
||||
|
||||
if (url !== undefined)
|
||||
embed.setURL(template(url, args));
|
||||
|
||||
if (timestamp === true)
|
||||
embed.setTimestamp();
|
||||
else if (typeof timestamp === 'string')
|
||||
embed.setTimestamp(new Date(template(timestamp, args)));
|
||||
else if (timestamp !== false)
|
||||
embed.setTimestamp(timestamp);
|
||||
|
||||
fields?.forEach(field => {
|
||||
embed.addField(template(field.name, args), template(bigString(field.value), args), field.inline);
|
||||
});
|
||||
|
||||
return embed;
|
||||
}
|
||||
9
src/discord/types.ts
Normal file
9
src/discord/types.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { MessageEmbed } from "discord.js";
|
||||
|
||||
/**
|
||||
* an object that contains embeds and can be passed directly to methods like `Discord.TextChannel.send()`
|
||||
*/
|
||||
export interface embedDataType {
|
||||
content?: string,
|
||||
embeds: MessageEmbed[]
|
||||
}
|
||||
76
src/main.ts
Normal file
76
src/main.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
|
||||
import { authorData, basicObject, basicObjectString, bigStringType, embedObject, footerData } from './types';
|
||||
|
||||
/**
|
||||
*
|
||||
* @param str
|
||||
* @param args
|
||||
* @returns
|
||||
*/
|
||||
export function template(str: string, args: basicObject): string {
|
||||
|
||||
return str.replace(/{\w+}/g, str => {
|
||||
|
||||
const key = str.substring(1, str.length-1);
|
||||
|
||||
if (key in args)
|
||||
return args[key];
|
||||
|
||||
return key;
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* converts bigString to string
|
||||
*/
|
||||
export function bigString(bigStr: bigStringType): 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, '');
|
||||
|
||||
const colorNum: [number, number, number] = [0, 0, 0];
|
||||
|
||||
if (color.length === 3 || color.length === 6) {
|
||||
|
||||
const colorSplRaw = /([0-9a-f]{1,2})([0-9a-f]{1,2})([0-9a-f]{1,2})/.exec(color);
|
||||
|
||||
if (!colorSplRaw)
|
||||
return colorNum;
|
||||
|
||||
const colorSpl = colorSplRaw.slice(1, 4);
|
||||
|
||||
if (color.length === 3)
|
||||
colorSpl.map(c => c + c);
|
||||
|
||||
for (let i = 0; i < colorSpl.length && i < colorNum.length; i++)
|
||||
colorNum[i] = parseInt(colorSpl[i], 16);
|
||||
|
||||
}
|
||||
|
||||
return colorNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* converts embedObj to a string if applicable
|
||||
* @param fallback the string to use if no valid strings can be found
|
||||
*/
|
||||
export function embedObjStr(embedObj: embedObject, args: basicObjectString = {}, fallback = ''): string {
|
||||
|
||||
if (embedObj.content !== undefined)
|
||||
return template(bigString(embedObj.content), args);
|
||||
|
||||
if (embedObj.description !== undefined)
|
||||
return template(bigString(embedObj.description), args);
|
||||
|
||||
return fallback;
|
||||
}
|
||||
99
src/types.ts
Normal file
99
src/types.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
|
||||
|
||||
/**
|
||||
* any indexable object
|
||||
*/
|
||||
//this is a generic type, and needs 'any'
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type basicObject = {[keys: string]: any};
|
||||
|
||||
/**
|
||||
* any indexable object with string values
|
||||
*/
|
||||
export type basicObjectStringable = {[keys: string]: string|number|boolean|null};
|
||||
export type basicObjectString = {[keys: string]: string};
|
||||
|
||||
/**
|
||||
* an abstract version of strings
|
||||
*/
|
||||
export type bigStringType = string | string[];
|
||||
|
||||
|
||||
/**
|
||||
* a representation of an author in the LANG object
|
||||
*
|
||||
* `LANG > Language > Embed > Field`
|
||||
*/
|
||||
export interface embedField {
|
||||
name: string,
|
||||
value: bigStringType,
|
||||
inline?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* a representation of an author in the LANG object
|
||||
*
|
||||
* `LANG > Language > Embed > Author`
|
||||
*/
|
||||
export interface authorData {
|
||||
name: string,
|
||||
url?: string,
|
||||
iconURL?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* a representation of a footer in the LANG object
|
||||
*
|
||||
* `LANG > Language > Embed > Footer`
|
||||
*/
|
||||
export interface footerData {
|
||||
text: string,
|
||||
iconURL?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* a representation of an embed in the LANG object
|
||||
*
|
||||
* `LANG > Language > Embed`
|
||||
*/
|
||||
export interface embedObject {
|
||||
embed: true,
|
||||
|
||||
content?: string,
|
||||
title?: string,
|
||||
description?: bigStringType,
|
||||
/**
|
||||
* URL
|
||||
*/
|
||||
url?: string,
|
||||
/**
|
||||
* #FFFFFF
|
||||
*/
|
||||
color?: string,
|
||||
footer?: string | footerData,
|
||||
thumbnail?: string,
|
||||
/**
|
||||
* URL
|
||||
*/
|
||||
image?: string,
|
||||
/**
|
||||
* URL
|
||||
*/
|
||||
author?: string | authorData,
|
||||
fields?: embedField[],
|
||||
timestamp?: boolean | string | number
|
||||
}
|
||||
|
||||
/**
|
||||
* a specific language in the LANG object
|
||||
*
|
||||
* `LANG > Language`
|
||||
*/
|
||||
export type LangObj = { [keys:string]: LangObj | embedObject | string }
|
||||
|
||||
/**
|
||||
* the entire LANG object
|
||||
*
|
||||
* `LANG`
|
||||
*/
|
||||
export type LangObjWhole = { [langid:string]: LangObj }
|
||||
Reference in New Issue
Block a user