diff --git a/src/core/env.d.ts b/src/core/env.d.ts new file mode 100644 index 000000000..11f02fe2a --- /dev/null +++ b/src/core/env.d.ts @@ -0,0 +1 @@ +/// diff --git a/src/events/interactionCreate.ts b/src/events/interactionCreate.ts index 1ff1a293c..294c5b5a0 100644 --- a/src/events/interactionCreate.ts +++ b/src/events/interactionCreate.ts @@ -5,14 +5,12 @@ import { ButtonStyle, CacheType, ChatInputCommandInteraction, - Collection, Interaction, REST, RESTPostAPIChatInputApplicationCommandsJSONBody, Routes, SlashCommandBuilder, } from 'discord.js'; -import { readdirSync } from 'fs'; import discord from '../../discord.json' assert { type: 'json' }; import negativeEmbed from '../core/interaction/negativeEmbed.js'; import { discordToken } from '../core/process/args.js'; @@ -32,50 +30,58 @@ export interface CommandRegistry { const rest = new REST().setToken(discordToken); -const commandFolders = readdirSync('src/commands/'); -const commandCollection = new Collection(); +const commands = Object.entries( + import.meta.glob('../commands/*', { + eager: true, + import: 'default', + }), +).reduce>((accumulator, [, registry]) => { + return { ...accumulator, [registry.command.name]: registry }; +}, {}); + export const guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = []; export const publicCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = []; -for (const file of commandFolders) { - const command = (await import(`../commands/${file}`)) - .default as CommandRegistry; - - if (isDev() ? command.inDevelopment : command.inProduction) { - if (command.inPublic) { - publicCommands.push(command.command.toJSON()); +Object.entries(commands).forEach(([, registry]) => { + if (isDev() ? registry.inDevelopment : registry.inProduction) { + if (registry.inPublic) { + publicCommands.push(registry.command.toJSON()); } else { - guildCommands.push(command.command.toJSON()); + guildCommands.push(registry.command.toJSON()); } - - commandCollection.set(command.command.name, command); } -} +}); try { 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).`); + rest + .put(Routes.applicationCommands(getClientId()), { body: publicCommands }) + .then((publicData) => { + console.log( + `Successfully refreshed ${ + (publicData as RESTPostAPIChatInputApplicationCommandsJSONBody[]) + .length + } public command(s).`, + ); + }); console.log(`Refreshing ${guildCommands.length} guild command(s).`); - const guildData = [ - ...((await rest.put( + Promise.all([ + rest.put( Routes.applicationGuildCommands(getClientId(), discord.sklld_guild_id), { body: guildCommands }, - )) as RESTPostAPIChatInputApplicationCommandsJSONBody[]), - ...((await rest.put( + ), + rest.put( Routes.applicationGuildCommands(getClientId(), discord.tres_guild_id), { body: guildCommands }, - )) as RESTPostAPIChatInputApplicationCommandsJSONBody[]), - ]; - console.log( - `Successfully refreshed ${guildData.length / 2} guild command(s).`, - ); + ), + ]).then((guildData) => { + console.log( + `Successfully refreshed ${guildData.flat().length / 2} guild command(s).`, + ); + }); } catch (error) { console.error(error); } @@ -84,7 +90,7 @@ export default async function interactionCreate( interaction: Interaction, ) { if (interaction.isAutocomplete()) { - const command = commandCollection.get(interaction.commandName); + const command = commands[interaction.commandName]; if (!command) { console.error( @@ -95,7 +101,7 @@ export default async function interactionCreate( if (command.autocomplete) command.autocomplete(interaction); } else if (interaction.isChatInputCommand()) { - const command = commandCollection.get(interaction.commandName); + const command = commands[interaction.commandName]; if (!command) { console.error(