TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame^] | 1 | import type { SlashCommandSubcommandGroupBuilder } from "discord.js"; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 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 | 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, "│ ") |
| 26 | 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] | 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) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 35 | subcommandGroup.addSubcommand(subcommand.command); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | return subcommandGroup; |
| 39 | }; |
| 40 | } |
| 41 | |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 42 | export async function command( |
| 43 | name: string, |
| 44 | description: string, |
| 45 | path: string, |
| 46 | commandString: string | undefined = undefined, |
| 47 | nameLocalizations?: Record<string, string>, |
| 48 | descriptionLocalizations?: Record<string, string>, |
| 49 | userPermissions?: Discord.PermissionsString[], |
| 50 | allowedInDMs?: boolean |
| 51 | ) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 52 | // If the name of the command does not match the path (e.g. attachment.ts has /attachments), use commandString |
| 53 | commandString = "commands/" + (commandString ?? path); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 54 | const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path); |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 55 | 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] | 56 | return (command: SlashCommandBuilder) => { |
| 57 | command.setName(name) |
| 58 | command.setDescription(description) |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 59 | command.setNameLocalizations(nameLocalizations ?? {}) |
| 60 | command.setDescriptionLocalizations(descriptionLocalizations ?? {}) |
| 61 | command.setDMPermission(allowedInDMs ?? false) |
| 62 | if (userPermissions) { |
| 63 | const bitfield = new Discord.PermissionsBitField() |
| 64 | bitfield.add(userPermissions) |
| 65 | command.setDefaultMemberPermissions(bitfield.bitfield) |
| 66 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame^] | 67 | client.commands[commandString!] = [undefined, { name: name, description: description }] |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 68 | |
| 69 | for (const subcommand of fetched.subcommands) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 70 | let fetchedCommand; |
| 71 | if (subcommand.command instanceof Function) { |
| 72 | fetchedCommand = subcommand.command(new Discord.SlashCommandSubcommandBuilder()); |
| 73 | } else { |
| 74 | fetchedCommand = subcommand.command; |
| 75 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame^] | 76 | client.commands[commandString! + "/" + fetchedCommand.name] = [subcommand, { name: fetchedCommand.name, description: fetchedCommand.description }] |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 77 | command.addSubcommand(fetchedCommand); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 78 | } |
| 79 | for (const group of fetched.subcommandGroups) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 80 | command.addSubcommandGroup(group.command); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame^] | 81 | client.commands[commandString! + "/" + group.command.name] = [undefined, { name: group.command.name, description: group.command.description }] |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 82 | }; |
| 83 | return command; |
| 84 | }; |
| 85 | } |