From 65801cf0c63aa9fe618ba2f0ac2d2f23c000bfcb Mon Sep 17 00:00:00 2001 From: PineaFan Date: Mon, 2 Jan 2023 19:26:12 +0000 Subject: [PATCH] Context menus work! --- src/utils/commandRegistration/register.ts | 84 +++++++++++++---------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/src/utils/commandRegistration/register.ts b/src/utils/commandRegistration/register.ts index d1b58f1..1f2241c 100644 --- a/src/utils/commandRegistration/register.ts +++ b/src/utils/commandRegistration/register.ts @@ -1,5 +1,4 @@ -import { typeSlashCommandSubcommandBuilder } from '@discordjs/builders'; -import { Interaction, SlashCommandBuilder, ApplicationCommandType } from 'discord.js'; +import Discord, { Interaction, SlashCommandBuilder, ApplicationCommandType } from 'discord.js'; // @ts-expect-error import config from "../../config/main.json" assert { type: "json" }; import client from "../client.js"; @@ -16,7 +15,6 @@ const colours = { } async function registerCommands() { - const developmentMode = config.enableDevelopment; const commands = []; const files = fs.readdirSync(config.commandsFolder, { withFileTypes: true }).filter( @@ -49,17 +47,8 @@ async function registerCommands() { } } - console.log(`Processed ${commands.length} commands, registering...`) - - const updateCommands = process.argv.includes("--update-commands"); - if (developmentMode) { - const guild = await client.guilds.fetch(config.developmentGuildID); - if (updateCommands) guild.commands.set(processed); - console.log(`${colours.purple}Commands registered in ${guild.name}${colours.none}`) - } else { - if (updateCommands) client.application!.commands.set(processed); - console.log(`${colours.blue}Commands registered globally${colours.none}`) - } + console.log(`Processed ${processed.length} commands`) + return processed; }; @@ -100,31 +89,35 @@ async function registerContextMenus() { console.log(`Registering ${messageFiles.length} message context menus and ${userFiles.length} user context menus`) let i = 0; let errors = 0; + const commands: (Discord.ContextMenuCommandBuilder)[] = [] + const totalFiles = messageFiles.length + userFiles.length; for (const file of messageFiles) { - const last = i === messageFiles.length - 1 ? "└" : "├"; + const last = i === totalFiles - 1 ? "└" : "├"; i++; try { console.log(`${last}─ ${colours.yellow}Loading message context menu ${file.name}${colours.none}`) const context = (await import(`../../../${config.messageContextFolder}/${file.name}`)); context.command.setType(ApplicationCommandType.Message); + commands.push(context.command); - client.commands["contextCommands/message/" + file.name.replace(".js", "")] = context; + client.commands["contextCommands/message/" + context.command.name] = context; - console.log(`${last.replace("└", " ").replace("├", "│")} └─ ${colours.green}Loaded ${file.name} [${i} / ${messageFiles.length}]${colours.none}`) + console.log(`${last.replace("└", " ").replace("├", "│")} └─ ${colours.green}Loaded ${file.name} [${i} / ${totalFiles}]${colours.none}`) } catch (e) { errors++; - console.log(`${last.replace("└", " ").replace("├", "│")} └─ ${colours.red}Failed to load ${file.name} [${i} / ${messageFiles.length}]${colours.none}`) + console.log(`${last.replace("└", " ").replace("├", "│")} └─ ${colours.red}Failed to load ${file.name} [${i} / ${totalFiles}] | ${e}${colours.none}`) } } for (const file of userFiles) { - const last = i === userFiles.length - 1 ? "└" : "├"; + const last = i === totalFiles - 1 ? "└" : "├"; i++; try { console.log(`${last}─ ${colours.yellow}Loading user context menu ${file.name}${colours.none}`) const context = (await import(`../../../${config.userContextFolder}/${file.name}`)); context.command.setType(ApplicationCommandType.User); + commands.push(context.command); - client.commands["contextCommands/user/" + file.name.replace(".js", "")] = context; + client.commands["contextCommands/user/" + context.command.name] = context; console.log(`${last.replace("└", " ").replace("├", "│")} └─ ${colours.green}Loaded ${file.name} [${i} / ${userFiles.length}]${colours.none}`) } catch (e) { @@ -134,17 +127,18 @@ async function registerContextMenus() { } console.log(`Loaded ${messageFiles.length + userFiles.length - errors} context menus (${errors} failed)`) + return commands; }; async function registerCommandHandler() { client.on("interactionCreate", async (interaction: Interaction) => { if (interaction.isUserContextMenuCommand()) {; - const commandName = interaction.commandName; - console.log(commandName); + const commandName = "contextCommands/user/" + interaction.commandName; + execute(client.commands[commandName]?.check, client.commands[commandName]?.callback, interaction) return; } else if (interaction.isMessageContextMenuCommand()) { - const commandName = interaction.commandName; - console.log(commandName); + const commandName = "contextCommands/message/" + interaction.commandName; + execute(client.commands[commandName]?.check, client.commands[commandName]?.callback, interaction) return; } if (!interaction.isChatInputCommand()) return; @@ -158,25 +152,39 @@ async function registerCommandHandler() { const command = client.commands[fullCommandName]; const callback = command?.callback; const check = command?.check; + execute(check, callback, interaction); + }); +} - if (!callback) return; - if (check) { - let result; - try { - result = await check(interaction); - } catch (e) { - console.log(e); - result = false; - } - if (!result) return; +async function execute(check: Function | undefined, callback: Function | undefined, data: Interaction) { + if (!callback) return; + if (check) { + let result; + try { + result = await check(data); + } catch (e) { + console.log(e); + result = false; } - callback(interaction); - }); + if (!result) return; + } + callback(data); } export default async function register() { - let commandList: (SlashCommandBuilder | SlashCommandSubcommandBuilder)[] = [] - commandList.concat(await registerCommands()); + let commandList: ( Discord.SlashCommandBuilder | Discord.ContextMenuCommandBuilder )[] = []; + commandList = commandList.concat(await registerCommands()); + commandList = commandList.concat(await registerContextMenus()); + if (process.argv.includes("--update-commands")) { + if (config.enableDevelopment) { + const guild = await client.guilds.fetch(config.developmentGuildID); + console.log(`${colours.purple}Registering commands in ${guild!.name}${colours.none}`) + await guild.commands.set(commandList); + } else { + console.log(`${colours.blue}Registering commands in production mode${colours.none}`) + await client.application?.commands.set(commandList); + } + } await registerCommandHandler(); await registerEvents(); console.log(`${colours.green}Registered commands and events${colours.none}`)