120 lines
3.8 KiB
JavaScript
120 lines
3.8 KiB
JavaScript
"use strict";
|
|
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);
|
|
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 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;
|
|
}
|
|
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;
|
|
/**
|
|
* 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: 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;
|
|
}
|
|
exports.embedObjEmbed = embedObjEmbed;
|