TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 1 | import { |
| 2 | ActionRowBuilder, |
| 3 | CommandInteraction, |
| 4 | StringSelectMenuBuilder, |
| 5 | ApplicationCommandOptionType, |
| 6 | ApplicationCommandType, |
| 7 | StringSelectMenuOptionBuilder, |
| 8 | SlashCommandBuilder, |
| 9 | StringSelectMenuInteraction, |
| 10 | ComponentType, |
| 11 | APIMessageComponentEmoji, |
| 12 | ApplicationCommandSubGroup, |
| 13 | PermissionsBitField, |
| 14 | Interaction, |
| 15 | ApplicationCommandOption, |
| 16 | ApplicationCommandSubCommand |
| 17 | } from "discord.js"; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 18 | import client from "../utils/client.js"; |
| 19 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 20 | import { LoadingEmbed } from "../utils/defaults.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 21 | import { capitalize } from "../utils/generateKeyValueList.js"; |
| 22 | import { getCommandByName, getCommandMentionByName } from "../utils/getCommandDataByName.js"; |
| 23 | import getEmojiByName from "../utils/getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 24 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 25 | const command = new SlashCommandBuilder().setName("help").setDescription("Shows help for commands"); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 26 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 27 | const styles: Record<string, { emoji: string }> = { |
| 28 | help: { emoji: "NUCLEUS.LOGO" }, |
| 29 | mod: { emoji: "PUNISH.BAN.RED" }, |
| 30 | nucleus: { emoji: "NUCLEUS.LOGO" }, |
| 31 | privacy: { emoji: "NUCLEUS.LOGO" }, |
| 32 | role: { emoji: "GUILD.ROLES.DELETE" }, |
| 33 | rolemenu: { emoji: "GUILD.ROLES.DELETE" }, |
| 34 | server: { emoji: "GUILD.RED" }, |
| 35 | settings: { emoji: "GUILD.SETTINGS.RED" }, |
| 36 | tag: { emoji: "PUNISH.NICKNAME.RED" }, |
| 37 | tags: { emoji: "PUNISH.NICKNAME.RED" }, |
| 38 | ticket: { emoji: "GUILD.TICKET.CLOSE" }, |
| 39 | user: { emoji: "MEMBER.LEAVE" }, |
| 40 | verify: { emoji: "CONTROL.REDTICK" } |
| 41 | }; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 42 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 44 | const m = await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 45 | const commands = client.fetchedCommands; |
| 46 | |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 47 | let closed = false; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 48 | let currentPath: [string, string, string] = ["", "", ""]; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 49 | do { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 50 | const commandRow = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 51 | new StringSelectMenuBuilder() |
| 52 | .setCustomId("commandRow") |
| 53 | .setPlaceholder("Select a command") |
| 54 | .addOptions( |
| 55 | ...commands |
| 56 | .filter((command) => command.type === ApplicationCommandType.ChatInput) |
| 57 | .map((command) => { |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 58 | const builder = new StringSelectMenuOptionBuilder() |
| 59 | .setLabel(capitalize(command.name)) |
| 60 | .setValue(command.name) |
| 61 | .setDescription(command.description) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 62 | .setDefault(currentPath[0] === command.name); |
| 63 | if (styles[command.name]) |
| 64 | builder.setEmoji( |
| 65 | getEmojiByName(styles[command.name]!.emoji, "id") as APIMessageComponentEmoji |
| 66 | ); |
| 67 | return builder; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 68 | }) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 69 | ) |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 70 | ); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 71 | const subcommandGroupRow = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 72 | new StringSelectMenuBuilder().setCustomId("subcommandGroupRow") |
| 73 | ); |
| 74 | const subcommandRow = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 75 | new StringSelectMenuBuilder().setCustomId("subcommandRow") |
| 76 | ); |
| 77 | const embed = new EmojiEmbed().setTitle("Help").setStatus("Danger").setEmoji("NUCLEUS.LOGO"); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 78 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 79 | if (currentPath[0] === "" || currentPath[0] === "help") { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 80 | embed.setDescription( |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 81 | `Welcome to Nucleus\n\n` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 82 | `Select a command to get started${ |
| 83 | (interaction.member?.permissions as PermissionsBitField).has("ManageGuild") |
| 84 | ? `, or run ${getCommandMentionByName("nucleus/guide")} for commands to set up your server` |
| 85 | : `` |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 86 | }\n\n\n` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 87 | `Nucleus is fully [open source](https://github.com/clicksminuteper/Nucleus), and all currently free features will remain free forever.\n\n` + |
TheCodedProf | e915a38 | 2023-03-05 16:44:29 -0500 | [diff] [blame] | 88 | `You can invite Nucleus to your server using ${getCommandMentionByName("nucleus/invite")}\n` + |
| 89 | `Our support server can be found [here](https://discord.gg/bPaNnxe), and we can be emailed at support@clicks.codes` |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 90 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 91 | } else { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 92 | const currentData = getCommandByName( |
| 93 | currentPath.filter((value) => value !== "" && value !== "none").join("/") |
| 94 | ); |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 95 | const current = commands.find((command) => command.name === currentPath[0])!; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 96 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 97 | let optionString = ``; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 98 | let options: (ApplicationCommandOption & { |
| 99 | nameLocalized?: string; |
| 100 | descriptionLocalized?: string; |
| 101 | })[] = []; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 102 | //options |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 103 | if ( |
| 104 | currentPath[1] !== "" && |
| 105 | currentPath[1] !== "none" && |
| 106 | currentPath[2] !== "" && |
| 107 | currentPath[2] !== "none" |
| 108 | ) { |
| 109 | const Op = current.options.find( |
| 110 | (option) => option.name === currentPath[1] |
| 111 | )! as ApplicationCommandSubGroup; |
| 112 | const Op2 = Op.options!.find((option) => option.name === currentPath[2])!; |
| 113 | options = Op2.options ?? []; |
| 114 | } else if (currentPath[1] !== "" && currentPath[1] !== "none") { |
| 115 | let Op = current.options.find((option) => option.name === currentPath[1])!; |
| 116 | if (Op.type === ApplicationCommandOptionType.SubcommandGroup) { |
| 117 | options = []; |
TheCodedProf | cd187ee | 2023-01-27 20:46:12 -0500 | [diff] [blame] | 118 | } else { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 119 | Op = Op as ApplicationCommandSubCommand; |
| 120 | options = Op.options ?? []; |
TheCodedProf | cd187ee | 2023-01-27 20:46:12 -0500 | [diff] [blame] | 121 | } |
| 122 | } else { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 123 | options = current.options.filter( |
| 124 | (option) => |
| 125 | option.type !== ApplicationCommandOptionType.SubcommandGroup && |
| 126 | option.type !== ApplicationCommandOptionType.Subcommand |
| 127 | ); |
TheCodedProf | cd187ee | 2023-01-27 20:46:12 -0500 | [diff] [blame] | 128 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 129 | for (const option of options) { |
TheCodedProf | 80ad854 | 2023-03-10 12:52:33 -0500 | [diff] [blame^] | 130 | optionString += `> \`${option.name}\` (${ApplicationCommandOptionType[option.type]}) - ${ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 131 | option.description |
| 132 | }\n`; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 133 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 134 | const APICommand = |
| 135 | client.commands[ |
| 136 | "commands/" + currentPath.filter((value) => value !== "" && value !== "none").join("/") |
| 137 | ]![0]; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 138 | let allowedToRun = true; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 139 | if (APICommand?.check) { |
| 140 | allowedToRun = await APICommand.check(interaction as Interaction, true); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 141 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 142 | embed.setDescription( |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 143 | `${getEmojiByName(styles[currentPath[0]]!.emoji)} **${capitalize(currentData.name)}**\n> ${ |
| 144 | currentData.mention |
| 145 | }\n\n` + |
| 146 | `> ${currentData.description}\n\n` + |
| 147 | (APICommand |
| 148 | ? `${getEmojiByName(allowedToRun ? "CONTROL.TICK" : "CONTROL.CROSS")} You ${ |
| 149 | allowedToRun ? "" : "don't " |
| 150 | }` + `have permission to use this command\n\n` |
| 151 | : "") + |
| 152 | (optionString.length > 0 ? "**Options:**\n" + optionString : "") |
| 153 | ); |
| 154 | const subcommands = current.options.filter( |
| 155 | (option) => option.type === ApplicationCommandOptionType.Subcommand |
| 156 | ); |
| 157 | const subcommandGroups = current.options.filter( |
| 158 | (option) => option.type === ApplicationCommandOptionType.SubcommandGroup |
| 159 | ); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 160 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 161 | if (subcommandGroups.length > 0) { |
| 162 | subcommandGroupRow.components[0]!.addOptions( |
| 163 | new StringSelectMenuOptionBuilder() |
| 164 | .setLabel("Select a subcommand") |
| 165 | .setValue("none") |
| 166 | .setDefault(currentPath[1] === "none"), |
| 167 | ...subcommandGroups.map((option) => |
| 168 | new StringSelectMenuOptionBuilder() |
| 169 | .setLabel(capitalize(option.name)) |
| 170 | .setValue(option.name) |
| 171 | .setDefault(currentPath[1] === option.name) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 172 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 173 | ); |
| 174 | if ( |
| 175 | subcommandGroupRow.components[0]!.options.find( |
| 176 | (option) => option.data.default && option.data.value !== "none" |
| 177 | ) |
| 178 | ) { |
| 179 | const subsubcommands = |
| 180 | ( |
| 181 | subcommandGroups.find( |
| 182 | (option) => option.name === currentPath[1] |
| 183 | )! as ApplicationCommandSubGroup |
| 184 | ).options ?? []; |
| 185 | subcommandRow.components[0]!.addOptions( |
| 186 | new StringSelectMenuOptionBuilder() |
| 187 | .setLabel("Select a subcommand") |
| 188 | .setValue("none") |
| 189 | .setDefault(currentPath[2] === "none"), |
| 190 | ...subsubcommands.map((option) => |
| 191 | new StringSelectMenuOptionBuilder() |
| 192 | .setLabel(capitalize(option.name)) |
| 193 | .setValue(option.name) |
| 194 | .setDefault(currentPath[2] === option.name) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 195 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 196 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 197 | } |
| 198 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 199 | if (subcommands.length > 0) { |
| 200 | subcommandGroupRow.components[0]!.addOptions( |
| 201 | ...subcommands.map((option) => |
| 202 | new StringSelectMenuOptionBuilder() |
| 203 | .setLabel(capitalize(option.name)) |
| 204 | .setValue(option.name) |
| 205 | .setDefault(currentPath[1] === option.name) |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 206 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 207 | ); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 208 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 209 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 210 | |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 211 | const cmps = [commandRow]; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 212 | if (subcommandGroupRow.components[0]!.options.length > 0) cmps.push(subcommandGroupRow); |
| 213 | if (subcommandRow.components[0]!.options.length > 0) cmps.push(subcommandRow); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 214 | |
| 215 | await interaction.editReply({ embeds: [embed], components: cmps }); |
| 216 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 217 | let i: StringSelectMenuInteraction; |
| 218 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 219 | i = await m.awaitMessageComponent<ComponentType.StringSelect>({ |
| 220 | filter: (newInteraction) => interaction.user.id === newInteraction.user.id, |
| 221 | time: 300000 |
| 222 | }); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 223 | } catch (e) { |
| 224 | closed = true; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 225 | continue; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 226 | } |
| 227 | await i.deferUpdate(); |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 228 | const value = i.values[0]!; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 229 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 230 | case "commandRow": { |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 231 | currentPath = [value, "", ""]; |
| 232 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 233 | } |
| 234 | case "subcommandGroupRow": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 235 | currentPath = [currentPath[0], value, ""]; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 236 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 237 | } |
| 238 | case "subcommandRow": { |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 239 | currentPath[2] = value; |
| 240 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 241 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 242 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 243 | } while (!closed); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 244 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 245 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 246 | export { command as command }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 247 | export { callback }; |