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

84 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-06-25 02:32:56 +00:00
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
CacheType,
ChatInputCommandInteraction,
} from 'discord.js';
2023-07-11 19:54:57 +00:00
import { commands } from '..';
import discord from '../../../../discord.json' assert { type: 'json' };
2023-10-17 20:58:36 -05:00
import { UserError } from '../../../core/blitzkrieg/userError';
2023-10-14 11:32:02 -05:00
import embedNegative from '../../../core/discord/embedNegative';
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';
2023-06-25 02:32:56 +00:00
export default async function handleChatInputCommand(
interaction: ChatInputCommandInteraction<CacheType>,
) {
2023-08-04 18:24:20 +00:00
const registry = (await commands)[interaction.commandName];
2023-06-25 02:32:56 +00:00
await interaction.deferReply();
if (registry.inPreview && interaction.guildId !== discord.tres_guild_id) {
interaction.editReply({
embeds: [
embedWarning(
`\`/${registry.command.name}\` is in Public Preview`,
'[Join the official Discord server](https://discord.gg/nDt7AjGJQH) to gain early access to this command before it is released to the public.',
),
],
});
return;
}
2023-06-25 02:32:56 +00:00
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
if (psa.data) {
if (!reply.embeds) reply.embeds = [];
2023-06-27 02:22:28 +00:00
reply.embeds.push(embedWarning(psa.data.title, psa.data.description));
2023-06-25 02:32:56 +00:00
}
await interaction.editReply(reply);
} catch (error) {
2023-10-31 11:56:06 -05:00
const components = [
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setLabel('Get Help on Discord Server')
.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({
embeds: [
embedNegative(
error.message,
(error.cause as string | undefined) ??
'No further information is available.',
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({
embeds: [
embedNegative(
'Blitzkrieg ran into an error!',
"We're so sorry about this. Feel free to [join the official Discord server](https://discord.gg/nDt7AjGJQH) to get help.",
),
],
components,
});
}
2023-06-25 02:32:56 +00:00
}
}