diff --git a/src/commands/whois.context-menu.ts b/src/context-menus/whois.ts similarity index 92% rename from src/commands/whois.context-menu.ts rename to src/context-menus/whois.ts index 89b389f..10cdc2b 100644 --- a/src/commands/whois.context-menu.ts +++ b/src/context-menus/whois.ts @@ -2,9 +2,9 @@ import { ApplicationIntegrationType, Client, InteractionContextType, PermissionF import { channelIsInStaffCategory, handleWhoIsInteraction } from '../utils'; export default { - name: 'whois-context', + name: 'Whois', data: new ContextMenuCommandBuilder() - .setName('whois-context') + .setName('Whois') .setIntegrationTypes(ApplicationIntegrationType.GuildInstall) .setContexts(InteractionContextType.Guild) .setDefaultMemberPermissions(PermissionFlagsBits.BanMembers) diff --git a/src/index.ts b/src/index.ts index 0f7815e..1c5c5fc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,11 +31,13 @@ const client = new DiscordClient({ }); const commands: Handler[] = []; +const contextMenus: Handler[] = []; const buttons: Handler[] = []; const modals: Handler[] = []; const menus: Handler[] = []; loadHandlersFrom('commands', commands); +loadHandlersFrom('context-menus', contextMenus); loadHandlersFrom('buttons', buttons); loadHandlersFrom('modals', modals); loadHandlersFrom('menus', menus); @@ -59,14 +61,22 @@ client.on('interactionCreate', async (interaction) => { return; } - if (interaction.isChatInputCommand() || interaction.isContextMenuCommand()) { - // Handle chat and context menu commands. + if (interaction.isChatInputCommand()) { + // Handle chat for (const command of commands) { if (interaction.commandName == command.name) { command.handler(client, interaction); return; } } + } else if (interaction.isContextMenuCommand()) { + // Handle context menu commands. + for (const command of contextMenus) { + if (interaction.commandName == command.name) { + command.handler(client, interaction); + return; + } + } } else if (interaction.isAutocomplete()) { // Handle autocomplete requests. for (const command of commands) { diff --git a/src/utils/refresh-commands.ts b/src/utils/refresh-commands.ts index 8298c79..967ea68 100644 --- a/src/utils/refresh-commands.ts +++ b/src/utils/refresh-commands.ts @@ -13,27 +13,40 @@ export async function refreshCommands(client: Client) { try { const commands: RESTPostAPIApplicationCommandsJSONBody[] = []; const guildCommands: { [id: string]: RESTPostAPIApplicationCommandsJSONBody[] } = {}; - const commandFiles = fs.readdirSync(`${ROOT_DIR}/commands`).filter(file => file.endsWith('.js') || file.endsWith('.ts')); + const commandFiles = { + commands: fs.readdirSync(`${ROOT_DIR}/commands`).filter(file => file.endsWith('.js') || file.endsWith('.ts')), + 'context-menus': fs.readdirSync(`${ROOT_DIR}/context-menus`).filter(file => file.endsWith('.js') || file.endsWith('.ts')), + }; - for (const file of commandFiles) { - // eslint-disable-next-line @typescript-eslint/no-require-imports - const command: Command = require(`${ROOT_DIR}/commands/${file}`).default; - let data: RESTPostAPIApplicationCommandsJSONBody; - if (typeof (command.data) == 'function') { - data = (await command.data(client)).toJSON(); - } else { - data = command.data.toJSON(); - } + for (const [folderName, files] of Object.entries(commandFiles)) { + for (const file of files) { + const p = `${ROOT_DIR}/${folderName}/${file}`; + // eslint-disable-next-line @typescript-eslint/no-require-imports + const command: Command = require(p).default; - if (!command.guilds) { - commands.push(data); - } else { - for (const id of command.guilds) { - if (!guildCommands[id]) guildCommands[id] = []; - guildCommands[id].push(data); + if (!command) { + console.warn(`File at ${p} has no export. Skipping registering.`); + continue; + } + + let data: RESTPostAPIApplicationCommandsJSONBody; + if (typeof (command.data) == 'function') { + data = (await command.data(client)).toJSON(); + } else { + data = command.data.toJSON(); + } + + if (!command.guilds) { + commands.push(data); + } else { + for (const id of command.guilds) { + if (!guildCommands[id]) guildCommands[id] = []; + guildCommands[id].push(data); + } } } } + console.log('Started refreshing application (/) commands.'); console.log('Global commands: ' + commands.length);