PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 1 | import type { SlashCommandSubcommandGroupBuilder } from "@discordjs/builders"; |
| 2 | import type { SlashCommandBuilder } from "discord.js"; |
| 3 | import config from "../../config/main.json" assert { type: "json" }; |
| 4 | import getSubcommandsInFolder from "./getFilesInFolder.js"; |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame^] | 5 | import client from "../client.js"; |
| 6 | import Discord from "discord.js"; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 7 | |
| 8 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 9 | const colours = { |
| 10 | red: "\x1b[31m", |
| 11 | green: "\x1b[32m", |
| 12 | none: "\x1b[0m" |
| 13 | } |
| 14 | |
| 15 | |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 16 | export async function group(name: string, description: string, path: string) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame^] | 17 | // If the name of the command does not match the path (e.g. attachment.ts has /attachments), use commandString |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 18 | 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}`) |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 21 | return (subcommandGroup: SlashCommandSubcommandGroupBuilder) => { |
| 22 | subcommandGroup |
| 23 | .setName(name) |
| 24 | .setDescription(description) |
| 25 | |
| 26 | for (const subcommand of fetched.subcommands) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame^] | 27 | subcommandGroup.addSubcommand(subcommand.command); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | return subcommandGroup; |
| 31 | }; |
| 32 | } |
| 33 | |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame^] | 34 | export 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); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 37 | const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path); |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 38 | 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}`) |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 39 | return (command: SlashCommandBuilder) => { |
| 40 | command.setName(name) |
| 41 | command.setDescription(description) |
| 42 | |
| 43 | for (const subcommand of fetched.subcommands) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame^] | 44 | 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); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 52 | } |
| 53 | for (const group of fetched.subcommandGroups) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame^] | 54 | command.addSubcommandGroup(group.command); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 55 | }; |
| 56 | return command; |
| 57 | }; |
| 58 | } |