blob: 76ecabee1d6a20db238e8b53b777f09b17712971 [file] [log] [blame]
PineaFan64486c42022-12-28 09:21:04 +00001import type { SlashCommandSubcommandGroupBuilder } from "@discordjs/builders";
2import type { SlashCommandBuilder } from "discord.js";
3import config from "../../config/main.json" assert { type: "json" };
4import getSubcommandsInFolder from "./getFilesInFolder.js";
PineaFandf4996f2023-01-01 14:20:06 +00005import client from "../client.js";
6import Discord from "discord.js";
PineaFan64486c42022-12-28 09:21:04 +00007
8
PineaFan752af462022-12-31 21:59:38 +00009const colours = {
10 red: "\x1b[31m",
11 green: "\x1b[32m",
12 none: "\x1b[0m"
13}
14
15
PineaFan64486c42022-12-28 09:21:04 +000016export async function group(name: string, description: string, path: string) {
PineaFandf4996f2023-01-01 14:20:06 +000017 // 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 +000018 console.log(`│ ├─ Loading group ${name}`)
19 const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path, "│ ")
20 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 +000021 return (subcommandGroup: SlashCommandSubcommandGroupBuilder) => {
22 subcommandGroup
23 .setName(name)
24 .setDescription(description)
25
26 for (const subcommand of fetched.subcommands) {
PineaFandf4996f2023-01-01 14:20:06 +000027 subcommandGroup.addSubcommand(subcommand.command);
PineaFan64486c42022-12-28 09:21:04 +000028 };
29
30 return subcommandGroup;
31 };
32}
33
PineaFandf4996f2023-01-01 14:20:06 +000034export async function command(name: string, description: string, path: string, commandString: string | undefined = undefined) {
35 // If the name of the command does not match the path (e.g. attachment.ts has /attachments), use commandString
36 commandString = "commands/" + (commandString ?? path);
PineaFan64486c42022-12-28 09:21:04 +000037 const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path);
PineaFan752af462022-12-31 21:59:38 +000038 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 +000039 return (command: SlashCommandBuilder) => {
40 command.setName(name)
41 command.setDescription(description)
42
43 for (const subcommand of fetched.subcommands) {
PineaFandf4996f2023-01-01 14:20:06 +000044 let fetchedCommand;
45 if (subcommand.command instanceof Function) {
46 fetchedCommand = subcommand.command(new Discord.SlashCommandSubcommandBuilder());
47 } else {
48 fetchedCommand = subcommand.command;
49 }
50 client.commands[commandString! + "/" + fetchedCommand.name] = subcommand
51 command.addSubcommand(fetchedCommand);
PineaFan64486c42022-12-28 09:21:04 +000052 }
53 for (const group of fetched.subcommandGroups) {
PineaFandf4996f2023-01-01 14:20:06 +000054 command.addSubcommandGroup(group.command);
PineaFan64486c42022-12-28 09:21:04 +000055 };
56 return command;
57 };
58}