blob: 45cb8f1c207d36b14ae361449913ee7f93bd1f1c [file] [log] [blame]
PineaFan64486c42022-12-28 09:21:04 +00001import type { SlashCommandSubcommandGroupBuilder } from "@discordjs/builders";
2import type { SlashCommandBuilder } from "discord.js";
PineaFan100df682023-01-02 13:26:08 +00003// @ts-expect-error
PineaFan64486c42022-12-28 09:21:04 +00004import config from "../../config/main.json" assert { type: "json" };
5import getSubcommandsInFolder from "./getFilesInFolder.js";
PineaFandf4996f2023-01-01 14:20:06 +00006import client from "../client.js";
7import Discord from "discord.js";
PineaFan64486c42022-12-28 09:21:04 +00008
9
PineaFan752af462022-12-31 21:59:38 +000010const colours = {
11 red: "\x1b[31m",
12 green: "\x1b[32m",
13 none: "\x1b[0m"
14}
15
16
PineaFan64486c42022-12-28 09:21:04 +000017export async function group(name: string, description: string, path: string) {
PineaFandf4996f2023-01-01 14:20:06 +000018 // If the name of the command does not match the path (e.g. attachment.ts has /attachments), use commandString
PineaFan752af462022-12-31 21:59:38 +000019 console.log(`│ ├─ Loading group ${name}`)
20 const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path, "│ ")
21 console.log(`│ │ └─ ${fetched.errors ? colours.red : colours.green}Loaded ${fetched.subcommands.length} subcommands for ${name} (${fetched.errors} failed)${colours.none}`)
PineaFan64486c42022-12-28 09:21:04 +000022 return (subcommandGroup: SlashCommandSubcommandGroupBuilder) => {
23 subcommandGroup
24 .setName(name)
25 .setDescription(description)
26
27 for (const subcommand of fetched.subcommands) {
PineaFandf4996f2023-01-01 14:20:06 +000028 subcommandGroup.addSubcommand(subcommand.command);
PineaFan64486c42022-12-28 09:21:04 +000029 };
30
31 return subcommandGroup;
32 };
33}
34
PineaFandf4996f2023-01-01 14:20:06 +000035export async function command(name: string, description: string, path: string, commandString: string | undefined = undefined) {
36 // If the name of the command does not match the path (e.g. attachment.ts has /attachments), use commandString
37 commandString = "commands/" + (commandString ?? path);
PineaFan64486c42022-12-28 09:21:04 +000038 const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path);
PineaFan752af462022-12-31 21:59:38 +000039 console.log(`│ ├─ ${fetched.errors ? colours.red : colours.green}Loaded ${fetched.subcommands.length} subcommands and ${fetched.subcommandGroups.length} subcommand groups for ${name} (${fetched.errors} failed)${colours.none}`)
PineaFan64486c42022-12-28 09:21:04 +000040 return (command: SlashCommandBuilder) => {
41 command.setName(name)
42 command.setDescription(description)
43
44 for (const subcommand of fetched.subcommands) {
PineaFandf4996f2023-01-01 14:20:06 +000045 let fetchedCommand;
46 if (subcommand.command instanceof Function) {
47 fetchedCommand = subcommand.command(new Discord.SlashCommandSubcommandBuilder());
48 } else {
49 fetchedCommand = subcommand.command;
50 }
51 client.commands[commandString! + "/" + fetchedCommand.name] = subcommand
52 command.addSubcommand(fetchedCommand);
PineaFan64486c42022-12-28 09:21:04 +000053 }
54 for (const group of fetched.subcommandGroups) {
PineaFandf4996f2023-01-01 14:20:06 +000055 command.addSubcommandGroup(group.command);
PineaFan64486c42022-12-28 09:21:04 +000056 };
57 return command;
58 };
59}