Files
Yumi/src/utils/DiscordMessage.ts
T

141 lines
5.2 KiB
TypeScript
Raw Normal View History

2022-01-27 14:31:12 +07:00
import { MessageEmbed, User, MessagePayload, MessageOptions, GuildTextBasedChannel, TextChannel, DMChannel, PartialDMChannel, BaseGuildTextChannel, Message, ColorResolvable, Interaction, InteractionReplyOptions } from "discord.js";
2021-09-14 10:05:09 -07:00
import App from "..";
2021-09-16 01:20:05 -07:00
import Logger from "../libs/Logger";
2021-09-14 10:05:09 -07:00
const emotes = {
"yumiloading": "<a:yumiloading:887350424938627173>"
};
export function makeEmbed(icon: string | undefined, title: string, description: any, color: ColorResolvable, fields: any, user: User, setTimestamp: boolean) {
const embed = new MessageEmbed();
embed.setColor(color || '#FFFFFF');
if(typeof icon === 'undefined')
embed.setTitle(title);
else
embed.setTitle(`${icon} ${title}`);
if (typeof description !== 'undefined')
embed.setDescription(description)
if(setTimestamp)
embed.setTimestamp();
if(typeof user !== 'undefined')
2022-01-25 13:30:27 +07:00
embed.footer = {
text: `${user.username} | v${App.version}`,
iconURL: user.avatarURL() || ''
}
2021-09-15 06:05:47 -07:00
else
2022-01-25 13:30:27 +07:00
embed.footer = {
text: `v${App.version}`
}
2021-09-14 10:05:09 -07:00
if (typeof fields !== 'undefined')
embed.addFields(fields);
return embed;
}
export function makeSuccessEmbed(options: any) {
2021-09-19 08:39:16 -07:00
return makeEmbed(typeof options.icon === 'undefined' ? "✅" : options.icon, options.title, options.description, '#B5EAD7', options.fields, options.user, options.setTimestamp || true);
2021-09-14 10:05:09 -07:00
}
export function makeWarningEmbed(options: any) {
return makeEmbed(typeof options.icon === 'undefined' ? "⚠️" : options.icon, options.title, options.description, '#FFEEAD', options.fields, options.user, options.setTimestamp || true);
}
2021-09-14 10:05:09 -07:00
export function makeErrorEmbed(options: any) {
2021-09-19 08:39:16 -07:00
return makeEmbed(typeof options.icon === 'undefined' ? "❌" : options.icon, options.title, options.description, '#FF9AA2', options.fields, options.user, options.setTimestamp || true);
2021-09-14 10:05:09 -07:00
}
export function makeProcessingEmbed(options: any) {
2021-09-19 08:39:16 -07:00
return makeEmbed(typeof options.icon === 'undefined' ? getEmotes().yumiloading : options.icon, options.title, options.description, '#E2F0CB', options.fields, options.user, options.setTimestamp || true);
2021-09-15 06:05:47 -07:00
}
export function makeInfoEmbed(options: any) {
2021-09-19 08:39:16 -07:00
return makeEmbed(typeof options.icon === 'undefined' ? "🔮" : options.icon, options.title, options.description, '#C7CEEA', options.fields, options.user, options.setTimestamp || true);
2021-09-14 10:05:09 -07:00
}
2022-01-27 14:31:12 +07:00
export async function sendMessage(channel: TextChannel | DMChannel | BaseGuildTextChannel | GuildTextBasedChannel | PartialDMChannel , user: User | undefined, options: string | MessagePayload | MessageOptions) {
2021-09-14 10:05:09 -07:00
let message;
try { message = await channel.send(options); }
catch(error) {
2022-01-30 20:09:19 +07:00
if(typeof user === 'undefined') {
return Logger.error(`Cannot find available destinations to send the message CID: ${channel.id} C_ERR: ${error}`);
}
2021-09-14 10:05:09 -07:00
try { message = await user.send(options); }
catch(errorDM) {
Logger.error(`Cannot find available destinations to send the message CID: ${channel.id} UID: ${user.id} C_ERR: ${error} DM_ERR: ${errorDM}`);
return;
}
} finally {
return message;
}
}
export async function sendReply(rMessage: Message, options: string | MessagePayload | MessageOptions) {
let message;
try { message = await rMessage.reply(options); }
catch(error) {
try { message = await rMessage.author.send(options); }
catch(errorDM) {
Logger.error(`Cannot find available destinations to reply the message to. MID: ${rMessage.id} CID: ${rMessage.channel.id} UID: ${rMessage.author.id} C_ERR: ${error} DM_ERR: ${errorDM}`);
return;
}
} finally {
return message;
}
}
2021-09-17 20:37:34 -07:00
export async function sendMessageOrInteractionResponse(data: Message | Interaction, payload: MessageOptions | InteractionReplyOptions, replace = false) {
2021-09-17 20:37:34 -07:00
const isSlashCommand = data instanceof Interaction && data.isCommand();
const isMessage = data instanceof Message;
2022-01-30 20:09:19 +07:00
if(isSlashCommand || (data instanceof Interaction && data.isSelectMenu())) {
2021-09-17 20:37:34 -07:00
if(!data.replied) {
let message;
2021-09-19 08:39:16 -07:00
try {
if(!data.deferred)
return await data.reply(payload);
else
return await data.editReply(payload);
}
2021-09-17 20:37:34 -07:00
catch(errorDM) {
Logger.error(`Cannot find available destinations to send the message CID: ${data.channel!.id} UID: ${data.user.id} DM_ERR: ${errorDM}`);
return;
} finally {
return message;
}
}
else {
let message;
2021-09-18 06:28:41 -07:00
try {
if(replace)
return await data.editReply(payload);
else
return await data.followUp(payload);
}
2021-09-17 20:37:34 -07:00
catch(errorDM) {
Logger.error(`Cannot find available destinations to send the message CID: ${data.channel!.id} UID: ${data.user.id} DM_ERR: ${errorDM}`);
return;
} finally {
return message;
}
}
}
else if(isMessage) return await sendReply(data, payload);
}
2021-09-14 10:05:09 -07:00
export function getEmotes() {
return emotes;
}