blob: ef45875f3131a14a5e3a622ff06157d1a0405d29 [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";
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) {
PineaFanb0d0c242023-02-05 10:59:45 +000035 const processedCommand = subcommand.command(new SlashCommandSubcommandBuilder());
TheCodedProffe0a7862023-01-27 20:36:35 -050036 client.commands["commands/" + path + "/" + processedCommand.name] = [subcommand, { name: processedCommand.name, description: processedCommand.description }]
37 subcommandGroup.addSubcommand(processedCommand);
PineaFan64486c42022-12-28 09:21:04 +000038 };
39
40 return subcommandGroup;
41 };
42}
43
PineaFan538d3752023-01-12 21:48:23 +000044export async function command(
45 name: string,
46 description: string,
47 path: string,
48 commandString: string | undefined = undefined,
49 nameLocalizations?: Record<string, string>,
50 descriptionLocalizations?: Record<string, string>,
51 userPermissions?: Discord.PermissionsString[],
52 allowedInDMs?: boolean
53) {
PineaFandf4996f2023-01-01 14:20:06 +000054 // If the name of the command does not match the path (e.g. attachment.ts has /attachments), use commandString
55 commandString = "commands/" + (commandString ?? path);
PineaFan64486c42022-12-28 09:21:04 +000056 const fetched = await getSubcommandsInFolder(config.commandsFolder + "/" + path);
PineaFan752af462022-12-31 21:59:38 +000057 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}`)
TheCodedProf8c295232023-01-27 20:52:24 -050058 // console.log({name: name, description: description})
TheCodedProffe0a7862023-01-27 20:36:35 -050059 client.commands[commandString!] = [undefined, { name: name, description: description }]
PineaFan64486c42022-12-28 09:21:04 +000060 return (command: SlashCommandBuilder) => {
61 command.setName(name)
62 command.setDescription(description)
PineaFan538d3752023-01-12 21:48:23 +000063 command.setNameLocalizations(nameLocalizations ?? {})
64 command.setDescriptionLocalizations(descriptionLocalizations ?? {})
65 command.setDMPermission(allowedInDMs ?? false)
66 if (userPermissions) {
67 const bitfield = new Discord.PermissionsBitField()
68 bitfield.add(userPermissions)
69 command.setDefaultMemberPermissions(bitfield.bitfield)
70 }
PineaFan64486c42022-12-28 09:21:04 +000071
72 for (const subcommand of fetched.subcommands) {
PineaFandf4996f2023-01-01 14:20:06 +000073 let fetchedCommand;
74 if (subcommand.command instanceof Function) {
TheCodedProffe0a7862023-01-27 20:36:35 -050075 fetchedCommand = subcommand.command(new SlashCommandSubcommandBuilder());
PineaFandf4996f2023-01-01 14:20:06 +000076 } else {
77 fetchedCommand = subcommand.command;
78 }
TheCodedProff86ba092023-01-27 17:10:07 -050079 client.commands[commandString! + "/" + fetchedCommand.name] = [subcommand, { name: fetchedCommand.name, description: fetchedCommand.description }]
PineaFandf4996f2023-01-01 14:20:06 +000080 command.addSubcommand(fetchedCommand);
PineaFan64486c42022-12-28 09:21:04 +000081 }
82 for (const group of fetched.subcommandGroups) {
PineaFanb0d0c242023-02-05 10:59:45 +000083 const processedCommand = group.command(new SlashCommandSubcommandGroupBuilder());
TheCodedProffe0a7862023-01-27 20:36:35 -050084 client.commands[commandString! + "/" + processedCommand.name] = [undefined, { name: processedCommand.name, description: processedCommand.description }]
85 command.addSubcommandGroup(processedCommand);
PineaFan64486c42022-12-28 09:21:04 +000086 };
87 return command;
88 };
89}