Files
blitzkit/src/events/interactionCreate.ts
T

251 lines
7.1 KiB
TypeScript
Raw Normal View History

2023-04-20 13:37:10 +00:00
import {
2023-06-06 16:50:33 +00:00
ActionRowBuilder,
2023-05-13 21:42:40 +00:00
AutocompleteInteraction,
2023-06-06 16:50:33 +00:00
ButtonBuilder,
2023-06-23 01:35:15 +00:00
ButtonInteraction,
2023-06-06 16:50:33 +00:00
ButtonStyle,
2023-04-20 13:37:10 +00:00
CacheType,
2023-04-26 00:02:10 +00:00
ChatInputCommandInteraction,
2023-06-16 02:01:06 +00:00
EmbedBuilder,
2023-04-20 13:37:10 +00:00
Interaction,
2023-06-16 02:01:06 +00:00
InteractionEditReplyOptions,
2023-04-28 13:42:42 +00:00
REST,
2023-04-20 13:37:10 +00:00
RESTPostAPIChatInputApplicationCommandsJSONBody,
2023-04-28 13:42:42 +00:00
Routes,
2023-04-20 13:37:10 +00:00
SlashCommandBuilder,
SlashCommandSubcommandsOnlyBuilder,
2023-04-20 13:37:10 +00:00
} from 'discord.js';
2023-04-28 13:42:42 +00:00
import discord from '../../discord.json' assert { type: 'json' };
2023-06-10 01:09:07 +00:00
import debug from '../commands/debug.js';
import eligible from '../commands/eligible.js';
2023-06-21 01:52:26 +00:00
import evolution from '../commands/evolution.js';
2023-06-10 01:09:07 +00:00
import help from '../commands/help.js';
import inactive from '../commands/inactive.js';
import ownedtanks from '../commands/ownedtanks.js';
2023-06-17 18:16:41 +00:00
import ping from '../commands/ping.js';
2023-06-10 01:09:07 +00:00
import playerachievements from '../commands/playerachievements.js';
import playerinfo from '../commands/playerinfo.js';
import searchclans from '../commands/searchclans.js';
import searchplayers from '../commands/searchplayers.js';
import searchtanks from '../commands/searchtanks.js';
import stats from '../commands/stats.js';
import today from '../commands/today.js';
import verify from '../commands/verify.js';
2023-06-06 16:50:33 +00:00
import negativeEmbed from '../core/interaction/negativeEmbed.js';
2023-06-16 02:15:46 +00:00
import warningEmbed from '../core/interaction/warningEmbed.js';
2023-06-12 01:17:26 +00:00
import { DISCORD_TOKEN } from '../core/process/args.js';
2023-05-13 22:01:58 +00:00
import getClientId from '../core/process/getClientId.js';
import isDev from '../core/process/isDev.js';
2023-06-16 02:15:46 +00:00
import { psa } from '../core/process/psa.js';
2023-06-16 02:01:06 +00:00
import render from '../core/ui/render.js';
2023-06-06 16:50:33 +00:00
import { handleError } from './error.js';
2023-04-20 14:09:22 +00:00
2023-06-16 02:01:06 +00:00
export type CommandReturnable = EmbedBuilder | ButtonBuilder | JSX.Element;
export type RichCommandReturnable = CommandReturnable | CommandReturnable[];
2023-06-17 18:16:41 +00:00
export type CommandRegistry = {
2023-05-21 16:50:05 +00:00
inDevelopment: boolean;
inProduction: boolean;
inPublic: boolean;
2023-04-26 00:02:10 +00:00
2023-06-21 01:52:26 +00:00
command:
| SlashCommandBuilder
| SlashCommandSubcommandsOnlyBuilder
| Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
2023-05-13 21:42:40 +00:00
autocomplete?: (interaction: AutocompleteInteraction<CacheType>) => void;
2023-06-17 18:16:41 +00:00
} & (
| {
handlesInteraction?: false;
execute: (
interaction: ChatInputCommandInteraction<CacheType>,
) => RichCommandReturnable | Promise<RichCommandReturnable>;
}
| {
handlesInteraction: true;
execute: (interaction: ChatInputCommandInteraction<CacheType>) => void;
}
);
2023-04-20 13:37:10 +00:00
2023-06-12 01:17:26 +00:00
const rest = new REST().setToken(DISCORD_TOKEN);
2023-04-20 13:37:10 +00:00
2023-06-16 02:01:06 +00:00
const commands = (
[
debug,
eligible,
help,
inactive,
ownedtanks,
playerachievements,
playerinfo,
searchclans,
searchplayers,
searchtanks,
stats,
today,
verify,
2023-06-17 18:16:41 +00:00
ping,
2023-06-21 01:52:26 +00:00
evolution,
2023-06-16 02:01:06 +00:00
] as CommandRegistry[]
).reduce<Record<string, CommandRegistry>>((accumulator, registry) => {
2023-06-21 22:31:46 +00:00
registry.command.setName(
isDev() ? `${registry.command.name}dev` : registry.command.name,
);
2023-06-08 00:53:26 +00:00
return { ...accumulator, [registry.command.name]: registry };
}, {});
2023-04-28 13:42:42 +00:00
export const guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
export const publicCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
2023-06-08 00:53:26 +00:00
Object.entries(commands).forEach(([, registry]) => {
if (isDev() ? registry.inDevelopment : registry.inProduction) {
2023-06-21 22:31:46 +00:00
const json = registry.command.toJSON();
2023-06-08 00:53:26 +00:00
if (registry.inPublic) {
publicCommands.push(json);
2023-04-28 13:42:42 +00:00
} else {
guildCommands.push(json);
2023-04-26 00:02:10 +00:00
}
2023-04-28 13:42:42 +00:00
}
2023-06-08 00:53:26 +00:00
});
2023-04-28 13:42:42 +00:00
try {
console.log(`Refreshing ${publicCommands.length} public command(s).`);
2023-06-08 00:53:26 +00:00
rest
.put(Routes.applicationCommands(getClientId()), { body: publicCommands })
.then((publicData) => {
console.log(
`Successfully refreshed ${
(publicData as RESTPostAPIChatInputApplicationCommandsJSONBody[])
.length
} public command(s).`,
);
});
2023-04-28 13:42:42 +00:00
console.log(`Refreshing ${guildCommands.length} guild command(s).`);
2023-06-22 18:26:51 +00:00
rest
.put(
2023-06-04 00:01:10 +00:00
Routes.applicationGuildCommands(getClientId(), discord.tres_guild_id),
{ body: guildCommands },
2023-06-22 18:26:51 +00:00
)
.then((guildData) => {
console.log(
`Successfully refreshed ${
(guildData as unknown[]).length
} guild command(s).`,
);
});
2023-04-28 13:42:42 +00:00
} catch (error) {
console.error(error);
}
2023-06-23 01:35:15 +00:00
//#region Event type handlers
function handleAutocomplete(interaction: AutocompleteInteraction<CacheType>) {
const command = commands[interaction.commandName];
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
if (command.autocomplete) command.autocomplete(interaction);
}
async function handleChatInputCommand(
interaction: ChatInputCommandInteraction<CacheType>,
) {
const command = commands[interaction.commandName];
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
const metadata = [
`${interaction.user.id} (${interaction.user.username})`,
`${interaction.guildId}/${interaction.channelId} (${interaction.guild?.name})`,
new Date(),
];
console.log(
`${interaction.toString()}\n${metadata
.map((item) => ` ${item}`)
.join('\n')}`,
);
await interaction.deferReply();
try {
const result = await command.execute(interaction);
if (command.handlesInteraction) return;
const normalizedResult = Array.isArray(result) ? result : [result];
const reply: InteractionEditReplyOptions = {};
if (psa.data) {
normalizedResult.push(warningEmbed(psa.data.title, psa.data.description));
}
await Promise.all(
normalizedResult.map(async (item) => {
if (item instanceof EmbedBuilder) {
if (!reply.embeds) reply.embeds = [];
reply.embeds.push(item);
} else if (item instanceof ButtonBuilder) {
if (!reply.components)
reply.components = [new ActionRowBuilder<ButtonBuilder>()];
(
reply.components[0] as ActionRowBuilder<ButtonBuilder>
).addComponents(item);
} else {
const image = await render(item!);
if (!reply.files) reply.files = [];
reply.files.push(image);
}
}),
);
await interaction.editReply(reply);
} catch (error) {
const actionRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setLabel('Get Help on Discord Server')
.setURL('https://discord.gg/nDt7AjGJQH')
.setStyle(ButtonStyle.Link),
);
await interaction.editReply({
embeds: [
negativeEmbed(
(error as Error).message,
`${(error as Error).cause ?? 'No further information is available.'}`,
),
],
components: [actionRow],
});
handleError(error as Error, interaction.commandName);
}
}
function handleButton(interaction: ButtonInteraction<CacheType>) {
console.log(interaction.customId);
interaction.message.edit({
files: [],
});
}
//#endregion
2023-04-28 13:42:42 +00:00
export default async function interactionCreate(
interaction: Interaction<CacheType>,
) {
2023-05-13 21:42:40 +00:00
if (interaction.isAutocomplete()) {
2023-06-23 01:35:15 +00:00
handleAutocomplete(interaction);
2023-05-13 21:42:40 +00:00
} else if (interaction.isChatInputCommand()) {
2023-06-23 01:35:15 +00:00
handleChatInputCommand(interaction);
} else if (interaction.isButton()) {
handleButton(interaction);
2023-04-28 13:42:42 +00:00
}
2023-04-20 13:37:10 +00:00
}