blob: dcf45cab765e0f4f167bd03cbc14c817ba2450ec [file] [log] [blame]
TheCodedProff86ba092023-01-27 17:10:07 -05001import type Discord from "discord.js";
2import client from "./client.js";
3
Skyler Grey877a5c92023-03-11 21:02:36 +00004/**
Skyler Grey6a0bab52023-03-15 00:10:26 +00005 * @param name The name of the command, not including a leading slash. This can be space or slash separated e.g. "mod/about" or "mod about"
6 * @returns A string which when put into Discord will mention the command if the command exists or a codeblock with the command name if it does not
7 *
8 * @throws Will throw an error if as empty string is passed
9 **/
TheCodedProff86ba092023-01-27 17:10:07 -050010export const getCommandMentionByName = (name: string): string => {
Skyler Greyda16adf2023-03-05 10:22:12 +000011 const split = name.replaceAll("/", " ").split(" ");
Skyler Grey877a5c92023-03-11 21:02:36 +000012 const commandName: string | undefined = split[0];
13 if (commandName === undefined) throw new RangeError(`Invalid command ${name} provided to getCommandByName`);
TheCodedProff86ba092023-01-27 17:10:07 -050014
15 const filterCommand = (command: Discord.ApplicationCommand) => command.name === commandName;
16
Skyler Greyda16adf2023-03-05 10:22:12 +000017 const command = client.fetchedCommands.filter((c) => filterCommand(c));
Skyler Grey877a5c92023-03-11 21:02:36 +000018 const commandID = command.first()?.id;
19
20 if (commandID === undefined) return `\`/${name.replaceAll("/", " ")}\``;
21
TheCodedProff86ba092023-01-27 17:10:07 -050022 return `</${split.join(" ")}:${commandID}>`;
Skyler Greyda16adf2023-03-05 10:22:12 +000023};
TheCodedProff86ba092023-01-27 17:10:07 -050024
Skyler Grey877a5c92023-03-11 21:02:36 +000025/**
Skyler Grey6a0bab52023-03-15 00:10:26 +000026 * @param name The name of the command, not including a leading slash. This can be space or slash separated e.g. "mod/about" or "mod about"
27 * @returns An object containing the command name, the command description and a string which when put into Discord will mention the command
28 *
29 * @throws Will throw an error if the command doesn't exist
30 * @throws Will throw an error if as empty string is passed
31 **/
Skyler Greyda16adf2023-03-05 10:22:12 +000032export const getCommandByName = (name: string): { name: string; description: string; mention: string } => {
33 const split = name.replaceAll(" ", "/");
Skyler Grey877a5c92023-03-11 21:02:36 +000034 const command = client.commands["commands/" + split];
35
36 if (command === undefined) throw new RangeError(`Invalid command ${name} provided to getCommandByName`);
37
TheCodedProff86ba092023-01-27 17:10:07 -050038 const mention = getCommandMentionByName(name);
39 return {
40 name: command[1].name,
41 description: command[1].description,
42 mention: mention
Skyler Greyda16adf2023-03-05 10:22:12 +000043 };
44};