separated type definitions and util functions

This commit is contained in:
2022-02-15 15:06:34 -06:00
parent 513a9d1582
commit 5348ab487b
21 changed files with 559 additions and 428 deletions

103
dist/lang.js vendored
View File

@@ -1,100 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEmbed = exports.get = exports.setLang = void 0;
const discord_js_1 = require("discord.js");
/* UTIL FUNCS */
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;
});
}
function bigString(str) {
if (typeof str === 'object')
return str.join('\n');
return str;
}
function resolveColor(color) {
color = color.replace(/[^0-9a-f]/gi, '');
const colorNum = [0, 0, 0];
if (color.length === 3 || color.length === 6) {
const colorSpl = /([0-9a-f]{1,2})([0-9a-f]{1,2})([0-9a-f]{1,2})/.exec(color);
if (!colorSpl)
return colorNum;
for (let i = 0; i < colorSpl.length && i < colorNum.length; i++)
colorNum[i] = parseInt(colorSpl[i], 16);
}
return colorNum;
}
function embedObjStr(embedData, fallback = '') {
if (embedData.content !== undefined)
return bigString(embedData.content);
if (embedData.description !== undefined)
return bigString(embedData.description);
return fallback;
}
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: template(author, args)
};
else {
const { name, icon, url } = author;
authorFix = {
name: template(name, args)
};
if (icon !== undefined)
authorFix.icon = template(icon, args);
if (url !== undefined)
authorFix.url = template(url, args);
}
embed.setAuthor(authorFix);
}
if (footer !== undefined) {
let footerFix;
if (typeof footer === 'string')
footerFix = {
text: template(footer, args)
};
else {
const { text, icon } = footer;
footerFix = {
text: template(text, args)
};
if (icon !== undefined)
footerFix.icon = template(icon, 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;
}
/* LANG */
const lang_1 = require("./util/lang");
const LANG = {
en: {
main: {
@@ -190,9 +97,9 @@ function get(id, args = {}) {
if (key in finding) {
const found = finding[key];
if (typeof found === 'string')
return template(found, args);
return (0, lang_1.template)(found, args);
if (found.embed === true)
return embedObjStr(found, id);
return (0, lang_1.embedObjStr)(found, id);
finding = found;
}
else
@@ -219,11 +126,11 @@ function getEmbed(id, args = {}, otherOptions = {}) {
if (key in finding) {
const found = finding[key];
if (typeof found === 'string') {
embedData.content = template(found, args);
embedData.content = (0, lang_1.template)(found, args);
break;
}
if (found.embed === true) {
const embedObj = found, { content } = embedObj, embed = embedObjEmbed(embedObj, args);
const embedObj = found, { content } = embedObj, embed = (0, lang_1.embedObjEmbed)(embedObj, args);
embedData.embeds.push(embed);
if (content !== undefined)
embedData.content = content;