This commit is contained in:
2022-03-29 22:50:05 -05:00
parent 4528cba9b0
commit bb50db4dec
6 changed files with 280 additions and 149 deletions

View File

@@ -1,99 +1,67 @@
import { MessageEmbed } from 'discord.js';
import { template, resolveColor, bigString } from '..';
import { embedObject, basicObjectString, authorData, footerData } from '../types';
export * from "..";
import { basicObject, basicObjectStringable, embedObject, LangObj } from '../types';
import { convertBasicObject, template } from '../util';
import { embedDataType } from './types';
import { embedObjEmbed } from './util';
import Lang, { getLang } from '..';
/**
* converts embedObj to Discord.MessageEmbed
* 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
*/
export function embedObjEmbed(embedObj: embedObject, args: basicObjectString = {}): MessageEmbed {
const embed = new MessageEmbed(),
{ author, color, description, fields, footer, image, thumbnail, timestamp, title, url } = embedObj;
function getEmbed(id: string, argsraw: basicObjectStringable = {}, otherOptions: basicObject = {}): embedDataType {
if (author !== undefined) {
const args = convertBasicObject(argsraw),
embedData: embedDataType = {
...otherOptions,
embeds: []
};
const keySpl = id.split('.').map(k => k.trim()).filter(k => k);
let authorFix: authorData;
let finding = getLang();
if (typeof author === 'string')
authorFix = {
name: template(author, args)
};
else {
for (const key of keySpl) {
const {name, iconURL, url} = author;
if (key in finding) {
authorFix = {
name: template(name, args)
};
const found = finding[key];
if (iconURL !== undefined)
authorFix.iconURL = template(iconURL, args);
if (typeof found === 'string') {
embedData.content = template(found, args);
break;
}
if (url !== undefined)
authorFix.url = template(url, args);
if (found.embed === true) {
const embedObj = found as embedObject,
{content} = embedObj,
embed = embedObjEmbed(embedObj, args);
}
embedData.embeds.push(embed);
embed.setAuthor(authorFix);
if (content !== undefined)
embedData.content = content;
return embedData;
}
finding = found as LangObj;
} else
break;
}
if (footer !== undefined) {
return embedData;
}
let footerFix: footerData;
export * from './types';
export * from './util';
export * from '..';
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;
}
export default {
...Lang,
getEmbed
};

View File

@@ -6,4 +6,6 @@ import { MessageEmbed } from "discord.js";
export interface embedDataType {
content?: string,
embeds: MessageEmbed[]
}
}
export * from '../types';

99
src/discord/util.ts Normal file
View File

@@ -0,0 +1,99 @@
import { MessageEmbed } from 'discord.js';
import { template, resolveColor, bigString } from '../util';
import { embedObject, basicObjectString, authorData, footerData } from '../types';
/**
* 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;
}
export * from '../util';