blob: a4474ac7288dc40fb0f8c08f2f12e5811b75805c [file] [log] [blame]
TheCodedProff86ba092023-01-27 17:10:07 -05001import type { SlashCommandSubcommandGroupBuilder } from "discord.js";
PineaFan64486c42022-12-28 09:21:04 +00002import type { SlashCommandBuilder } from "discord.js";
3import config from "../../config/main.json" assert { type: "json" };
4import 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
8
PineaFan752af462022-12-31 21:59:38 +00009const colours = {
10 red: "\x1b[31m",
11 green: "\x1b[32m",
12 none: "\x1b[0m"
13}
14
15
PineaFan538d3752023-01-12 21:48:23 +000016export async function group(
17 name: string,
18 description: string,
19 path: string,
20 nameLocalizations?: Record<string, string>,
21 descriptionLocalizations?: Record<string, string>
22) {
PineaFandf4996f2023-01-01 14:20:06 +000023 // If the name of the command does not match the path (e.g. attachment.ts has /attachments), use commandString
PineaFan752af462022-12-31 21:59:38 +000024 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}`)
PineaFan64486c42022-12-28 09:21:04 +000027 return (subcommandGroup: SlashCommandSubcommandGroupBuilder) => {
28 subcommandGroup
29 .setName(name)
30 .setDescription(description)
PineaFan538d3752023-01-12 21:48:23 +000031 if (nameLocalizations) { subcommandGroup.setNameLocalizations(nameLocalizations) }
32 if (descriptionLocalizations) { subcommandGroup.setDescriptionLocalizations(descriptionLocalizations) }
PineaFan64486c42022-12-28 09:21:04 +000033
34 for (const subcommand of fetched.subcommands) {
PineaFandf4996f2023-01-01 14:20:06 +000035 subcommandGroup.addSubcommand(subcommand.command);
PineaFan64486c42022-12-28 09:21:04 +000036 };
37
38 return subcommandGroup;
39 };
40}
41
PineaFan538d3752023-01-12 21:48:23 +000042export 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) {
PineaFandf4996f2023-01-01 14:20:06 +000052 // If the name of the command does not match the path (e.g. attachment.ts has /attachments), use commandString
53 commandString = "commands/" + (commandString ?? path);
PineaFan64486c42022-12-28 09:21:04 +000054 const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path);
PineaFan752af462022-12-31 21:59:38 +000055 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}`)
PineaFan64486c42022-12-28 09:21:04 +000056 return (command: SlashCommandBuilder) => {
57 command.setName(name)
58 command.setDescription(description)
PineaFan538d3752023-01-12 21:48:23 +000059 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 }
TheCodedProff86ba092023-01-27 17:10:07 -050067 client.commands[commandString!] = [undefined, { name: name, description: description }]
PineaFan64486c42022-12-28 09:21:04 +000068
69 for (const subcommand of fetched.subcommands) {
PineaFandf4996f2023-01-01 14:20:06 +000070 let fetchedCommand;
71 if (subcommand.command instanceof Function) {
72 fetchedCommand = subcommand.command(new Discord.SlashCommandSubcommandBuilder());
73 } else {
74 fetchedCommand = subcommand.command;
75 }
TheCodedProff86ba092023-01-27 17:10:07 -050076 client.commands[commandString! + "/" + fetchedCommand.name] = [subcommand, { name: fetchedCommand.name, description: fetchedCommand.description }]
PineaFandf4996f2023-01-01 14:20:06 +000077 command.addSubcommand(fetchedCommand);
PineaFan64486c42022-12-28 09:21:04 +000078 }
79 for (const group of fetched.subcommandGroups) {
PineaFandf4996f2023-01-01 14:20:06 +000080 command.addSubcommandGroup(group.command);
TheCodedProff86ba092023-01-27 17:10:07 -050081 client.commands[commandString! + "/" + group.command.name] = [undefined, { name: group.command.name, description: group.command.description }]
PineaFan64486c42022-12-28 09:21:04 +000082 };
83 return command;
84 };
85}