Files
Yumi/src/utils/DiscordMessage.ts
T

229 lines
6.6 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 21:33:02 +07:00
interface EmbedData {
icon?: string;
title: string;
description?: any;
color?: ColorResolvable;
fields?: any;
user?: User;
setTimestamp: boolean;
}
2022-06-06 21:40:35 +07:00
interface EmbedDataPresets {
icon?: string;
title: string;
description?: any;
color?: ColorResolvable;
fields?: any;
user?: User;
setTimestamp?: boolean;
}
2022-06-06 21:33:02 +07:00
export function makeEmbed(data: EmbedData) {
2021-09-14 10:05:09 -07:00
const embed = new MessageEmbed();
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-01-25 13:30:27 +07:00
embed.footer = {
2022-06-06 21:33:02 +07:00
text: `${data.user.username} | v${App.version}`,
iconURL: `${data.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 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-06 21:40:35 +07:00
icon: !data.icon ? '🔮' : data.icon,
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:40:35 +07:00
export function makeSuccessEmbed(options: EmbedDataPresets) {
2022-06-06 21:33:02 +07:00
return makeEmbed({
icon: !options.icon ? '✅' : options.icon,
title: options.title,
description: options.description,
color: '#B5EAD7',
fields: options.fields,
user: options.user,
setTimestamp: options.setTimestamp || true
});
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-06 21:40:35 +07:00
icon: !data.icon ? '⚠️' : data.icon,
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-06 21:40:35 +07:00
icon: !data.icon ? '❌' : data.icon,
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-06 21:40:35 +07:00
icon: !data.icon ? getEmotes().yumiloading : data.icon,
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-06-06 21:33:02 +07:00
channel: TextChannel | DMChannel | BaseGuildTextChannel | GuildTextBasedChannel | PartialDMChannel,
2022-06-06 14:59:53 +07:00
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 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(
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-06-06 21:33:02 +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
}
2022-06-06 21:40:35 +07:00
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;
}
}