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"}, |
| 42 | "verify": {emoji: "CONTROL.BLOCKTICK"} |
| 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 | f86ba09 | 2023-01-27 17:10:07 -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` + |
| 87 | `Select a command to get started${(interaction.member?.permissions as PermissionsBitField).has("ManageGuild") ? `, or run ${getCommandMentionByName("nucleus/guide")} for commands to set up your server` : ``}` // FIXME |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 88 | ) |
| 89 | } else { |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 90 | let currentData = getCommandByName(currentPath.filter(value => value !== "" && value !== "none").join('/')); |
| 91 | let current = commands.find((command) => command.name === currentPath[0])!; |
| 92 | |
| 93 | let optionString = `` |
| 94 | let options: (ApplicationCommandOption & { |
| 95 | nameLocalized?: string; |
| 96 | descriptionLocalized?: string; |
| 97 | })[] = []; |
| 98 | //options |
| 99 | for(const option of options) { |
| 100 | optionString += `> ${option.name} (${option.type})- ${option.description}\n` |
| 101 | } |
| 102 | const APICommand = client.commands["commands/" + currentPath.filter(value => value !== "" && value !== "none").join("/")]![0] |
| 103 | let allowedToRun = true; |
| 104 | if(APICommand?.check) { |
| 105 | APICommand?.check(interaction as Interaction, true) |
| 106 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 107 | embed.setDescription( |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 108 | `${getEmojiByName(styles[currentPath[0]]!.emoji)} **${capitalize(currentData.name)}**\n> ${currentData.mention}\n\n` + |
| 109 | `> ${currentData.description}\n\n` + |
| 110 | (APICommand ? (`${getEmojiByName(allowedToRun ? "CONTROL.TICK" : "CONTROL.CROSS")} You ${allowedToRun ? "" : "don't "}` + |
| 111 | `have permission to use this command\n\n`) : "") + |
| 112 | ((optionString.length > 0) ? "**Options:**\n" + optionString : "") |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 113 | ) |
TheCodedProf | 92a003c | 2023-01-25 18:01:21 -0500 | [diff] [blame] | 114 | const subcommands = current.options.filter((option) => option.type === ApplicationCommandOptionType.Subcommand); |
| 115 | const subcommandGroups = current.options.filter((option) => option.type === ApplicationCommandOptionType.SubcommandGroup); |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 116 | |
TheCodedProf | 92a003c | 2023-01-25 18:01:21 -0500 | [diff] [blame] | 117 | if(subcommandGroups.length > 0) { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 118 | subcommandGroupRow.components[0]! |
| 119 | .addOptions( |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 120 | new StringSelectMenuOptionBuilder().setLabel("Select a subcommand").setValue("none").setDefault(currentPath[1] === "none"), |
| 121 | ...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] | 122 | ) |
TheCodedProf | fe0a786 | 2023-01-27 20:36:35 -0500 | [diff] [blame^] | 123 | if(subcommandGroupRow.components[0]!.options.find((option) => option.data.default && option.data.value !== "none")) { |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 124 | let 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] | 125 | subcommandRow.components[0]! |
| 126 | .addOptions( |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 127 | new StringSelectMenuOptionBuilder().setLabel("Select a subcommand").setValue("none").setDefault(currentPath[2] === "none"), |
| 128 | ...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] | 129 | ) |
| 130 | } |
| 131 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 132 | if(subcommands.length > 0) { |
| 133 | subcommandGroupRow.components[0]! |
| 134 | .addOptions( |
| 135 | ...subcommands.map((option) => new StringSelectMenuOptionBuilder().setLabel(capitalize(option.name)).setValue(option.name).setDefault(currentPath[1] === option.name)) |
| 136 | ) |
| 137 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 138 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 139 | |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 140 | let cmps = [commandRow]; |
| 141 | if(subcommandGroupRow.components[0]!.options.length > 0) cmps.push(subcommandGroupRow); |
| 142 | if(subcommandRow.components[0]!.options.length > 0) cmps.push(subcommandRow); |
| 143 | |
| 144 | await interaction.editReply({ embeds: [embed], components: cmps }); |
| 145 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 146 | let i: StringSelectMenuInteraction; |
| 147 | try { |
| 148 | i = await m.awaitMessageComponent<ComponentType.StringSelect>({filter: (newInteraction) => interaction.user.id === newInteraction.user.id,time: 300000}) |
| 149 | } catch (e) { |
| 150 | closed = true; |
| 151 | break; |
| 152 | } |
| 153 | await i.deferUpdate(); |
| 154 | let value = i.values[0]!; |
| 155 | switch(i.customId) { |
| 156 | case "commandRow": |
| 157 | currentPath = [value, "", ""]; |
| 158 | break; |
| 159 | case "subcommandGroupRow": |
| 160 | currentPath = [currentPath[0], value , ""]; |
| 161 | break; |
| 162 | case "subcommandRow": |
| 163 | currentPath[2] = value; |
| 164 | break; |
| 165 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 166 | |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 167 | } while (!closed); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 168 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 169 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 170 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 171 | export { command as command }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 172 | export { callback }; |