From 5a62482891d634fa1c2995a1a6ed2608b672cd6e Mon Sep 17 00:00:00 2001 From: ZomoXYZ Date: Tue, 29 Mar 2022 23:25:06 -0500 Subject: [PATCH] done i think --- .gitignore | 3 +- discord/buildexample.js | 16 +++++ discord/lib/discord/src/index.d.ts | 19 ++++++ discord/lib/discord/src/index.js | 54 ++++++++++++++++ discord/lib/discord/src/types.d.ts | 8 +++ discord/lib/discord/src/types.js | 2 + discord/lib/discord/src/util.d.ts | 6 ++ discord/lib/discord/src/util.js | 69 ++++++++++++++++++++ discord/lib/src/types.d.ts | 87 ++++++++++++++++++++++++++ discord/lib/src/types.js | 2 + discord/lib/src/util.d.ts | 22 +++++++ discord/lib/src/util.js | 74 ++++++++++++++++++++++ discord/package.json | 4 ++ src/discord.ts => discord/src/index.ts | 14 ++--- {src/discord => discord/src}/types.ts | 2 - {src/discord => discord/src}/util.ts | 6 +- discord/tsconfig.json | 11 ++++ lib/index.d.ts | 17 +++++ lib/index.js | 57 +++++++++++++++++ lib/types.d.ts | 87 ++++++++++++++++++++++++++ lib/types.js | 2 + lib/util.d.ts | 22 +++++++ lib/util.js | 74 ++++++++++++++++++++++ package.json | 3 +- 24 files changed, 643 insertions(+), 18 deletions(-) create mode 100644 discord/buildexample.js create mode 100644 discord/lib/discord/src/index.d.ts create mode 100644 discord/lib/discord/src/index.js create mode 100644 discord/lib/discord/src/types.d.ts create mode 100644 discord/lib/discord/src/types.js create mode 100644 discord/lib/discord/src/util.d.ts create mode 100644 discord/lib/discord/src/util.js create mode 100644 discord/lib/src/types.d.ts create mode 100644 discord/lib/src/types.js create mode 100644 discord/lib/src/util.d.ts create mode 100644 discord/lib/src/util.js create mode 100644 discord/package.json rename src/discord.ts => discord/src/index.ts (80%) rename {src/discord => discord/src}/types.ts (89%) rename {src/discord => discord/src}/util.ts (95%) create mode 100644 discord/tsconfig.json create mode 100644 lib/index.d.ts create mode 100644 lib/index.js create mode 100644 lib/types.d.ts create mode 100644 lib/types.js create mode 100644 lib/util.d.ts create mode 100644 lib/util.js diff --git a/.gitignore b/.gitignore index 9b26ed0..b512c09 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -node_modules -lib \ No newline at end of file +node_modules \ No newline at end of file diff --git a/discord/buildexample.js b/discord/buildexample.js new file mode 100644 index 0000000..ecbfb5c --- /dev/null +++ b/discord/buildexample.js @@ -0,0 +1,16 @@ +const { build } = require('esbuild'); + +const define = { + TOKEN: `"${process.argv[2]}"` +}; + +build({ + entryPoints: ['examples/main.ts'], + outfile: 'examples/main.js', + + target: 'es6', + platform: 'node', + format: 'cjs', + + define +}); \ No newline at end of file diff --git a/discord/lib/discord/src/index.d.ts b/discord/lib/discord/src/index.d.ts new file mode 100644 index 0000000..70e4c3d --- /dev/null +++ b/discord/lib/discord/src/index.d.ts @@ -0,0 +1,19 @@ +import { basicObject, basicObjectStringable } from '../..'; +import { embedDataType } from './types'; +/** + * reads language json as an object (could be embed or just string) + * @param id ex: discord.error.noActiveQueue + * @param argsraw list of key/value pairs to represent template values + * @param otherOptions values to be passed through to the return value + * @returns language value, defaults to `id` parameter + */ +declare function getEmbed(id: string, argsraw?: basicObjectStringable, otherOptions?: basicObject): embedDataType; +export * from '../..'; +export * from './types'; +export * from './util'; +declare const _default: { + getEmbed: typeof getEmbed; + setLang: typeof import("../..").setLang; + get: (id: string, argsraw?: basicObjectStringable | undefined) => string; +}; +export default _default; diff --git a/discord/lib/discord/src/index.js b/discord/lib/discord/src/index.js new file mode 100644 index 0000000..13da9f3 --- /dev/null +++ b/discord/lib/discord/src/index.js @@ -0,0 +1,54 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const __1 = require("../.."); +const util_1 = require("./util"); +/** + * reads language json as an object (could be embed or just string) + * @param id ex: discord.error.noActiveQueue + * @param argsraw list of key/value pairs to represent template values + * @param otherOptions values to be passed through to the return value + * @returns language value, defaults to `id` parameter + */ +function getEmbed(id, argsraw = {}, otherOptions = {}) { + const args = (0, __1.convertBasicObject)(argsraw), embedData = Object.assign(Object.assign({}, otherOptions), { embeds: [] }); + const keySpl = id.split('.').map(k => k.trim()).filter(k => k); + let finding = (0, __1.getLang)(); + for (const key of keySpl) { + if (key in finding) { + const found = finding[key]; + if (typeof found === 'string') { + embedData.content = (0, __1.template)(found, args); + break; + } + if (found.embed === true) { + const embedObj = found, { content } = embedObj, embed = (0, util_1.embedObjEmbed)(embedObj, args); + embedData.embeds.push(embed); + if (content !== undefined) + embedData.content = content; + return embedData; + } + finding = found; + } + else + break; + } + return embedData; +} +__exportStar(require("../.."), exports); +__exportStar(require("./types"), exports); +__exportStar(require("./util"), exports); +exports.default = Object.assign(Object.assign({}, __1.default), { getEmbed }); diff --git a/discord/lib/discord/src/types.d.ts b/discord/lib/discord/src/types.d.ts new file mode 100644 index 0000000..bf53976 --- /dev/null +++ b/discord/lib/discord/src/types.d.ts @@ -0,0 +1,8 @@ +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[]; +} diff --git a/discord/lib/discord/src/types.js b/discord/lib/discord/src/types.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/discord/lib/discord/src/types.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/discord/lib/discord/src/util.d.ts b/discord/lib/discord/src/util.d.ts new file mode 100644 index 0000000..2adb2c9 --- /dev/null +++ b/discord/lib/discord/src/util.d.ts @@ -0,0 +1,6 @@ +import { MessageEmbed } from 'discord.js'; +import { embedObject, basicObjectString } from '../../src/types'; +/** + * converts embedObj to Discord.MessageEmbed + */ +export declare function embedObjEmbed(embedObj: embedObject, args?: basicObjectString): MessageEmbed; diff --git a/discord/lib/discord/src/util.js b/discord/lib/discord/src/util.js new file mode 100644 index 0000000..8cd14ea --- /dev/null +++ b/discord/lib/discord/src/util.js @@ -0,0 +1,69 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.embedObjEmbed = void 0; +const discord_js_1 = require("discord.js"); +const util_1 = require("../../src/util"); +/** + * 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) { + let authorFix; + if (typeof author === 'string') + authorFix = { + name: (0, util_1.template)(author, args) + }; + else { + const { name, iconURL, url } = author; + authorFix = { + name: (0, util_1.template)(name, args) + }; + if (iconURL !== undefined) + authorFix.iconURL = (0, util_1.template)(iconURL, args); + if (url !== undefined) + authorFix.url = (0, util_1.template)(url, args); + } + embed.setAuthor(authorFix); + } + if (footer !== undefined) { + let footerFix; + if (typeof footer === 'string') { + footerFix = { + text: (0, util_1.template)(footer, args) + }; + } + else { + const { text, iconURL } = footer; + footerFix = { + text: (0, util_1.template)(text, args) + }; + if (iconURL !== undefined) + footerFix.iconURL = (0, util_1.template)(iconURL, args); + } + embed.setFooter(footerFix); + } + if (color !== undefined) + embed.setColor((0, util_1.resolveColor)((0, util_1.template)(color, args))); + if (description !== undefined) + embed.setDescription((0, util_1.template)((0, util_1.bigString)(description), args)); + if (image !== undefined) + embed.setImage((0, util_1.template)(image, args)); + if (thumbnail !== undefined) + embed.setThumbnail((0, util_1.template)(thumbnail, args)); + if (title !== undefined) + embed.setTitle((0, util_1.template)(title, args)); + if (url !== undefined) + embed.setURL((0, util_1.template)(url, args)); + if (timestamp === true) + embed.setTimestamp(); + else if (typeof timestamp === 'string') + embed.setTimestamp(new Date((0, util_1.template)(timestamp, args))); + else if (timestamp !== false) + embed.setTimestamp(timestamp); + fields === null || fields === void 0 ? void 0 : fields.forEach(field => { + embed.addField((0, util_1.template)(field.name, args), (0, util_1.template)((0, util_1.bigString)(field.value), args), field.inline); + }); + return embed; +} +exports.embedObjEmbed = embedObjEmbed; diff --git a/discord/lib/src/types.d.ts b/discord/lib/src/types.d.ts new file mode 100644 index 0000000..e63e379 --- /dev/null +++ b/discord/lib/src/types.d.ts @@ -0,0 +1,87 @@ +/** + * any indexable object + */ +export declare type basicObject = { + [keys: string]: any; +}; +/** + * any indexable object with string values + */ +export declare type basicObjectStringable = { + [keys: string]: string | number | boolean | null; +}; +export declare type basicObjectString = { + [keys: string]: string; +}; +/** + * an abstract version of strings + */ +export declare 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 declare type LangObj = { + [keys: string]: LangObj | embedObject | string; +}; diff --git a/discord/lib/src/types.js b/discord/lib/src/types.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/discord/lib/src/types.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/discord/lib/src/util.d.ts b/discord/lib/src/util.d.ts new file mode 100644 index 0000000..afa0e99 --- /dev/null +++ b/discord/lib/src/util.d.ts @@ -0,0 +1,22 @@ +import { basicObject, basicObjectString, basicObjectStringable, bigStringType, embedObject } from './types'; +/** + * + * @param str + * @param args + * @returns + */ +export declare function template(str: string, args: basicObject): string; +/** + * converts bigString to string + */ +export declare function bigString(bigStr: bigStringType): string; +/** + * converts Hex Color string to an RGB array + */ +export declare function resolveColor(color: string): [number, number, number]; +/** + * converts embedObj to a string if applicable + * @param fallback the string to use if no valid strings can be found + */ +export declare function embedObjStr(embedObj: embedObject, args?: basicObjectString, fallback?: string): string; +export declare function convertBasicObject(obj: basicObjectStringable): basicObjectString; diff --git a/discord/lib/src/util.js b/discord/lib/src/util.js new file mode 100644 index 0000000..276e74e --- /dev/null +++ b/discord/lib/src/util.js @@ -0,0 +1,74 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.convertBasicObject = exports.embedObjStr = exports.resolveColor = exports.bigString = exports.template = void 0; +/** + * + * @param str + * @param args + * @returns + */ +function template(str, args) { + return str.replace(/{\w+}/g, str => { + const key = str.substring(1, str.length - 1); + if (key in args) + return args[key]; + return key; + }); +} +exports.template = template; +/** + * 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]; + 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; +} +exports.resolveColor = resolveColor; +/** + * 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; +function convertBasicObject(obj) { + let ret = {}; + for (const key in obj) { + const val = obj[key]; + if (typeof val === 'string') + ret[key] = val; + else if (typeof val === 'boolean') + ret[key] = val.toString(); + else if (typeof val === 'number') + ret[key] = val.toString(); + else + ret[key] = ''; + } + return ret; +} +exports.convertBasicObject = convertBasicObject; diff --git a/discord/package.json b/discord/package.json new file mode 100644 index 0000000..5e090eb --- /dev/null +++ b/discord/package.json @@ -0,0 +1,4 @@ +{ + "main": "lib/index.js", + "types": "lib/index.d.ts" +} diff --git a/src/discord.ts b/discord/src/index.ts similarity index 80% rename from src/discord.ts rename to discord/src/index.ts index 7aa970d..0307512 100644 --- a/src/discord.ts +++ b/discord/src/index.ts @@ -1,8 +1,6 @@ -import { basicObject, basicObjectStringable, embedObject, LangObj } from './types'; -import { convertBasicObject, template } from './util'; -import { embedDataType } from './discord/types'; -import { embedObjEmbed } from './discord/util'; -import Lang, { getLang } from '.'; +import Lang, { basicObject, basicObjectStringable, convertBasicObject, embedObject, getLang, LangObj, template } from '../..'; +import { embedDataType } from './types'; +import { embedObjEmbed } from './util'; /** * reads language json as an object (could be embed or just string) @@ -57,9 +55,9 @@ function getEmbed(id: string, argsraw: basicObjectStringable = {}, otherOptions: return embedData; } -export * from './discord/types'; -export * from './discord/util'; -export * from '.'; +export * from '../..'; +export * from './types'; +export * from './util'; export default { ...Lang, diff --git a/src/discord/types.ts b/discord/src/types.ts similarity index 89% rename from src/discord/types.ts rename to discord/src/types.ts index e626324..b3df57d 100644 --- a/src/discord/types.ts +++ b/discord/src/types.ts @@ -7,5 +7,3 @@ export interface embedDataType { content?: string, embeds: MessageEmbed[] } - -export * from '../types'; diff --git a/src/discord/util.ts b/discord/src/util.ts similarity index 95% rename from src/discord/util.ts rename to discord/src/util.ts index 33ffb3c..c30e0b7 100644 --- a/src/discord/util.ts +++ b/discord/src/util.ts @@ -1,6 +1,6 @@ import { MessageEmbed } from 'discord.js'; -import { template, resolveColor, bigString } from '../util'; -import { embedObject, basicObjectString, authorData, footerData } from '../types'; +import { template, resolveColor, bigString } from '../../src/util'; +import { embedObject, basicObjectString, authorData, footerData } from '../../src/types'; /** * converts embedObj to Discord.MessageEmbed @@ -95,5 +95,3 @@ export function embedObjEmbed(embedObj: embedObject, args: basicObjectString = { return embed; } - -export * from '../util'; diff --git a/discord/tsconfig.json b/discord/tsconfig.json new file mode 100644 index 0000000..c698472 --- /dev/null +++ b/discord/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "commonjs", + "declaration": true, + "outDir": "./lib", + "strict": true + }, + "include": ["src"], + "exclude": ["node_modules"] +} \ No newline at end of file diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..978fb9d --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,17 @@ +import { basicObjectStringable, LangObj } from './types'; +export declare function setLang(lang: LangObj): void; +export declare function getLang(): LangObj; +/** + * reads language json (just strings) + * @param id ex: discord.error.noActiveQueue + * @param argsraw list of key/value pairs to represent template values + * @returns language value, defaults to `id` parameter + */ +declare function get(id: string, argsraw?: basicObjectStringable): string; +declare const _default: { + setLang: typeof setLang; + get: typeof get; +}; +export default _default; +export * from './types'; +export * from './util'; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..4494e31 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,57 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.getLang = exports.setLang = void 0; +const util_1 = require("./util"); +/* MAIN */ +var LANG = {}; +function setLang(lang) { + LANG = lang; +} +exports.setLang = setLang; +function getLang() { + return LANG; +} +exports.getLang = getLang; +/** + * reads language json (just strings) + * @param id ex: discord.error.noActiveQueue + * @param argsraw list of key/value pairs to represent template values + * @returns language value, defaults to `id` parameter + */ +function get(id, argsraw = {}) { + const args = (0, util_1.convertBasicObject)(argsraw), keySpl = id.split('.').map(k => k.trim()).filter(k => k); + let finding = LANG; + for (const key of keySpl) { + if (key in finding) { + const found = finding[key]; + if (typeof found === 'string') + return (0, util_1.template)(found, args); + if (found.embed === true) + return (0, util_1.embedObjStr)(found, args, id); + finding = found; + } + else + break; + } + return id; +} +exports.default = { + setLang, + get +}; +__exportStar(require("./types"), exports); +__exportStar(require("./util"), exports); diff --git a/lib/types.d.ts b/lib/types.d.ts new file mode 100644 index 0000000..e63e379 --- /dev/null +++ b/lib/types.d.ts @@ -0,0 +1,87 @@ +/** + * any indexable object + */ +export declare type basicObject = { + [keys: string]: any; +}; +/** + * any indexable object with string values + */ +export declare type basicObjectStringable = { + [keys: string]: string | number | boolean | null; +}; +export declare type basicObjectString = { + [keys: string]: string; +}; +/** + * an abstract version of strings + */ +export declare 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 declare type LangObj = { + [keys: string]: LangObj | embedObject | string; +}; diff --git a/lib/types.js b/lib/types.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/lib/types.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/lib/util.d.ts b/lib/util.d.ts new file mode 100644 index 0000000..afa0e99 --- /dev/null +++ b/lib/util.d.ts @@ -0,0 +1,22 @@ +import { basicObject, basicObjectString, basicObjectStringable, bigStringType, embedObject } from './types'; +/** + * + * @param str + * @param args + * @returns + */ +export declare function template(str: string, args: basicObject): string; +/** + * converts bigString to string + */ +export declare function bigString(bigStr: bigStringType): string; +/** + * converts Hex Color string to an RGB array + */ +export declare function resolveColor(color: string): [number, number, number]; +/** + * converts embedObj to a string if applicable + * @param fallback the string to use if no valid strings can be found + */ +export declare function embedObjStr(embedObj: embedObject, args?: basicObjectString, fallback?: string): string; +export declare function convertBasicObject(obj: basicObjectStringable): basicObjectString; diff --git a/lib/util.js b/lib/util.js new file mode 100644 index 0000000..276e74e --- /dev/null +++ b/lib/util.js @@ -0,0 +1,74 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.convertBasicObject = exports.embedObjStr = exports.resolveColor = exports.bigString = exports.template = void 0; +/** + * + * @param str + * @param args + * @returns + */ +function template(str, args) { + return str.replace(/{\w+}/g, str => { + const key = str.substring(1, str.length - 1); + if (key in args) + return args[key]; + return key; + }); +} +exports.template = template; +/** + * 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]; + 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; +} +exports.resolveColor = resolveColor; +/** + * 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; +function convertBasicObject(obj) { + let ret = {}; + for (const key in obj) { + const val = obj[key]; + if (typeof val === 'string') + ret[key] = val; + else if (typeof val === 'boolean') + ret[key] = val.toString(); + else if (typeof val === 'number') + ret[key] = val.toString(); + else + ret[key] = ''; + } + return ret; +} +exports.convertBasicObject = convertBasicObject; diff --git a/package.json b/package.json index d59fba7..af1bb41 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,7 @@ "types": "lib/index.d.ts", "devDependencies": {}, "scripts": { - "build": "tsc", - "prepare": "npm run build" + "build": "tsc && cd discord && tsc && cd .." }, "repository": { "type": "git",