Files
Yumi/src/utils/DiscordMessage.ts
T

243 lines
7.3 KiB
TypeScript
Raw Normal View History

2022-06-06 14:59:53 +07:00
import {
2022-06-29 15:27:42 +07:00
EmbedBuilder,
2022-06-06 14:59:53 +07:00
User,
MessagePayload,
2022-10-05 12:34:19 +07:00
BaseMessageOptions,
2022-06-06 14:59:53 +07:00
GuildTextBasedChannel,
TextChannel,
DMChannel,
PartialDMChannel,
BaseGuildTextChannel,
Message,
ColorResolvable,
2022-06-29 15:27:42 +07:00
InteractionReplyOptions,
2022-06-29 18:59:06 +07:00
BaseInteraction,
BaseGuildVoiceChannel,
VoiceChannel
2022-06-06 14:59:53 +07:00
} from 'discord.js';
import App from '..';
import { HybridInteractionMessage } from './DiscordModule';
import Logger from '../libs/Logger';
2021-09-14 10:05:09 -07:00
2023-04-23 18:54:32 +07:00
const LOGGING_TAG = '[DiscordMessage]';
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 21:33:02 +07:00
interface EmbedData {
2022-06-07 03:08:27 +07:00
icon?: string | null;
2022-06-06 21:33:02 +07:00
title: string;
description?: any;
color?: ColorResolvable;
fields?: any;
2022-06-06 21:43:27 +07:00
user?: User | null;
2022-06-06 21:33:02 +07:00
setTimestamp: boolean;
}
2022-06-06 21:40:35 +07:00
interface EmbedDataPresets {
2022-06-07 03:08:27 +07:00
icon?: string | null;
2022-06-06 21:40:35 +07:00
title: string;
description?: any;
color?: ColorResolvable;
fields?: any;
2022-06-06 21:43:27 +07:00
user?: User | null;
2022-06-06 21:40:35 +07:00
setTimestamp?: boolean;
}
2022-06-06 21:33:02 +07:00
export function makeEmbed(data: EmbedData) {
2022-06-29 15:27:42 +07:00
const embed = new EmbedBuilder();
2022-06-06 21:33:02 +07:00
embed.setColor(data.color || '#FFFFFF');
2021-09-14 10:05:09 -07:00
2022-06-06 21:33:02 +07:00
if (!data.icon) embed.setTitle(data.title);
else embed.setTitle(`${data.icon} ${data.title}`);
2021-09-14 10:05:09 -07:00
2022-06-06 21:33:02 +07:00
if (data.description) embed.setDescription(data.description);
2021-09-14 10:05:09 -07:00
2022-06-06 21:33:02 +07:00
if (data.setTimestamp) embed.setTimestamp();
2021-09-14 10:05:09 -07:00
2022-06-06 21:33:02 +07:00
if (data.user)
2022-06-29 15:27:42 +07:00
embed.setFooter({
2022-06-06 21:33:02 +07:00
text: `${data.user.username} | v${App.version}`,
iconURL: `${data.user.displayAvatarURL()}?size=4096`
2022-06-29 15:27:42 +07:00
});
2021-09-15 06:05:47 -07:00
else
2022-06-29 15:27:42 +07:00
embed.setFooter({
2022-01-25 13:30:27 +07:00
text: `v${App.version}`
2022-06-29 15:27:42 +07:00
});
2021-09-14 10:05:09 -07:00
2022-06-06 21:33:02 +07:00
if (data.fields) embed.addFields(data.fields);
2022-06-06 14:59:53 +07:00
return embed;
2021-09-14 10:05:09 -07:00
}
2022-06-06 21:40:35 +07:00
export function makeInfoEmbed(data: EmbedDataPresets) {
2022-06-06 21:33:02 +07:00
return makeEmbed({
2022-06-07 03:08:27 +07:00
icon: !data.icon && data.icon !== null ? '🔮' : data.icon,
2022-06-06 21:40:35 +07:00
title: data.title,
description: data.description,
2022-06-06 21:33:02 +07:00
color: '#C7CEEA',
2022-06-06 21:40:35 +07:00
fields: data.fields,
user: data.user,
setTimestamp: data.setTimestamp || true
2022-06-06 21:33:02 +07:00
});
}
2022-06-06 21:43:27 +07:00
export function makeSuccessEmbed(data: EmbedDataPresets) {
2022-06-06 21:33:02 +07:00
return makeEmbed({
2022-06-07 03:08:27 +07:00
icon: !data.icon && data.icon !== null ? '✅' : data.icon,
2022-06-06 21:43:27 +07:00
title: data.title,
description: data.description,
2022-06-06 21:33:02 +07:00
color: '#B5EAD7',
2022-06-06 21:43:27 +07:00
fields: data.fields,
user: data.user,
setTimestamp: data.setTimestamp || true
2022-06-06 21:33:02 +07:00
});
2021-09-14 10:05:09 -07:00
}
2022-06-06 21:40:35 +07:00
export function makeWarningEmbed(data: EmbedDataPresets) {
2022-06-06 21:33:02 +07:00
return makeEmbed({
2022-06-07 03:08:27 +07:00
icon: !data.icon && data.icon !== null ? '⚠️' : data.icon,
2022-06-06 21:40:35 +07:00
title: data.title,
description: data.description,
2022-06-06 21:33:02 +07:00
color: '#FFEEAD',
2022-06-06 21:40:35 +07:00
fields: data.fields,
user: data.user,
setTimestamp: data.setTimestamp || true
2022-06-06 21:33:02 +07:00
});
}
2022-06-06 21:40:35 +07:00
export function makeErrorEmbed(data: EmbedDataPresets) {
2022-06-06 21:33:02 +07:00
return makeEmbed({
2022-06-07 03:08:27 +07:00
icon: !data.icon && data.icon !== null ? '❌' : data.icon,
2022-06-06 21:40:35 +07:00
title: data.title,
description: data.description,
2022-06-06 21:33:02 +07:00
color: '#FF9AA2',
2022-06-06 21:40:35 +07:00
fields: data.fields,
user: data.user,
setTimestamp: data.setTimestamp || true
2022-06-06 21:33:02 +07:00
});
2021-09-14 10:05:09 -07:00
}
2022-06-06 21:40:35 +07:00
export function makeProcessingEmbed(data: EmbedDataPresets) {
2022-06-06 21:33:02 +07:00
return makeEmbed({
2022-06-07 03:08:27 +07:00
icon: !data.icon && data.icon !== null ? getEmotes().yumiloading : data.icon,
2022-06-06 21:40:35 +07:00
title: data.title,
description: data.description,
2022-06-06 21:33:02 +07:00
color: '#E2F0CB',
2022-06-06 21:40:35 +07:00
fields: data.fields,
user: data.user,
setTimestamp: data.setTimestamp || true
2022-06-06 21:33:02 +07:00
});
2021-09-14 10:05:09 -07:00
}
2022-06-06 14:59:53 +07:00
export async function sendMessage(
2022-10-05 12:34:19 +07:00
channel:
| TextChannel
| DMChannel
| BaseGuildTextChannel
| GuildTextBasedChannel
| PartialDMChannel
| BaseGuildTextChannel
| BaseGuildVoiceChannel,
2022-06-06 14:59:53 +07:00
user: User | undefined,
2022-10-05 12:34:19 +07:00
options: string | MessagePayload | BaseMessageOptions
2022-06-06 14:59:53 +07:00
) {
2023-04-23 18:54:32 +07:00
Logger.verbose(LOGGING_TAG, `Sending message, ${JSON.stringify(options)})}`);
2021-09-14 10:05:09 -07:00
let message;
2022-06-06 14:59:53 +07:00
try {
2022-10-05 12:34:19 +07:00
if (channel instanceof BaseGuildVoiceChannel) message = await (channel as VoiceChannel).send(options);
else message = await channel.send(options);
2022-06-06 14:59:53 +07:00
} catch (error) {
if (typeof user === 'undefined') {
2024-03-04 23:04:32 +07:00
Logger.error(`Cannot find available destinations to send the message CID: ${channel.id} C_ERR: ${error}`);
return;
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 sendHybridInteractionMessageResponse(
data: HybridInteractionMessage,
2022-10-05 12:34:19 +07:00
payload: BaseMessageOptions | InteractionReplyOptions,
2022-06-06 14:59:53 +07:00
replace = false
2022-06-29 15:27:42 +07:00
): Promise<Message | BaseInteraction | undefined> {
2023-04-23 18:54:32 +07:00
Logger.verbose(LOGGING_TAG, `Sending hybrid interaction message response, ${JSON.stringify(payload)})}`);
2023-06-04 16:43:55 +07:00
if (data.isApplicationCommand() || data.isButton() || data.isStringSelectMenu()) {
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(
2022-06-06 21:33:02 +07:00
`Cannot find available destinations to send the message CID: ${messageComponent.channel!.id} UID: ${
messageComponent.user.id
} DM_ERR: ${errorDM}`
2022-06-06 14:59:53 +07:00
);
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-06-06 21:33:02 +07:00
else return (await messageComponent.followUp(payload as InteractionReplyOptions)) as Message;
2022-06-06 14:59:53 +07:00
} catch (errorDM) {
Logger.error(
2022-06-06 21:33:02 +07:00
`Cannot find available destinations to send the message CID: ${messageComponent.channel!.id} UID: ${
messageComponent.user.id
} DM_ERR: ${errorDM}`
2022-06-06 14:59:53 +07:00
);
2022-03-03 20:56:49 +07:00
return;
} finally {
return message;
}
}
2022-10-05 12:34:19 +07:00
} else if (data.isMessage()) return await sendReply(data.getMessage(), payload as BaseMessageOptions);
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
}
2022-06-06 21:40:35 +07:00
2022-10-05 12:34:19 +07:00
async function sendReply(rMessage: Message, options: string | MessagePayload | BaseMessageOptions) {
2023-04-23 18:54:32 +07:00
Logger.verbose(LOGGING_TAG, `Sending reply, ${JSON.stringify(options)})}`);
2022-06-06 21:40:35 +07:00
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;
}
2022-06-06 21:43:27 +07:00
}