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