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 | |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame^] | 25 | const command = new SlashCommandBuilder().setName("help").setDescription("Shows help for commands").setDMPermission(true); |
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 | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame^] | 130 | optionString += ` - \`${option.name}\` (${ApplicationCommandOptionType[option.type].replace("Integer", "Number").replace("String", "Text")}) - ${ |
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; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame^] | 139 | if (interaction.guild && APICommand?.check) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 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( |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame^] | 143 | `${getEmojiByName(styles[currentPath[0]]!.emoji)} **${capitalize(currentData.name)}** | ${currentData.mention}\n\n` + |
| 144 | `${currentData.description}\n\n` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 145 | (APICommand |
| 146 | ? `${getEmojiByName(allowedToRun ? "CONTROL.TICK" : "CONTROL.CROSS")} You ${ |
| 147 | allowedToRun ? "" : "don't " |
| 148 | }` + `have permission to use this command\n\n` |
| 149 | : "") + |
| 150 | (optionString.length > 0 ? "**Options:**\n" + optionString : "") |
| 151 | ); |
| 152 | const subcommands = current.options.filter( |
| 153 | (option) => option.type === ApplicationCommandOptionType.Subcommand |
| 154 | ); |
| 155 | const subcommandGroups = current.options.filter( |
| 156 | (option) => option.type === ApplicationCommandOptionType.SubcommandGroup |
| 157 | ); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 158 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 159 | if (subcommandGroups.length > 0) { |
| 160 | subcommandGroupRow.components[0]!.addOptions( |
| 161 | new StringSelectMenuOptionBuilder() |
| 162 | .setLabel("Select a subcommand") |
| 163 | .setValue("none") |
| 164 | .setDefault(currentPath[1] === "none"), |
| 165 | ...subcommandGroups.map((option) => |
| 166 | new StringSelectMenuOptionBuilder() |
| 167 | .setLabel(capitalize(option.name)) |
| 168 | .setValue(option.name) |
| 169 | .setDefault(currentPath[1] === option.name) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 170 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 171 | ); |
| 172 | if ( |
| 173 | subcommandGroupRow.components[0]!.options.find( |
| 174 | (option) => option.data.default && option.data.value !== "none" |
| 175 | ) |
| 176 | ) { |
| 177 | const subsubcommands = |
| 178 | ( |
| 179 | subcommandGroups.find( |
| 180 | (option) => option.name === currentPath[1] |
| 181 | )! as ApplicationCommandSubGroup |
| 182 | ).options ?? []; |
| 183 | subcommandRow.components[0]!.addOptions( |
| 184 | new StringSelectMenuOptionBuilder() |
| 185 | .setLabel("Select a subcommand") |
| 186 | .setValue("none") |
| 187 | .setDefault(currentPath[2] === "none"), |
| 188 | ...subsubcommands.map((option) => |
| 189 | new StringSelectMenuOptionBuilder() |
| 190 | .setLabel(capitalize(option.name)) |
| 191 | .setValue(option.name) |
| 192 | .setDefault(currentPath[2] === option.name) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 193 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 194 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 195 | } |
| 196 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 197 | if (subcommands.length > 0) { |
| 198 | subcommandGroupRow.components[0]!.addOptions( |
| 199 | ...subcommands.map((option) => |
| 200 | new StringSelectMenuOptionBuilder() |
| 201 | .setLabel(capitalize(option.name)) |
| 202 | .setValue(option.name) |
| 203 | .setDefault(currentPath[1] === option.name) |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 204 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 205 | ); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 206 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 207 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 208 | |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 209 | const cmps = [commandRow]; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 210 | if (subcommandGroupRow.components[0]!.options.length > 0) cmps.push(subcommandGroupRow); |
| 211 | if (subcommandRow.components[0]!.options.length > 0) cmps.push(subcommandRow); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 212 | |
| 213 | await interaction.editReply({ embeds: [embed], components: cmps }); |
| 214 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 215 | let i: StringSelectMenuInteraction; |
| 216 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 217 | i = await m.awaitMessageComponent<ComponentType.StringSelect>({ |
| 218 | filter: (newInteraction) => interaction.user.id === newInteraction.user.id, |
| 219 | time: 300000 |
| 220 | }); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 221 | } catch (e) { |
| 222 | closed = true; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 223 | continue; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 224 | } |
| 225 | await i.deferUpdate(); |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 226 | const value = i.values[0]!; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 227 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 228 | case "commandRow": { |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 229 | currentPath = [value, "", ""]; |
| 230 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 231 | } |
| 232 | case "subcommandGroupRow": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 233 | currentPath = [currentPath[0], value, ""]; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 234 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 235 | } |
| 236 | case "subcommandRow": { |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 237 | currentPath[2] = value; |
| 238 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 239 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 240 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 241 | } while (!closed); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 242 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 243 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 244 | export { command as command }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 245 | export { callback }; |