COMMAND REGISTRATION WORKS
diff --git a/src/utils/commandRegistration/getFilesInFolder.ts b/src/utils/commandRegistration/getFilesInFolder.ts
new file mode 100644
index 0000000..a669065
--- /dev/null
+++ b/src/utils/commandRegistration/getFilesInFolder.ts
@@ -0,0 +1,23 @@
+import fs from "fs";
+
+export default async function getSubcommandsInFolder(path: string) {
+ const files = fs.readdirSync(path, { withFileTypes: true }).filter(
+ file => !file.name.endsWith(".ts") && !file.name.endsWith(".map")
+ );
+ const subcommands = [];
+ const subcommandGroups = [];
+ for (const file of files) {
+ if (file.name === "_meta.js") continue;
+ // If its a folder
+ if (file.isDirectory()) {
+ // Get the _meta.ts file
+ console.log(`│ ├─ Loading subcommand group ${file.name}}`)
+ subcommandGroups.push((await import(`../../../${path}/${file.name}/_meta.js`)).command);
+ } else if (file.name.endsWith(".js")) {
+ // If its a file
+ console.log(`│ ├─ Loading subcommand ${file.name}}`)
+ subcommands.push((await import(`../../../${path}/${file.name}`)).command);
+ }
+ }
+ return {subcommands, subcommandGroups};
+}
diff --git a/src/utils/commandRegistration/register.ts b/src/utils/commandRegistration/register.ts
new file mode 100644
index 0000000..af0b5fc
--- /dev/null
+++ b/src/utils/commandRegistration/register.ts
@@ -0,0 +1,61 @@
+import { SlashCommandBuilder } from 'discord.js';
+import config from "../../config/main.json" assert { type: "json" };
+import client from "../client.js";
+import fs from "fs";
+
+
+async function registerCommands() {
+ const developmentMode = config.enableDevelopment;
+ const commands = [];
+
+ const files = fs.readdirSync(config.commandsFolder, { withFileTypes: true }).filter(
+ file => !file.name.endsWith(".ts") && !file.name.endsWith(".map")
+ );
+ console.log(`Registering ${files.length} commands`)
+ let i = 0;
+ for (const file of files) {
+ // Box drawing characters: | └ ─ ┌ ┐ ┘ ┬ ┤ ├ ┴ ┼
+ console.log(`├─ ${file.name}`)
+ if (file.isDirectory()) {
+ commands.push((await import(`../../../${config.commandsFolder}/${file.name}/_meta.js`)).command);
+ } else if (file.name.endsWith(".js")) {
+ commands.push((await import(`../../../${config.commandsFolder}/${file.name}`)).command);
+ }
+ i++;
+ console.log(`├─ Loaded ${file.name} [${i} / ${files.length}]`)
+ }
+ console.log(`Loaded ${commands.length} commands, processing...`)
+ const processed = []
+
+ for (const subcommand of commands) {
+ if (subcommand instanceof Function) {
+ processed.push(subcommand(new SlashCommandBuilder()))
+ } else {
+ processed.push(subcommand)
+ }
+ }
+
+ console.log(`Processed ${commands.length} commands, registering...`)
+
+ if (developmentMode) {
+ const guild = await client.guilds.fetch(config.developmentGuildID);
+ guild.commands.set([])
+ guild.commands.set(processed);
+ console.log(`Commands registered in ${guild.name}`)
+ } else {
+ client.application!.commands.set([])
+ client.application!.commands.set(processed);
+ console.log(`Commands registered globally`)
+ }
+
+};
+
+async function registerEvents() {
+ // pass
+};
+
+export default async function register() {
+ console.log("> Registering commands")
+ await registerCommands();
+ await registerEvents();
+};
diff --git a/src/utils/commandRegistration/slashCommandBuilder.ts b/src/utils/commandRegistration/slashCommandBuilder.ts
new file mode 100644
index 0000000..719855f
--- /dev/null
+++ b/src/utils/commandRegistration/slashCommandBuilder.ts
@@ -0,0 +1,37 @@
+import type { SlashCommandSubcommandGroupBuilder } from "@discordjs/builders";
+import type { SlashCommandBuilder } from "discord.js";
+import config from "../../config/main.json" assert { type: "json" };
+import getSubcommandsInFolder from "./getFilesInFolder.js";
+
+
+export async function group(name: string, description: string, path: string) {
+ const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path)
+ return (subcommandGroup: SlashCommandSubcommandGroupBuilder) => {
+ subcommandGroup
+ .setName(name)
+ .setDescription(description)
+
+ for (const subcommand of fetched.subcommands) {
+ subcommandGroup.addSubcommand(subcommand);
+ };
+
+ return subcommandGroup;
+ };
+}
+
+export async function command(name: string, description: string, path: string) {
+ const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path);
+ console.log(`│ ├─ Loaded ${fetched.subcommands.length} subcommands and ${fetched.subcommandGroups.length} subcommand groups for ${name}`)
+ return (command: SlashCommandBuilder) => {
+ command.setName(name)
+ command.setDescription(description)
+
+ for (const subcommand of fetched.subcommands) {
+ command.addSubcommand(subcommand);
+ }
+ for (const group of fetched.subcommandGroups) {
+ command.addSubcommandGroup(group);
+ };
+ return command;
+ };
+}