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