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