TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 1 | import type Discord from "discord.js"; |
| 2 | import client from "./client.js"; |
| 3 | |
Skyler Grey | 877a5c9 | 2023-03-11 21:02:36 +0000 | [diff] [blame] | 4 | /** |
| 5 | * @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 | **/ |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 10 | export const getCommandMentionByName = (name: string): string => { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 11 | const split = name.replaceAll("/", " ").split(" "); |
Skyler Grey | 877a5c9 | 2023-03-11 21:02:36 +0000 | [diff] [blame] | 12 | const commandName: string | undefined = split[0]; |
| 13 | if (commandName === undefined) throw new RangeError(`Invalid command ${name} provided to getCommandByName`); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 14 | |
| 15 | const filterCommand = (command: Discord.ApplicationCommand) => command.name === commandName; |
| 16 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 17 | const command = client.fetchedCommands.filter((c) => filterCommand(c)); |
Skyler Grey | 877a5c9 | 2023-03-11 21:02:36 +0000 | [diff] [blame] | 18 | const commandID = command.first()?.id; |
| 19 | |
| 20 | if (commandID === undefined) return `\`/${name.replaceAll("/", " ")}\``; |
| 21 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 22 | return `</${split.join(" ")}:${commandID}>`; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 23 | }; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 24 | |
Skyler Grey | 877a5c9 | 2023-03-11 21:02:36 +0000 | [diff] [blame] | 25 | /** |
| 26 | * @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 Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 32 | export const getCommandByName = (name: string): { name: string; description: string; mention: string } => { |
| 33 | const split = name.replaceAll(" ", "/"); |
Skyler Grey | 877a5c9 | 2023-03-11 21:02:36 +0000 | [diff] [blame] | 34 | const command = client.commands["commands/" + split]; |
| 35 | |
| 36 | if (command === undefined) throw new RangeError(`Invalid command ${name} provided to getCommandByName`); |
| 37 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 38 | const mention = getCommandMentionByName(name); |
| 39 | return { |
| 40 | name: command[1].name, |
| 41 | description: command[1].description, |
| 42 | mention: mention |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 43 | }; |
| 44 | }; |