Files
Yumi/src/utils/DiscordMessage.ts
T

275 lines
8.0 KiB
TypeScript
Raw Normal View History

2022-06-06 14:59:53 +07:00
import {
MessageEmbed,
User,
MessagePayload,
MessageOptions,
GuildTextBasedChannel,
TextChannel,
DMChannel,
PartialDMChannel,
BaseGuildTextChannel,
Message,
ColorResolvable,
Interaction,
InteractionReplyOptions,
CommandInteraction
} from 'discord.js';
import App from '..';
import { HybridInteractionMessage } from './DiscordModule';
import Logger from '../libs/Logger';
2021-09-14 10:05:09 -07:00
const emotes = {
2022-06-06 14:59:53 +07:00
yumiloading: '<a:yumiloading:983269480085983262>'
2021-09-14 10:05:09 -07:00
};
2022-06-06 14:59:53 +07:00
export function makeEmbed(
icon: string | undefined,
title: string,
description: any,
color: ColorResolvable,
fields: any,
user: User,
setTimestamp: boolean
) {
2021-09-14 10:05:09 -07:00
const embed = new MessageEmbed();
embed.setColor(color || '#FFFFFF');
2022-06-06 14:59:53 +07:00
if (typeof icon === 'undefined') embed.setTitle(title);
else embed.setTitle(`${icon} ${title}`);
2021-09-14 10:05:09 -07:00
2022-06-06 14:59:53 +07:00
if (typeof description !== 'undefined') embed.setDescription(description);
2021-09-14 10:05:09 -07:00
2022-06-06 14:59:53 +07:00
if (setTimestamp) embed.setTimestamp();
2021-09-14 10:05:09 -07:00
2022-06-06 14:59:53 +07:00
if (typeof user !== 'undefined')
2022-01-25 13:30:27 +07:00
embed.footer = {
text: `${user.username} | v${App.version}`,
2022-06-06 13:00:38 +07:00
iconURL: `${user.displayAvatarURL()}?size=4096`
2022-06-06 14:59:53 +07:00
};
2021-09-15 06:05:47 -07:00
else
2022-01-25 13:30:27 +07:00
embed.footer = {
text: `v${App.version}`
2022-06-06 14:59:53 +07:00
};
2021-09-14 10:05:09 -07:00
2022-06-06 14:59:53 +07:00
if (typeof fields !== 'undefined') embed.addFields(fields);
return embed;
2021-09-14 10:05:09 -07:00
}
export function makeSuccessEmbed(options: any) {
2022-06-06 14:59:53 +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) {
2022-06-06 14:59:53 +07:00
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) {
2022-06-06 14:59:53 +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) {
2022-06-06 14:59:53 +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) {
2022-06-06 14:59:53 +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-06-06 14:59:53 +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;
2022-06-06 14:59:53 +07:00
try {
message = await channel.send(options);
} catch (error) {
if (typeof user === 'undefined') {
return Logger.error(
`Cannot find available destinations to send the message CID: ${channel.id} C_ERR: ${error}`
);
2022-01-30 20:09:19 +07:00
}
2022-06-06 14:59:53 +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}`
);
2021-09-14 10:05:09 -07:00
return;
}
} finally {
return message;
}
}
2022-06-06 14:59:53 +07:00
export async function sendReply(
rMessage: Message,
options: string | MessagePayload | MessageOptions
) {
2021-09-14 10:05:09 -07:00
let message;
2022-06-06 14:59:53 +07:00
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}`
);
2021-09-14 10:05:09 -07:00
return;
}
} finally {
return message;
}
}
2022-06-06 14:59:53 +07:00
export async function sendMessageOrInteractionResponse(
data: Message | Interaction,
payload: MessageOptions | InteractionReplyOptions,
replace = false
) {
2022-03-03 22:18:46 +07:00
const isSlashCommand = data instanceof CommandInteraction && data.isCommand();
2021-09-17 20:37:34 -07:00
const isMessage = data instanceof Message;
2022-06-06 14:59:53 +07:00
if (
isSlashCommand ||
(data instanceof Interaction && (data.isSelectMenu() || data.isButton()))
) {
if (!data.replied) {
2021-09-17 20:37:34 -07:00
let message;
2021-09-19 08:39:16 -07:00
try {
2022-06-06 14:59:53 +07:00
if (!data.deferred) return await data.reply(payload as InteractionReplyOptions);
else return await data.editReply(payload);
} catch (errorDM) {
Logger.error(
`Cannot find available destinations to send the message CID: ${
data.channel!.id
} UID: ${data.user.id} DM_ERR: ${errorDM}`
);
2021-09-17 20:37:34 -07:00
return;
} finally {
return message;
}
2022-06-06 14:59:53 +07:00
} else {
2021-09-17 20:37:34 -07:00
let message;
2022-06-06 14:59:53 +07:00
try {
if (replace) return await data.editReply(payload);
else return await data.followUp(payload as InteractionReplyOptions);
} catch (errorDM) {
Logger.error(
`Cannot find available destinations to send the message CID: ${
data.channel!.id
} UID: ${data.user.id} DM_ERR: ${errorDM}`
);
2021-09-17 20:37:34 -07:00
return;
} finally {
return message;
}
}
2022-06-06 14:59:53 +07:00
} else if (isMessage) return await sendReply(data, payload as MessageOptions);
2021-09-17 20:37:34 -07:00
}
2022-06-06 14:59:53 +07:00
export async function sendHybridInteractionMessageResponse(
data: HybridInteractionMessage,
payload: MessageOptions | InteractionReplyOptions,
replace = false
): Promise<Message | Interaction | undefined> {
if (data.isSlashCommand() || data.isButton() || data.isSelectMenu()) {
2022-03-03 20:56:49 +07:00
const messageComponent = data.getMessageComponentInteraction();
2022-06-06 14:59:53 +07:00
if (!messageComponent.replied) {
2022-03-03 20:56:49 +07:00
let message;
try {
2022-06-06 14:59:53 +07:00
if (!messageComponent.deferred) {
2022-06-06 13:00:38 +07:00
await messageComponent.reply(payload as InteractionReplyOptions);
2022-03-03 20:56:49 +07:00
return messageComponent;
2022-06-06 14:59:53 +07:00
} else {
2022-03-03 20:56:49 +07:00
await messageComponent.editReply(payload);
return messageComponent;
}
2022-06-06 14:59:53 +07:00
} catch (errorDM) {
Logger.error(
`Cannot find available destinations to send the message CID: ${
messageComponent.channel!.id
} UID: ${messageComponent.user.id} DM_ERR: ${errorDM}`
);
2022-03-03 20:56:49 +07:00
return;
} finally {
return message;
}
2022-06-06 14:59:53 +07:00
} else {
2022-03-03 20:56:49 +07:00
let message;
2022-06-06 14:59:53 +07:00
try {
if (replace) return (await messageComponent.editReply(payload)) as Message;
2022-03-03 20:56:49 +07:00
else
2022-06-06 14:59:53 +07:00
return (await messageComponent.followUp(
payload as InteractionReplyOptions
)) as Message;
} catch (errorDM) {
Logger.error(
`Cannot find available destinations to send the message CID: ${
messageComponent.channel!.id
} UID: ${messageComponent.user.id} DM_ERR: ${errorDM}`
);
2022-03-03 20:56:49 +07:00
return;
} finally {
return message;
}
}
2022-06-06 14:59:53 +07:00
} else if (data.isMessage())
return await sendReply(data.getMessage(), payload as MessageOptions);
2022-03-03 20:56:49 +07:00
}
2021-09-14 10:05:09 -07:00
export function getEmotes() {
return emotes;
2022-06-06 14:59:53 +07:00
}