blob: b2927d6220bec36222693b14af9310fd3f56ede2 [file] [log] [blame]
PineaFan64486c42022-12-28 09:21:04 +00001import type { SlashCommandSubcommandGroupBuilder } from "@discordjs/builders";
2import 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 }
PineaFan64486c42022-12-28 09:21:04 +000067
68 for (const subcommand of fetched.subcommands) {
PineaFandf4996f2023-01-01 14:20:06 +000069 let fetchedCommand;
70 if (subcommand.command instanceof Function) {
71 fetchedCommand = subcommand.command(new Discord.SlashCommandSubcommandBuilder());
72 } else {
73 fetchedCommand = subcommand.command;
74 }
75 client.commands[commandString! + "/" + fetchedCommand.name] = subcommand
76 command.addSubcommand(fetchedCommand);
PineaFan64486c42022-12-28 09:21:04 +000077 }
78 for (const group of fetched.subcommandGroups) {
PineaFandf4996f2023-01-01 14:20:06 +000079 command.addSubcommandGroup(group.command);
PineaFan64486c42022-12-28 09:21:04 +000080 };
81 return command;
82 };
83}