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

141 lines
4.4 KiB
TypeScript
Raw Normal View History

2025-09-27 18:01:20 -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,
2025-10-13 23:20:26 -05:00
type CacheType,
2023-06-25 02:32:56 +00:00
ChatInputCommandInteraction,
EmbedBuilder,
2025-10-13 23:20:26 -05:00
type Interaction,
2025-01-16 14:10:28 -06:00
REST,
Routes,
2023-06-25 02:32:56 +00:00
SlashCommandBuilder,
2025-10-13 23:20:26 -05:00
type SlashCommandOptionsOnlyBuilder,
type SlashCommandSubcommandsOnlyBuilder,
2025-09-27 18:01:20 -05:00
} from "discord.js";
import { aboutCommand } from "../../commands/about";
import { breakdownCommand } from "../../commands/breakdown";
import { debugCommand } from "../../commands/debug";
import { evolutionCommand } from "../../commands/evolution";
import { fullStatsCommand } from "../../commands/fullStats";
import { inactiveCommand } from "../../commands/inactive";
import { verifyCommand } from "../../commands/link";
import { ownedTanksCommand } from "../../commands/ownedTanks";
import { permissionsCommand } from "../../commands/permissions";
import { pingCommand } from "../../commands/ping";
import { playerInfoCommand } from "../../commands/playerInfo";
import { ratingCommand } from "../../commands/rating";
import { ratingLeaderboardCommand } from "../../commands/ratingLeaderboard";
import { replayCommand } from "../../commands/replay";
import { researchCommand } from "../../commands/research";
import { searchClansCommand } from "../../commands/searchClans";
import { searchPlayersCommand } from "../../commands/searchPlayers";
import { statsCommand } from "../../commands/stats";
import { todayCommand } from "../../commands/today";
import { RenderConfiguration } from "../../core/blitzkit/renderConfiguration";
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
2025-09-27 18:01:20 -05:00
| React.JSX.Element
2023-10-11 18:26:35 -05:00
| 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
2025-09-27 18:01:20 -05:00
| Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">;
2024-03-06 15:38:53 -06:00
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
}
2025-01-15 20:14:05 -06:00
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
}
2025-01-15 20:14:05 -06:00
2024-03-06 15:38:53 -06:00
interface CommandRegistryExplicit extends CommandRegistryBase {
2024-01-19 21:32:46 -06:00
handlesInteraction?: false;
handler: (
2025-09-27 18:01:20 -05:00
interaction: ChatInputCommandInteraction<CacheType>
2024-01-19 21:32:46 -06:00
) => InteractionReturnable;
2024-03-06 15:38:53 -06:00
}
2025-01-15 20:14:05 -06:00
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>[] = [
2025-03-30 12:53:14 -05:00
// auctionCommand,
2025-01-16 14:10:28 -06:00
permissionsCommand,
debugCommand,
aboutCommand,
inactiveCommand,
ownedTanksCommand,
playerInfoCommand,
searchClansCommand,
searchPlayersCommand,
fullStatsCommand,
breakdownCommand,
verifyCommand,
pingCommand,
evolutionCommand,
statsCommand,
ratingLeaderboardCommand,
todayCommand,
researchCommand,
ratingCommand,
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) => {
2025-01-16 14:10:28 -06:00
return rawCommands.reduce<Record<string, CommandRegistry>>(
(commands, registry, index) => {
2025-09-27 18:01:20 -05:00
if (registry.status === "rejected") {
2025-01-16 14:10:28 -06:00
console.warn(
`Command ${index} failed to load; skipping...`,
2025-09-27 18:01:20 -05:00
registry.reason
2025-01-16 14:10:28 -06:00
);
return commands;
}
return { ...commands, [registry.value.command.name]: registry.value };
},
2025-09-27 18:01:20 -05:00
{}
2025-01-16 14:10:28 -06:00
);
2024-03-03 22:21:06 -06:00
});
2023-06-25 02:32:56 +00:00
2025-01-16 14:10:28 -06:00
const rest = new REST().setToken(assertSecret(import.meta.env.DISCORD_TOKEN));
2023-06-25 02:32:56 +00:00
2025-01-16 14:10:28 -06:00
commands.then((awaitedCommands) => {
const body = Object.values(awaitedCommands).map((registry) =>
2025-09-27 18:01:20 -05:00
registry.command.toJSON()
2025-01-16 14:10:28 -06:00
);
2023-06-25 02:32:56 +00:00
2025-01-16 14:10:28 -06:00
rest.put(
Routes.applicationCommands(assertSecret(import.meta.env.DISCORD_CLIENT_ID)),
2025-09-27 18:01:20 -05:00
{ body }
2025-01-16 14:10:28 -06:00
);
});
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()) {
2025-01-16 14:10:28 -06:00
handleAutocomplete(interaction);
2023-06-25 02:32:56 +00:00
} else if (interaction.isChatInputCommand()) {
2025-01-16 14:10:28 -06:00
handleChatInputCommand(interaction);
2023-06-25 02:32:56 +00:00
} else if (interaction.isButton()) {
2025-01-16 14:10:28 -06:00
handleButton(interaction);
2023-06-25 02:32:56 +00:00
}
}