feat: simplified command registration

This commit is contained in:
TresAbhi committed 2023-06-08 00:53:26 +00:00
1 parent 3a23a3f8c4
commit 1e32da01ee
2 files changed
+38 -31

No files matched your search

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