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-07-15 18:44:20 +00:00
import discord from '../../../../discord.json' assert { type : 'json' };
2023-10-17 20:58:36 -05:00
import { UserError } from '../../../core/blitzkrieg/userError' ;
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' ;
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 ();
2023-11-01 11:21:30 -05:00
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
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 ) {
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
2023-11-01 11:21:30 -05: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 {
2023-11-01 11:21:30 -05:00
console . error ( interaction . commandName , error );
2023-10-31 11:56:06 -05:00
interaction . editReply ({
2024-01-21 18:29:32 -06:00
content :
"# Blitzkrieg ran into an error!\nWe're so sorry about this. Feel free to [join the official Discord server](https://discord.gg/nDt7AjGJQH) to get help." ,
2023-10-31 11:56:06 -05:00
components ,
});
}
2023-06-25 02:32:56 +00:00
}
}