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

160 lines
4.9 KiB
TypeScript
Raw Normal View History

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,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Routes,
SlashCommandBuilder,
SlashCommandSubcommandsOnlyBuilder,
} from 'discord.js';
import discord from '../../../discord.json' assert { type: 'json' };
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';
import { ratingsCommand } from '../../commands/ratings';
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';
2023-10-14 11:36:10 -05:00
import getClientId from '../../core/blitzkrieg/getClientId';
import isDev from '../../core/blitzkrieg/isDev';
2023-10-14 11:36:48 -05:00
import { secrets } from '../../core/blitzkrieg/secrets';
2023-07-11 19:54:57 +00: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
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 {
2024-01-19 21:32:46 -06:00
inProduction: boolean;
inPublic: boolean;
2023-06-27 01:43:30 +00:00
2024-03-06 15:38:53 -06:00
command:
| SlashCommandBuilder
| SlashCommandSubcommandsOnlyBuilder
| 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,
ratingsCommand,
2024-01-19 21:32:46 -06:00
todayCommand,
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-03-03 22:21:06 -06:00
if (isDev()) registry.value.command.setDefaultMemberPermissions(0);
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-03-06 15:38:53 -06:00
const rest = new REST().setToken(secrets.DISCORD_TOKEN);
2023-06-25 02:32:56 +00:00
export const guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
export const publicCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
2023-08-04 02:13:21 +00:00
commands.then((awaitedCommands) => {
2024-01-19 21:32:46 -06:00
Object.values(awaitedCommands).forEach((registry) => {
2023-08-24 00:08:08 +00:00
const json = registry.command.toJSON();
2023-06-25 02:32:56 +00:00
2023-08-24 00:08:08 +00:00
if (registry.inPublic) {
publicCommands.push(json);
} else {
guildCommands.push(json);
2023-06-25 02:32:56 +00:00
}
2023-08-04 02:13:21 +00:00
});
try {
2023-10-08 18:53:21 -05:00
rest.put(Routes.applicationCommands(getClientId()), {
body: publicCommands,
});
2023-08-04 02:13:21 +00:00
2023-10-08 18:53:21 -05:00
(isDev()
? [discord.test_guild_id]
: [discord.tres_guild_id, discord.sklld_guild_id]
).forEach((guildId) =>
rest.put(Routes.applicationGuildCommands(getClientId(), guildId), {
body: guildCommands,
}),
);
2023-08-04 02:13:21 +00:00
} catch (error) {
console.error(error);
2023-06-25 02:32:56 +00:00
}
});
export default async function interactionCreate(
interaction: Interaction<CacheType>,
) {
if (interaction.isAutocomplete()) {
handleAutocomplete(interaction);
} else if (interaction.isChatInputCommand()) {
handleChatInputCommand(interaction);
} else if (interaction.isButton()) {
handleButton(interaction);
}
}