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,
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<string, CommandRegistry>();
const commands = Object.entries(
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 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<CacheType>,
) {
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(