Files
blitzkit/packages/bot/src/events/interactionCreate/index.ts
T

141 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-09-07 21:45:00 -05:00
import { assertSecret } from '@blitzkit/core';
2023-06-25 02:32:56 +00:00
import {
2023-10-10 21:20:01 -05:00
AttachmentBuilder,
2023-06-25 02:32:56 +00:00
AutocompleteInteraction,
ButtonBuilder,
ButtonInteraction,
CacheType,
ChatInputCommandInteraction,
EmbedBuilder,
Interaction,
REST,
Routes,
SlashCommandBuilder,
2024-06-23 20:59:03 -05:00
SlashCommandOptionsOnlyBuilder,
2023-06-25 02:32:56 +00:00
SlashCommandSubcommandsOnlyBuilder,
} from 'discord.js';
2023-10-01 10:56:03 -05:00
import { aboutCommand } from '../../commands/about';
2023-10-15 15:24:07 -05:00
import { breakdownCommand } from '../../commands/breakdown';
2023-07-11 19:54:57 +00:00
import { debugCommand } from '../../commands/debug';
import { evolutionCommand } from '../../commands/evolution';
import { fullStatsCommand } from '../../commands/fullStats';
import { inactiveCommand } from '../../commands/inactive';
2023-10-10 21:20:01 -05:00
import { verifyCommand } from '../../commands/link';
2023-07-11 19:54:57 +00:00
import { ownedTanksCommand } from '../../commands/ownedTanks';
2024-01-21 18:43:07 -06:00
import { permissionsCommand } from '../../commands/permissions';
2023-07-11 19:54:57 +00:00
import { pingCommand } from '../../commands/ping';
import { playerInfoCommand } from '../../commands/playerInfo';
2024-11-20 10:50:09 -06:00
import { ratingCommand } from '../../commands/rating';
import { ratingLeaderboardCommand } from '../../commands/ratingLeaderboard';
2024-04-23 16:07:59 -05:00
import { replayCommand } from '../../commands/replay';
2024-03-14 16:43:28 -05:00
import { researchCommand } from '../../commands/research';
2023-07-11 19:54:57 +00:00
import { searchClansCommand } from '../../commands/searchClans';
import { searchPlayersCommand } from '../../commands/searchPlayers';
import { statsCommand } from '../../commands/stats';
2024-01-19 21:32:46 -06:00
import { todayCommand } from '../../commands/today';
2024-09-08 10:28:54 -05:00
import { RenderConfiguration } from '../../core/blitzkit/renderConfiguration';
2024-09-08 10:11:24 -05:00
import { handleAutocomplete } from './handlers/autocomplete';
import { handleButton } from './handlers/button';
import { handleChatInputCommand } from './handlers/chatInputCommand';
2023-06-25 02:32:56 +00:00
export type InteractionRawReturnable =
2023-07-10 19:14:16 +00:00
| string
2023-06-25 02:32:56 +00:00
| EmbedBuilder
| ButtonBuilder
2023-10-10 21:20:01 -05:00
| AttachmentBuilder
2024-04-24 16:22:48 -05:00
| RenderConfiguration
2023-10-11 18:26:35 -05:00
| JSX.Element
| null;
2023-06-25 02:32:56 +00:00
export type InteractionIterableReturnable =
| InteractionRawReturnable
| InteractionRawReturnable[];
export type InteractionReturnable =
| InteractionIterableReturnable
| Promise<InteractionIterableReturnable>;
2024-03-06 15:38:53 -06:00
interface CommandRegistryBase {
command:
| SlashCommandBuilder
| SlashCommandSubcommandsOnlyBuilder
2024-06-23 20:59:03 -05:00
| SlashCommandOptionsOnlyBuilder
2024-03-06 15:38:53 -06:00
| Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
2023-06-25 02:32:56 +00:00
autocomplete?: (interaction: AutocompleteInteraction<CacheType>) => void;
button?: (interaction: ButtonInteraction<CacheType>) => InteractionReturnable;
2024-03-06 15:38:53 -06:00
}
interface CommandRegistryImplicit extends CommandRegistryBase {
2024-01-19 21:32:46 -06:00
handlesInteraction: true;
handler: (interaction: ChatInputCommandInteraction<CacheType>) => void;
2024-03-06 15:38:53 -06:00
}
interface CommandRegistryExplicit extends CommandRegistryBase {
2024-01-19 21:32:46 -06:00
handlesInteraction?: false;
handler: (
interaction: ChatInputCommandInteraction<CacheType>,
) => InteractionReturnable;
2024-03-06 15:38:53 -06:00
}
export type CommandRegistry = CommandRegistryImplicit | CommandRegistryExplicit;
2023-06-25 02:32:56 +00:00
2024-03-06 15:34:10 -06:00
export const COMMANDS_RAW: Promise<CommandRegistry>[] = [
2024-01-21 18:43:07 -06:00
permissionsCommand,
2023-07-10 19:14:16 +00:00
debugCommand,
2023-10-01 10:56:03 -05:00
aboutCommand,
2023-07-10 19:14:16 +00:00
inactiveCommand,
ownedTanksCommand,
playerInfoCommand,
searchClansCommand,
searchPlayersCommand,
fullStatsCommand,
2023-10-15 15:24:07 -05:00
breakdownCommand,
2023-07-10 19:14:16 +00:00
verifyCommand,
pingCommand,
evolutionCommand,
statsCommand,
2024-11-20 10:50:09 -06:00
ratingLeaderboardCommand,
2024-01-19 21:32:46 -06:00
todayCommand,
2024-03-14 16:43:28 -05:00
researchCommand,
2024-11-20 10:50:09 -06:00
ratingCommand,
2024-04-23 16:07:59 -05:00
replayCommand,
2023-07-10 19:14:16 +00:00
];
2023-08-04 02:13:21 +00:00
2024-03-03 22:21:06 -06:00
export const commands = Promise.allSettled(COMMANDS_RAW).then((rawCommands) => {
2024-03-06 15:34:10 -06:00
return rawCommands.reduce<Record<string, CommandRegistry>>(
2024-03-07 18:27:05 -06:00
(commands, registry, index) => {
if (registry.status === 'rejected') {
console.warn(
`Command ${index} failed to load; skipping...`,
registry.reason,
);
return commands;
}
2024-07-24 13:14:21 -05:00
2024-03-03 22:21:06 -06:00
return { ...commands, [registry.value.command.name]: registry.value };
2023-08-04 02:13:21 +00:00
},
{},
2024-03-03 22:21:06 -06:00
);
});
2023-06-25 02:32:56 +00:00
2024-11-29 12:11:51 -06:00
const rest = new REST().setToken(assertSecret(import.meta.env.DISCORD_TOKEN));
2023-06-25 02:32:56 +00:00
2023-08-04 02:13:21 +00:00
commands.then((awaitedCommands) => {
2024-07-21 19:41:37 -05:00
const body = Object.values(awaitedCommands).map((registry) =>
registry.command.toJSON(),
);
2023-06-25 02:32:56 +00:00
2024-08-27 14:40:43 -05:00
rest.put(
2024-11-29 12:11:51 -06:00
Routes.applicationCommands(assertSecret(import.meta.env.DISCORD_CLIENT_ID)),
2024-08-27 14:40:43 -05:00
{
body,
},
);
2023-06-25 02:32:56 +00:00
});
2024-09-08 10:11:24 -05:00
export async function interactionCreate(interaction: Interaction<CacheType>) {
2023-06-25 02:32:56 +00:00
if (interaction.isAutocomplete()) {
handleAutocomplete(interaction);
} else if (interaction.isChatInputCommand()) {
handleChatInputCommand(interaction);
} else if (interaction.isButton()) {
handleButton(interaction);
}
}