Files
blitzkit/packages/bot/src/events/interactionCreate/handlers/chatInputCommand.ts
T

149 lines
4.7 KiB
TypeScript
Raw Normal View History

2023-06-25 02:32:56 +00:00
import {
ActionRowBuilder,
2023-11-14 13:42:15 -06:00
AttachmentBuilder,
2023-06-25 02:32:56 +00:00
ButtonBuilder,
CacheType,
ChatInputCommandInteraction,
2023-11-14 13:42:15 -06:00
InteractionReplyOptions,
2023-06-25 02:32:56 +00:00
} from 'discord.js';
2024-03-16 00:06:44 -05:00
import { InteractionRawReturnable, commands } from '..';
2024-09-08 10:11:24 -05:00
import { buttonLink } from '../../../core/discord/buttonLink';
import { embedWarning } from '../../../core/discord/embedWarning';
import { normalizeInteractionReturnable } from '../../../core/discord/normalizeInteractionReturnable';
2023-10-17 20:58:36 -05:00
import { psa } from '../../../core/discord/psa';
2024-03-09 12:01:41 -06:00
import { translator } from '../../../core/localization/translator';
2024-06-23 20:59:03 -05:00
import { Writeable } from '../../../types/writable';
2023-06-25 02:32:56 +00:00
2024-09-08 10:11:24 -05:00
export async function handleChatInputCommand(
2023-06-25 02:32:56 +00:00
interaction: ChatInputCommandInteraction<CacheType>,
) {
2024-01-19 21:32:46 -06:00
const awaitedCommands = await commands;
const registry = awaitedCommands[interaction.commandName];
2023-06-25 02:32:56 +00:00
await interaction.deferReply();
try {
2023-06-27 01:43:30 +00:00
const returnable = await registry.handler(interaction);
2023-06-25 02:32:56 +00:00
2023-06-27 01:43:30 +00:00
if (registry.handlesInteraction) return;
2023-06-25 02:32:56 +00:00
2024-03-16 00:06:44 -05:00
const chunks: InteractionRawReturnable[][] = [[]];
2023-06-25 02:32:56 +00:00
2024-03-16 00:06:44 -05:00
(Array.isArray(returnable) ? returnable : [returnable]).forEach(
(object) => {
if (typeof object === 'string') {
chunks.push([object]);
} else chunks.at(-1)?.push(object!);
},
);
2023-11-14 13:42:15 -06:00
2024-03-16 00:06:44 -05:00
let index = 0;
for (const chunk of chunks.filter((chunk) => chunk.length > 0)) {
const reply = await normalizeInteractionReturnable(chunk);
if (
psa.data &&
!psa.data.secret &&
(psa.data.commands === undefined ||
psa.data.commands.includes(interaction.commandName))
) {
2024-04-10 16:29:12 -05:00
if (psa.data.type === undefined || psa.data.type === 'embed') {
2024-03-16 00:06:44 -05:00
if (!reply.embeds) reply.embeds = [];
2024-04-10 16:29:12 -05:00
const embed = embedWarning(psa.data.title, psa.data.description);
if (psa.data.image) embed.setImage(psa.data.image);
2024-06-23 20:59:03 -05:00
(reply.embeds as Writeable<typeof reply.embeds>).push(embed);
2024-04-10 16:29:12 -05:00
} else if (psa.data.type === 'image') {
2024-03-16 00:06:44 -05:00
if (!reply.files) reply.files = [];
2024-06-23 20:59:03 -05:00
(reply.files as Writeable<typeof reply.files>).push(
new AttachmentBuilder(psa.data.image),
);
2024-03-16 00:06:44 -05:00
reply.content = reply.content ?? psa.data.title;
2023-11-14 13:42:15 -06:00
}
2024-03-16 00:06:44 -05:00
if (psa.data.links) {
if (!reply.components?.[0]) {
reply.components = [new ActionRowBuilder<ButtonBuilder>()];
}
(
reply.components[0] as ActionRowBuilder<ButtonBuilder>
).addComponents(
psa.data.links.map(({ title, url }) => buttonLink(url, title)),
);
}
2023-11-14 13:42:15 -06:00
}
2023-06-25 02:32:56 +00:00
2024-03-16 00:06:44 -05:00
if (index === 0) {
await interaction.editReply(reply);
2023-11-14 13:42:15 -06:00
} else {
2024-03-16 00:06:44 -05:00
await interaction.followUp(reply);
2023-11-14 13:42:15 -06:00
}
2024-03-16 00:06:44 -05:00
if (
psa.data &&
psa.data.secret &&
(psa.data.commands === undefined ||
psa.data.commands.includes(interaction.commandName))
) {
const followUp: InteractionReplyOptions = {
ephemeral: true,
};
2024-04-10 16:29:12 -05:00
if (psa.data.type === undefined || psa.data.type === 'embed') {
2024-03-16 00:06:44 -05:00
if (!followUp.embeds) followUp.embeds = [];
2024-04-10 16:29:12 -05:00
const embed = embedWarning(psa.data.title, psa.data.description);
if (psa.data.image) embed.setImage(psa.data.image);
2024-06-23 20:59:03 -05:00
(followUp.embeds as Writeable<typeof followUp.embeds>).push(embed);
2024-04-10 16:29:12 -05:00
} else if (psa.data.type === 'image') {
2024-03-16 00:06:44 -05:00
if (!followUp.files) followUp.files = [];
2024-06-23 20:59:03 -05:00
(followUp.files as Writeable<typeof followUp.files>).push(
new AttachmentBuilder(psa.data.image),
);
2024-03-16 00:06:44 -05:00
followUp.content = followUp.content ?? psa.data.title;
2023-11-14 13:42:15 -06:00
}
2024-03-16 00:06:44 -05:00
if (psa.data.links) {
if (!followUp.components?.[0]) {
followUp.components = [new ActionRowBuilder<ButtonBuilder>()];
}
(
followUp.components[0] as ActionRowBuilder<ButtonBuilder>
).addComponents(
psa.data.links.map(({ title, url }) => buttonLink(url, title)),
);
}
interaction.followUp(followUp);
2023-11-14 13:42:15 -06:00
}
2024-03-16 00:06:44 -05:00
index++;
2023-11-14 13:42:15 -06:00
}
2023-06-25 02:32:56 +00:00
} catch (error) {
2024-03-09 12:01:41 -06:00
const { t } = translator(interaction.locale);
2025-01-15 20:14:05 -06:00
// const components = [
// new ActionRowBuilder<ButtonBuilder>().addComponents(
// new ButtonBuilder()
// .setLabel(t`bot.common.errors.get_help`)
// .setURL('https://discord.gg/nDt7AjGJQH')
// .setStyle(ButtonStyle.Link),
// ),
// ];
// if (error instanceof UserError) {
// interaction.editReply({
// content: error.message,
// components,
// });
// } else {
// console.error(interaction.commandName, error);
// interaction.editReply({
// content: t`bot.common.errors.uncaught_error`,
// components,
// });
// }
2023-06-25 02:32:56 +00:00
}
}