blob: edfdfb7d8637293d5dd1efa4f92dcebc087bb955 [file] [log] [blame]
TheCodedProffe0a7862023-01-27 20:36:35 -05001import { SlashCommandSubcommandBuilder, SlashCommandSubcommandGroupBuilder } from "discord.js";
PineaFan64486c42022-12-28 09:21:04 +00002import type { SlashCommandBuilder } from "discord.js";
pineafana2e39c72023-02-21 18:37:32 +00003import config from "../../config/main.js";
PineaFan64486c42022-12-28 09:21:04 +00004import getSubcommandsInFolder from "./getFilesInFolder.js";
PineaFandf4996f2023-01-01 14:20:06 +00005import client from "../client.js";
6import Discord from "discord.js";
PineaFan64486c42022-12-28 09:21:04 +00007
TheCodedProf46518a42023-02-18 17:08:23 -05008const colors = {
PineaFan752af462022-12-31 21:59:38 +00009 red: "\x1b[31m",
10 green: "\x1b[32m",
11 none: "\x1b[0m"
Skyler Greyda16adf2023-03-05 10:22:12 +000012};
PineaFan752af462022-12-31 21:59:38 +000013
PineaFan538d3752023-01-12 21:48:23 +000014export async function group(
15 name: string,
16 description: string,
17 path: string,
18 nameLocalizations?: Record<string, string>,
19 descriptionLocalizations?: Record<string, string>
20) {
PineaFandf4996f2023-01-01 14:20:06 +000021 // If the name of the command does not match the path (e.g. attachment.ts has /attachments), use commandString
Skyler Greyda16adf2023-03-05 10:22:12 +000022 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 );
PineaFan64486c42022-12-28 09:21:04 +000029 return (subcommandGroup: SlashCommandSubcommandGroupBuilder) => {
Skyler Greyda16adf2023-03-05 10:22:12 +000030 subcommandGroup.setName(name).setDescription(description);
31 if (nameLocalizations) {
32 subcommandGroup.setNameLocalizations(nameLocalizations);
33 }
34 if (descriptionLocalizations) {
35 subcommandGroup.setDescriptionLocalizations(descriptionLocalizations);
36 }
PineaFan64486c42022-12-28 09:21:04 +000037
38 for (const subcommand of fetched.subcommands) {
PineaFanb0d0c242023-02-05 10:59:45 +000039 const processedCommand = subcommand.command(new SlashCommandSubcommandBuilder());
Skyler Greyda16adf2023-03-05 10:22:12 +000040 client.commands["commands/" + path + "/" + processedCommand.name] = [
41 subcommand,
42 { name: processedCommand.name, description: processedCommand.description }
43 ];
TheCodedProffe0a7862023-01-27 20:36:35 -050044 subcommandGroup.addSubcommand(processedCommand);
Skyler Greyda16adf2023-03-05 10:22:12 +000045 }
PineaFan64486c42022-12-28 09:21:04 +000046
47 return subcommandGroup;
48 };
49}
50
PineaFan538d3752023-01-12 21:48:23 +000051export 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) {
PineaFandf4996f2023-01-01 14:20:06 +000061 // If the name of the command does not match the path (e.g. attachment.ts has /attachments), use commandString
62 commandString = "commands/" + (commandString ?? path);
PineaFan64486c42022-12-28 09:21:04 +000063 const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path);
Skyler Greyda16adf2023-03-05 10:22:12 +000064 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 );
TheCodedProf8c295232023-01-27 20:52:24 -050069 // console.log({name: name, description: description})
Skyler Greyda16adf2023-03-05 10:22:12 +000070 client.commands[commandString!] = [undefined, { name: name, description: description }];
PineaFan64486c42022-12-28 09:21:04 +000071 return (command: SlashCommandBuilder) => {
Skyler Greyda16adf2023-03-05 10:22:12 +000072 command.setName(name);
73 command.setDescription(description);
74 command.setNameLocalizations(nameLocalizations ?? {});
75 command.setDescriptionLocalizations(descriptionLocalizations ?? {});
76 command.setDMPermission(allowedInDMs ?? false);
PineaFan538d3752023-01-12 21:48:23 +000077 if (userPermissions) {
Skyler Greyda16adf2023-03-05 10:22:12 +000078 const bitfield = new Discord.PermissionsBitField();
79 bitfield.add(userPermissions);
80 command.setDefaultMemberPermissions(bitfield.bitfield);
PineaFan538d3752023-01-12 21:48:23 +000081 }
PineaFan64486c42022-12-28 09:21:04 +000082
83 for (const subcommand of fetched.subcommands) {
PineaFandf4996f2023-01-01 14:20:06 +000084 let fetchedCommand;
85 if (subcommand.command instanceof Function) {
TheCodedProffe0a7862023-01-27 20:36:35 -050086 fetchedCommand = subcommand.command(new SlashCommandSubcommandBuilder());
PineaFandf4996f2023-01-01 14:20:06 +000087 } else {
88 fetchedCommand = subcommand.command;
89 }
Skyler Greyda16adf2023-03-05 10:22:12 +000090 client.commands[commandString! + "/" + fetchedCommand.name] = [
91 subcommand,
92 { name: fetchedCommand.name, description: fetchedCommand.description }
93 ];
PineaFandf4996f2023-01-01 14:20:06 +000094 command.addSubcommand(fetchedCommand);
PineaFan64486c42022-12-28 09:21:04 +000095 }
96 for (const group of fetched.subcommandGroups) {
PineaFanb0d0c242023-02-05 10:59:45 +000097 const processedCommand = group.command(new SlashCommandSubcommandGroupBuilder());
Skyler Greyda16adf2023-03-05 10:22:12 +000098 client.commands[commandString! + "/" + processedCommand.name] = [
99 undefined,
100 { name: processedCommand.name, description: processedCommand.description }
101 ];
TheCodedProffe0a7862023-01-27 20:36:35 -0500102 command.addSubcommandGroup(processedCommand);
Skyler Greyda16adf2023-03-05 10:22:12 +0000103 }
PineaFan64486c42022-12-28 09:21:04 +0000104 return command;
105 };
106}