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` + |
| 88 | `You can invite Nucleus to your server using ${getCommandMentionByName("nucleus/invite")}` |
| 89 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 90 | } else { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 91 | const currentData = getCommandByName( |
| 92 | currentPath.filter((value) => value !== "" && value !== "none").join("/") |
| 93 | ); |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 94 | const current = commands.find((command) => command.name === currentPath[0])!; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 95 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 96 | let optionString = ``; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 97 | let options: (ApplicationCommandOption & { |
| 98 | nameLocalized?: string; |
| 99 | descriptionLocalized?: string; |
| 100 | })[] = []; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 101 | //options |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 102 | if ( |
| 103 | currentPath[1] !== "" && |
| 104 | currentPath[1] !== "none" && |
| 105 | currentPath[2] !== "" && |
| 106 | currentPath[2] !== "none" |
| 107 | ) { |
| 108 | const Op = current.options.find( |
| 109 | (option) => option.name === currentPath[1] |
| 110 | )! as ApplicationCommandSubGroup; |
| 111 | const Op2 = Op.options!.find((option) => option.name === currentPath[2])!; |
| 112 | options = Op2.options ?? []; |
| 113 | } else if (currentPath[1] !== "" && currentPath[1] !== "none") { |
| 114 | let Op = current.options.find((option) => option.name === currentPath[1])!; |
| 115 | if (Op.type === ApplicationCommandOptionType.SubcommandGroup) { |
| 116 | options = []; |
TheCodedProf | cd187ee | 2023-01-27 20:46:12 -0500 | [diff] [blame] | 117 | } else { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 118 | Op = Op as ApplicationCommandSubCommand; |
| 119 | options = Op.options ?? []; |
TheCodedProf | cd187ee | 2023-01-27 20:46:12 -0500 | [diff] [blame] | 120 | } |
| 121 | } else { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 122 | options = current.options.filter( |
| 123 | (option) => |
| 124 | option.type !== ApplicationCommandOptionType.SubcommandGroup && |
| 125 | option.type !== ApplicationCommandOptionType.Subcommand |
| 126 | ); |
TheCodedProf | cd187ee | 2023-01-27 20:46:12 -0500 | [diff] [blame] | 127 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 128 | for (const option of options) { |
| 129 | optionString += `> ${option.name} (${ApplicationCommandOptionType[option.type]})- ${ |
| 130 | option.description |
| 131 | }\n`; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 132 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 133 | const APICommand = |
| 134 | client.commands[ |
| 135 | "commands/" + currentPath.filter((value) => value !== "" && value !== "none").join("/") |
| 136 | ]![0]; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 137 | let allowedToRun = true; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 138 | if (APICommand?.check) { |
| 139 | allowedToRun = await APICommand.check(interaction as Interaction, true); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 140 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 141 | embed.setDescription( |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 142 | `${getEmojiByName(styles[currentPath[0]]!.emoji)} **${capitalize(currentData.name)}**\n> ${ |
| 143 | currentData.mention |
| 144 | }\n\n` + |
| 145 | `> ${currentData.description}\n\n` + |
| 146 | (APICommand |
| 147 | ? `${getEmojiByName(allowedToRun ? "CONTROL.TICK" : "CONTROL.CROSS")} You ${ |
| 148 | allowedToRun ? "" : "don't " |
| 149 | }` + `have permission to use this command\n\n` |
| 150 | : "") + |
| 151 | (optionString.length > 0 ? "**Options:**\n" + optionString : "") |
| 152 | ); |
| 153 | const subcommands = current.options.filter( |
| 154 | (option) => option.type === ApplicationCommandOptionType.Subcommand |
| 155 | ); |
| 156 | const subcommandGroups = current.options.filter( |
| 157 | (option) => option.type === ApplicationCommandOptionType.SubcommandGroup |
| 158 | ); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 159 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 160 | if (subcommandGroups.length > 0) { |
| 161 | subcommandGroupRow.components[0]!.addOptions( |
| 162 | new StringSelectMenuOptionBuilder() |
| 163 | .setLabel("Select a subcommand") |
| 164 | .setValue("none") |
| 165 | .setDefault(currentPath[1] === "none"), |
| 166 | ...subcommandGroups.map((option) => |
| 167 | new StringSelectMenuOptionBuilder() |
| 168 | .setLabel(capitalize(option.name)) |
| 169 | .setValue(option.name) |
| 170 | .setDefault(currentPath[1] === option.name) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 171 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 172 | ); |
| 173 | if ( |
| 174 | subcommandGroupRow.components[0]!.options.find( |
| 175 | (option) => option.data.default && option.data.value !== "none" |
| 176 | ) |
| 177 | ) { |
| 178 | const subsubcommands = |
| 179 | ( |
| 180 | subcommandGroups.find( |
| 181 | (option) => option.name === currentPath[1] |
| 182 | )! as ApplicationCommandSubGroup |
| 183 | ).options ?? []; |
| 184 | subcommandRow.components[0]!.addOptions( |
| 185 | new StringSelectMenuOptionBuilder() |
| 186 | .setLabel("Select a subcommand") |
| 187 | .setValue("none") |
| 188 | .setDefault(currentPath[2] === "none"), |
| 189 | ...subsubcommands.map((option) => |
| 190 | new StringSelectMenuOptionBuilder() |
| 191 | .setLabel(capitalize(option.name)) |
| 192 | .setValue(option.name) |
| 193 | .setDefault(currentPath[2] === option.name) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 194 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 195 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 196 | } |
| 197 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 198 | if (subcommands.length > 0) { |
| 199 | subcommandGroupRow.components[0]!.addOptions( |
| 200 | ...subcommands.map((option) => |
| 201 | new StringSelectMenuOptionBuilder() |
| 202 | .setLabel(capitalize(option.name)) |
| 203 | .setValue(option.name) |
| 204 | .setDefault(currentPath[1] === option.name) |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 205 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 206 | ); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 207 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 208 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 209 | |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 210 | const cmps = [commandRow]; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 211 | if (subcommandGroupRow.components[0]!.options.length > 0) cmps.push(subcommandGroupRow); |
| 212 | if (subcommandRow.components[0]!.options.length > 0) cmps.push(subcommandRow); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 213 | |
| 214 | await interaction.editReply({ embeds: [embed], components: cmps }); |
| 215 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 216 | let i: StringSelectMenuInteraction; |
| 217 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 218 | i = await m.awaitMessageComponent<ComponentType.StringSelect>({ |
| 219 | filter: (newInteraction) => interaction.user.id === newInteraction.user.id, |
| 220 | time: 300000 |
| 221 | }); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 222 | } catch (e) { |
| 223 | closed = true; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 224 | continue; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 225 | } |
| 226 | await i.deferUpdate(); |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 227 | const value = i.values[0]!; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 228 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 229 | case "commandRow": { |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 230 | currentPath = [value, "", ""]; |
| 231 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 232 | } |
| 233 | case "subcommandGroupRow": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 234 | currentPath = [currentPath[0], value, ""]; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 235 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 236 | } |
| 237 | case "subcommandRow": { |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 238 | currentPath[2] = value; |
| 239 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 240 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 241 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 242 | } while (!closed); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 243 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 244 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 245 | export { command as command }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 246 | export { callback }; |