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

121 lines
3.5 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,
ButtonStyle,
CacheType,
ChatInputCommandInteraction,
2023-11-14 13:42:15 -06:00
InteractionReplyOptions,
2023-06-25 02:32:56 +00:00
} from 'discord.js';
2023-07-11 19:54:57 +00:00
import { commands } from '..';
2023-11-14 13:42:15 -06:00
import buttonLink from '../../../core/discord/buttonLink';
2023-10-14 11:32:02 -05:00
import embedWarning from '../../../core/discord/embedWarning';
2023-10-14 11:36:10 -05:00
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-02-17 10:20:43 -06:00
import { UserError } from '../../../hooks/userError';
2023-06-25 02:32:56 +00:00
export default async function handleChatInputCommand(
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
2023-06-27 02:02:00 +00:00
const reply = await normalizeInteractionReturnable(returnable!);
2023-06-25 02:32:56 +00:00
2023-11-15 19:30:03 -06:00
if (
psa.data &&
!psa.data.secret &&
(psa.data.commands === undefined ||
psa.data.commands.includes(interaction.commandName))
) {
2023-11-14 13:42:15 -06:00
if (psa.data.type === 'embed') {
if (!reply.embeds) reply.embeds = [];
reply.embeds.push(embedWarning(psa.data.title, psa.data.description));
} else {
if (!reply.files) reply.files = [];
reply.files.push(new AttachmentBuilder(psa.data.image));
reply.content = reply.content ?? psa.data.title;
}
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-06-25 02:32:56 +00:00
}
await interaction.editReply(reply);
2023-11-14 13:42:15 -06:00
2023-11-15 19:30:03 -06:00
if (
psa.data &&
psa.data.secret &&
(psa.data.commands === undefined ||
psa.data.commands.includes(interaction.commandName))
) {
2023-11-14 13:42:15 -06:00
const followUp: InteractionReplyOptions = {
ephemeral: true,
};
if (psa.data.type === 'embed') {
if (!followUp.embeds) followUp.embeds = [];
followUp.embeds.push(
embedWarning(psa.data.title, psa.data.description),
);
} else {
if (!followUp.files) followUp.files = [];
followUp.files.push(new AttachmentBuilder(psa.data.image));
followUp.content = followUp.content ?? psa.data.title;
}
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-06-25 02:32:56 +00:00
} catch (error) {
2024-03-09 12:01:41 -06:00
const { t } = translator(interaction.locale);
2023-10-31 11:56:06 -05:00
const components = [
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
2024-03-09 12:01:41 -06:00
.setLabel(t`bot.common.errors.get_help`)
2023-10-31 11:56:06 -05:00
.setURL('https://discord.gg/nDt7AjGJQH')
.setStyle(ButtonStyle.Link),
),
];
2023-06-25 02:32:56 +00:00
if (error instanceof UserError) {
2023-10-31 11:56:06 -05:00
interaction.editReply({
2024-01-21 18:29:32 -06:00
content: error.message,
2023-10-31 11:56:06 -05:00
components,
});
} else {
console.error(interaction.commandName, error);
2023-10-31 11:56:06 -05:00
interaction.editReply({
2024-03-09 12:01:41 -06:00
content: t`bot.common.errors.uncaught_error`,
2023-10-31 11:56:06 -05:00
components,
});
}
2023-06-25 02:32:56 +00:00
}
}