Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 1 | import { SlashCommandSubcommandBuilder, SlashCommandSubcommandGroupBuilder } from "discord.js"; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 2 | import type { SlashCommandBuilder } from "discord.js"; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 3 | import config from "../../config/main.js"; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 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 | |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 9 | const colors = { |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 10 | red: "\x1b[31m", |
| 11 | green: "\x1b[32m", |
| 12 | none: "\x1b[0m" |
| 13 | } |
| 14 | |
| 15 | |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 16 | export async function group( |
| 17 | name: string, |
| 18 | description: string, |
| 19 | path: string, |
| 20 | nameLocalizations?: Record<string, string>, |
| 21 | descriptionLocalizations?: Record<string, string> |
| 22 | ) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 23 | // 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] | 24 | console.log(`│ ├─ Loading group ${name}`) |
| 25 | const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path, "│ ") |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 26 | console.log(`│ │ └─ ${fetched.errors ? colors.red : colors.green}Loaded ${fetched.subcommands.length} subcommands for ${name} (${fetched.errors} failed)${colors.none}`) |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 27 | return (subcommandGroup: SlashCommandSubcommandGroupBuilder) => { |
| 28 | subcommandGroup |
| 29 | .setName(name) |
| 30 | .setDescription(description) |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 31 | if (nameLocalizations) { subcommandGroup.setNameLocalizations(nameLocalizations) } |
| 32 | if (descriptionLocalizations) { subcommandGroup.setDescriptionLocalizations(descriptionLocalizations) } |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 33 | |
| 34 | for (const subcommand of fetched.subcommands) { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 35 | const processedCommand = subcommand.command(new SlashCommandSubcommandBuilder()); |
| 36 | client.commands["commands/" + path + "/" + processedCommand.name] = [subcommand, { name: processedCommand.name, description: processedCommand.description }] |
| 37 | subcommandGroup.addSubcommand(processedCommand); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | return subcommandGroup; |
| 41 | }; |
| 42 | } |
| 43 | |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 44 | export async function command( |
| 45 | name: string, |
| 46 | description: string, |
| 47 | path: string, |
| 48 | commandString: string | undefined = undefined, |
| 49 | nameLocalizations?: Record<string, string>, |
| 50 | descriptionLocalizations?: Record<string, string>, |
| 51 | userPermissions?: Discord.PermissionsString[], |
| 52 | allowedInDMs?: boolean |
| 53 | ) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 54 | // If the name of the command does not match the path (e.g. attachment.ts has /attachments), use commandString |
| 55 | commandString = "commands/" + (commandString ?? path); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 56 | const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 57 | console.log(`│ ├─ ${fetched.errors ? colors.red : colors.green}Loaded ${fetched.subcommands.length} subcommands and ${fetched.subcommandGroups.length} subcommand groups for ${name} (${fetched.errors} failed)${colors.none}`) |
| 58 | // console.log({name: name, description: description}) |
| 59 | client.commands[commandString!] = [undefined, { name: name, description: description }] |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 60 | return (command: SlashCommandBuilder) => { |
| 61 | command.setName(name) |
| 62 | command.setDescription(description) |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 63 | command.setNameLocalizations(nameLocalizations ?? {}) |
| 64 | command.setDescriptionLocalizations(descriptionLocalizations ?? {}) |
| 65 | command.setDMPermission(allowedInDMs ?? false) |
| 66 | if (userPermissions) { |
| 67 | const bitfield = new Discord.PermissionsBitField() |
| 68 | bitfield.add(userPermissions) |
| 69 | command.setDefaultMemberPermissions(bitfield.bitfield) |
| 70 | } |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 71 | |
| 72 | for (const subcommand of fetched.subcommands) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 73 | let fetchedCommand; |
| 74 | if (subcommand.command instanceof Function) { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 75 | fetchedCommand = subcommand.command(new SlashCommandSubcommandBuilder()); |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 76 | } else { |
| 77 | fetchedCommand = subcommand.command; |
| 78 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 79 | client.commands[commandString! + "/" + fetchedCommand.name] = [subcommand, { name: fetchedCommand.name, description: fetchedCommand.description }] |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 80 | command.addSubcommand(fetchedCommand); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 81 | } |
| 82 | for (const group of fetched.subcommandGroups) { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 83 | const processedCommand = group.command(new SlashCommandSubcommandGroupBuilder()); |
| 84 | client.commands[commandString! + "/" + processedCommand.name] = [undefined, { name: processedCommand.name, description: processedCommand.description }] |
| 85 | command.addSubcommandGroup(processedCommand); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 86 | }; |
| 87 | return command; |
| 88 | }; |
| 89 | } |