Files
blitzkit/src/events/interactionCreate.ts
T

165 lines
4.7 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,
ButtonStyle,
2023-04-20 13:37:10 +00:00
CacheType,
2023-04-26 00:02:10 +00:00
ChatInputCommandInteraction,
2023-04-20 13:37:10 +00:00
Interaction,
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,
} 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';
import help from '../commands/help.js';
import inactive from '../commands/inactive.js';
import ownedtanks from '../commands/ownedtanks.js';
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 tankstats from '../commands/tankstats.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-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-06 16:50:33 +00:00
import { handleError } from './error.js';
2023-04-20 14:09:22 +00:00
2023-04-20 13:37:10 +00:00
export interface CommandRegistry {
2023-05-21 16:50:05 +00:00
inDevelopment: boolean;
inProduction: boolean;
inPublic: boolean;
2023-04-26 00:02:10 +00:00
command: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
2023-04-28 13:42:42 +00:00
execute: (interaction: ChatInputCommandInteraction<CacheType>) => void;
2023-05-13 21:42:40 +00:00
autocomplete?: (interaction: AutocompleteInteraction<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-10 01:09:07 +00:00
const commands = [
debug,
eligible,
help,
inactive,
ownedtanks,
playerachievements,
playerinfo,
searchclans,
searchplayers,
searchtanks,
stats,
tankstats,
today,
verify,
].reduce<Record<string, CommandRegistry>>((accumulator, registry) => {
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) {
if (registry.inPublic) {
publicCommands.push(registry.command.toJSON());
2023-04-28 13:42:42 +00:00
} else {
2023-06-08 00:53:26 +00:00
guildCommands.push(registry.command.toJSON());
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-08 00:53:26 +00:00
Promise.all([
rest.put(
2023-06-04 00:01:10 +00:00
Routes.applicationGuildCommands(getClientId(), discord.sklld_guild_id),
{ body: guildCommands },
2023-06-08 00:53:26 +00:00
),
rest.put(
2023-06-04 00:01:10 +00:00
Routes.applicationGuildCommands(getClientId(), discord.tres_guild_id),
{ body: guildCommands },
2023-06-08 00:53:26 +00:00
),
]).then((guildData) => {
console.log(
`Successfully refreshed ${guildData.flat().length / 2} guild command(s).`,
);
});
2023-04-28 13:42:42 +00:00
} catch (error) {
console.error(error);
}
export default async function interactionCreate(
interaction: Interaction<CacheType>,
) {
2023-05-13 21:42:40 +00:00
if (interaction.isAutocomplete()) {
2023-06-08 00:53:26 +00:00
const command = commands[interaction.commandName];
2023-04-28 13:42:42 +00:00
2023-05-13 21:42:40 +00:00
if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`,
);
return;
}
2023-04-28 13:42:42 +00:00
if (command.autocomplete) command.autocomplete(interaction);
2023-05-13 21:42:40 +00:00
} else if (interaction.isChatInputCommand()) {
2023-06-08 00:53:26 +00:00
const command = commands[interaction.commandName];
2023-05-13 21:42:40 +00:00
if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`,
);
return;
}
console.log(interaction.toString());
await interaction.deferReply();
2023-06-06 16:50:33 +00:00
try {
await command.execute(interaction);
} 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);
}
2023-04-28 13:42:42 +00:00
}
2023-04-20 13:37:10 +00:00
}