Files
blitzkit/src/behaviors/interactionCreate.ts
T

93 lines
3.0 KiB
TypeScript
Raw Normal View History

2023-04-20 13:37:10 +00:00
import {
CacheType,
2023-04-26 00:02:10 +00:00
ChatInputCommandInteraction,
2023-04-20 13:37:10 +00:00
Collection,
Interaction,
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Routes,
SlashCommandBuilder,
} from 'discord.js';
import { readdirSync } from 'fs';
2023-04-20 14:09:22 +00:00
import discord from '../../discord.json' assert { type: 'json' };
2023-04-21 23:45:13 +00:00
import getClientId from '../utilities/getClientId.js';
2023-04-22 19:32:49 +00:00
import isDev from '../utilities/isDev.js';
2023-04-20 14:09:22 +00:00
2023-04-20 13:37:10 +00:00
export interface CommandRegistry {
2023-04-26 00:02:10 +00:00
inDevelopment: boolean; // register with Skilled Bot (default: false)
inProduction: boolean; // register with Skilled Canary (default: false)
inPublic: boolean; // registers on all servers (default: true)
command: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
execute: (interaction: ChatInputCommandInteraction<CacheType>) => void;
2023-04-20 13:37:10 +00:00
}
export const rest = new REST().setToken(process.env.DISCORD_TOKEN!);
2023-04-26 00:02:10 +00:00
const commandFolders = readdirSync('src/commands/');
const commandCollection = new Collection<string, CommandRegistry>();
2023-04-26 14:23:42 +00:00
export const guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
export const publicCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
2023-04-20 13:37:10 +00:00
for (const file of commandFolders) {
2023-04-26 00:02:10 +00:00
const command = (await import(`../commands/${file}`))
.default as CommandRegistry;
2023-04-21 23:45:13 +00:00
2023-04-26 00:02:10 +00:00
if (isDev() ? command.inDevelopment : command.inProduction) {
if (command.inPublic) {
publicCommands.push(command.command.toJSON());
} else {
guildCommands.push(command.command.toJSON());
}
commandCollection.set(command.command.name, command);
2023-04-21 23:45:13 +00:00
}
2023-04-20 13:37:10 +00:00
}
try {
2023-04-26 00:02:10 +00:00
console.log(`Refreshing ${publicCommands.length} public command(s).`);
const publicData = (await rest.put(
Routes.applicationCommands(getClientId()),
{ body: publicCommands },
)) as RESTPostAPIChatInputApplicationCommandsJSONBody[];
console.log(`Successfully refreshed ${publicData.length} public command(s).`);
2023-04-26 23:42:15 +00:00
console.log(`Refreshing ${guildCommands.length} guild command(s).`);
const guildData = (await rest.put(
Routes.applicationGuildCommands(getClientId(), discord.guild_id),
{ body: guildCommands },
)) as RESTPostAPIChatInputApplicationCommandsJSONBody[];
console.log(`Successfully refreshed ${guildData.length} guild command(s).`);
2023-04-20 13:37:10 +00:00
} catch (error) {
console.error(error);
}
export default async function interactionCreate(
interaction: Interaction<CacheType>,
) {
if (!interaction.isChatInputCommand()) return;
const command = commandCollection.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this command!',
ephemeral: true,
});
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true,
});
}
}
}