PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../utils/defaults.js"; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 2 | import Discord, { |
| 3 | CommandInteraction, |
| 4 | Message, |
| 5 | ActionRowBuilder, |
| 6 | StringSelectMenuBuilder, |
| 7 | ButtonBuilder, |
| 8 | ButtonStyle, |
| 9 | StringSelectMenuOptionBuilder, |
| 10 | APIMessageComponentEmoji, |
| 11 | TextInputBuilder, |
| 12 | StringSelectMenuInteraction, |
| 13 | ButtonInteraction, |
| 14 | MessageComponentInteraction, |
| 15 | ChannelSelectMenuBuilder, |
| 16 | ChannelSelectMenuInteraction, |
| 17 | ModalBuilder |
| 18 | } from "discord.js"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 19 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 20 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 21 | import client from "../../utils/client.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 22 | import convertCurlyBracketString from "../../utils/convertCurlyBracketString.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | import singleNotify from "../../utils/singleNotify.js"; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 24 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
| 25 | import createPageIndicator from "../../utils/createPageIndicator.js"; |
| 26 | import { modalInteractionCollector } from "../../utils/dualCollector.js"; |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 27 | |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 28 | const command = (builder: SlashCommandSubcommandBuilder) => |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 29 | builder.setName("stats").setDescription("Controls channels which update when someone joins or leaves the server"); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 30 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 31 | const showModal = async (interaction: MessageComponentInteraction, current: { enabled: boolean; name: string }) => { |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 32 | await interaction.showModal( |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 33 | new ModalBuilder() |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 34 | .setCustomId("modal") |
| 35 | .setTitle(`Stats channel name`) |
| 36 | .addComponents( |
| 37 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 38 | new TextInputBuilder() |
| 39 | .setCustomId("ex1") |
| 40 | .setLabel("Server Info (1/3)") |
| 41 | .setPlaceholder( |
| 42 | `{serverName} - This server's name\n\n` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 43 | `These placeholders will be replaced with the server's name, etc..` |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 44 | ) |
| 45 | .setMaxLength(1) |
| 46 | .setRequired(false) |
| 47 | .setStyle(Discord.TextInputStyle.Paragraph) |
| 48 | ), |
| 49 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 50 | new TextInputBuilder() |
| 51 | .setCustomId("ex2") |
| 52 | .setLabel("Member Counts (2/3) - {MemberCount:...}") |
| 53 | .setPlaceholder( |
| 54 | `{:all} - Total member count\n` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 55 | `{:humans} - Total non-bot users\n` + |
| 56 | `{:bots} - Number of bots\n` |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 57 | ) |
| 58 | .setMaxLength(1) |
| 59 | .setRequired(false) |
| 60 | .setStyle(Discord.TextInputStyle.Paragraph) |
| 61 | ), |
| 62 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 63 | new TextInputBuilder() |
| 64 | .setCustomId("ex3") |
| 65 | .setLabel("Latest Member (3/3) - {member:...}") |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 66 | .setPlaceholder(`{:name} - The members name\n`) |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 67 | .setMaxLength(1) |
| 68 | .setRequired(false) |
| 69 | .setStyle(Discord.TextInputStyle.Paragraph) |
| 70 | ), |
| 71 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 72 | new TextInputBuilder() |
| 73 | .setCustomId("text") |
| 74 | .setLabel("Channel name input") |
| 75 | .setMaxLength(1000) |
| 76 | .setRequired(true) |
| 77 | .setStyle(Discord.TextInputStyle.Short) |
| 78 | .setValue(current.name) |
| 79 | ) |
| 80 | ) |
| 81 | ); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 82 | }; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 83 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 84 | type ObjectSchema = Record<string, { name: string; enabled: boolean }>; |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 85 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 86 | const addStatsChannel = async ( |
| 87 | interaction: CommandInteraction, |
| 88 | m: Message, |
| 89 | currentObject: ObjectSchema |
| 90 | ): Promise<ObjectSchema> => { |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 91 | let closed = false; |
| 92 | let cancelled = false; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 93 | const originalObject = Object.fromEntries(Object.entries(currentObject).map(([k, v]) => [k, { ...v }])); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 94 | let newChannel: string | undefined; |
| 95 | let newChannelName: string = "{memberCount:all}-members"; |
| 96 | let newChannelEnabled: boolean = true; |
| 97 | do { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 98 | m = await interaction.editReply({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 99 | embeds: [ |
| 100 | new EmojiEmbed() |
| 101 | .setTitle("Stats Channel") |
| 102 | .setDescription( |
| 103 | `New stats channel` + |
| 104 | (newChannel ? ` in <#${newChannel}>` : "") + |
| 105 | "\n\n" + |
| 106 | `**Name:** \`${newChannelName}\`\n` + |
| 107 | `**Preview:** ${await convertCurlyBracketString( |
| 108 | newChannelName, |
| 109 | interaction.user!.id, |
| 110 | interaction.user.username, |
| 111 | interaction.guild!.name, |
| 112 | interaction.guild!.members |
| 113 | )}\n` + |
| 114 | `**Enabled:** ${newChannelEnabled ? "Yes" : "No"}\n\n` |
| 115 | ) |
| 116 | .setEmoji("SETTINGS.STATS.GREEN") |
| 117 | .setStatus("Success") |
| 118 | ], |
| 119 | components: [ |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 120 | new ActionRowBuilder<ChannelSelectMenuBuilder>().addComponents( |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 121 | new ChannelSelectMenuBuilder().setCustomId("channel").setPlaceholder("Select a channel to use") |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 122 | ), |
| 123 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 124 | new ButtonBuilder() |
| 125 | .setLabel("Cancel") |
| 126 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 127 | .setStyle(ButtonStyle.Danger) |
| 128 | .setCustomId("back"), |
| 129 | new ButtonBuilder() |
| 130 | .setLabel("Save") |
| 131 | .setEmoji(getEmojiByName("ICONS.SAVE", "id")) |
| 132 | .setStyle(ButtonStyle.Success) |
| 133 | .setCustomId("save"), |
| 134 | new ButtonBuilder() |
| 135 | .setLabel("Edit name") |
| 136 | .setEmoji(getEmojiByName("ICONS.EDIT", "id")) |
| 137 | .setStyle(ButtonStyle.Primary) |
| 138 | .setCustomId("editName"), |
| 139 | new ButtonBuilder() |
| 140 | .setLabel(newChannelEnabled ? "Enabled" : "Disabled") |
| 141 | .setEmoji(getEmojiByName(newChannelEnabled ? "CONTROL.TICK" : "CONTROL.CROSS", "id")) |
| 142 | .setStyle(ButtonStyle.Secondary) |
| 143 | .setCustomId("toggleEnabled") |
| 144 | ) |
| 145 | ] |
| 146 | }); |
| 147 | let i: ButtonInteraction | ChannelSelectMenuInteraction; |
| 148 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 149 | i = (await m.awaitMessageComponent({ |
| 150 | time: 300000, |
| 151 | filter: (i) => { |
| 152 | return ( |
| 153 | i.user.id === interaction.user.id && |
| 154 | i.channel!.id === interaction.channel!.id && |
| 155 | i.message.id === m.id |
| 156 | ); |
| 157 | } |
| 158 | })) as ButtonInteraction | ChannelSelectMenuInteraction; |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 159 | } catch (e) { |
| 160 | closed = true; |
| 161 | cancelled = true; |
| 162 | break; |
| 163 | } |
| 164 | if (i.isButton()) { |
| 165 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 166 | case "back": { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 167 | await i.deferUpdate(); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 168 | closed = true; |
| 169 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 170 | } |
| 171 | case "save": { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 172 | await i.deferUpdate(); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 173 | if (newChannel) { |
| 174 | currentObject[newChannel] = { |
| 175 | name: newChannelName, |
| 176 | enabled: newChannelEnabled |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 177 | }; |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 178 | } |
| 179 | closed = true; |
| 180 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 181 | } |
| 182 | case "editName": { |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 183 | await interaction.editReply({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 184 | embeds: [ |
| 185 | new EmojiEmbed() |
| 186 | .setTitle("Stats Channel") |
| 187 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 188 | .setStatus("Success") |
| 189 | .setEmoji("SETTINGS.STATS.GREEN") |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 190 | ], |
| 191 | components: [ |
| 192 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 193 | new ButtonBuilder() |
| 194 | .setLabel("Back") |
| 195 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 196 | .setStyle(ButtonStyle.Primary) |
| 197 | .setCustomId("back") |
| 198 | ) |
| 199 | ] |
| 200 | }); |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 201 | await showModal(i, { name: newChannelName, enabled: newChannelEnabled }); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 202 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 203 | const out: Discord.ModalSubmitInteraction | ButtonInteraction | null = |
| 204 | await modalInteractionCollector(m, interaction.user); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 205 | if (!out) continue; |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 206 | if (out.isButton()) continue; |
| 207 | newChannelName = out.fields.getTextInputValue("text"); |
| 208 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 209 | } |
| 210 | case "toggleEnabled": { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 211 | await i.deferUpdate(); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 212 | newChannelEnabled = !newChannelEnabled; |
| 213 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 214 | } |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 215 | } |
| 216 | } else { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 217 | await i.deferUpdate(); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 218 | if (i.customId === "channel") { |
| 219 | newChannel = i.values[0]; |
| 220 | } |
| 221 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 222 | } while (!closed); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 223 | if (cancelled) return originalObject; |
| 224 | if (!(newChannel && newChannelName && newChannelEnabled)) return originalObject; |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 225 | return currentObject; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 226 | }; |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 227 | const callback = async (interaction: CommandInteraction) => { |
| 228 | if (!interaction.guild) return; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 229 | const { renderChannel } = client.logger; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 230 | const m: Message = await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true }); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 231 | let page = 0; |
| 232 | let closed = false; |
| 233 | const config = await client.database.guilds.read(interaction.guild.id); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 234 | let currentObject: ObjectSchema = config.stats; |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 235 | let modified = false; |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 236 | do { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 237 | const embed = new EmojiEmbed().setTitle("Stats Settings").setEmoji("SETTINGS.STATS.GREEN").setStatus("Success"); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 238 | const noStatsChannels = Object.keys(currentObject).length === 0; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 239 | let current: { enabled: boolean; name: string }; |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 240 | |
| 241 | const pageSelect = new StringSelectMenuBuilder() |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 242 | .setCustomId("page") |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 243 | .setPlaceholder("Select a stats channel to manage"); |
| 244 | const actionSelect = new StringSelectMenuBuilder() |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 245 | .setCustomId("action") |
| 246 | .setPlaceholder("Perform an action") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 247 | .addOptions( |
| 248 | new StringSelectMenuOptionBuilder() |
| 249 | .setLabel("Edit") |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 250 | .setDescription("Edit the stats channel") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 251 | .setValue("edit") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 252 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji), |
| 253 | new StringSelectMenuOptionBuilder() |
| 254 | .setLabel("Delete") |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 255 | .setDescription("Delete the stats channel") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 256 | .setValue("delete") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 257 | .setEmoji(getEmojiByName("TICKETS.ISSUE", "id") as APIMessageComponentEmoji) |
| 258 | ); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 259 | const buttonRow = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 260 | new ButtonBuilder() |
| 261 | .setCustomId("back") |
| 262 | .setStyle(ButtonStyle.Primary) |
| 263 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
| 264 | .setDisabled(page === 0), |
| 265 | new ButtonBuilder() |
| 266 | .setCustomId("next") |
| 267 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id") as APIMessageComponentEmoji) |
| 268 | .setStyle(ButtonStyle.Primary) |
| 269 | .setDisabled(page === Object.keys(currentObject).length - 1), |
| 270 | new ButtonBuilder() |
| 271 | .setCustomId("add") |
| 272 | .setLabel("Create new") |
| 273 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id") as APIMessageComponentEmoji) |
| 274 | .setStyle(ButtonStyle.Secondary) |
| 275 | .setDisabled(Object.keys(currentObject).length >= 24), |
| 276 | new ButtonBuilder() |
| 277 | .setCustomId("save") |
| 278 | .setLabel("Save") |
| 279 | .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji) |
| 280 | .setStyle(ButtonStyle.Success) |
| 281 | .setDisabled(modified) |
| 282 | ); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 283 | if (noStatsChannels) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 284 | embed.setDescription( |
| 285 | "No stats channels have been set up yet. Use the button below to add one.\n\n" + |
| 286 | createPageIndicator(1, 1, undefined, true) |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 287 | ); |
| 288 | pageSelect.setDisabled(true); |
| 289 | actionSelect.setDisabled(true); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 290 | pageSelect.addOptions(new StringSelectMenuOptionBuilder().setLabel("No stats channels").setValue("none")); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 291 | } else { |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 292 | page = Math.min(page, Object.keys(currentObject).length - 1); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 293 | current = currentObject[Object.keys(config.stats)[page]!]!; |
| 294 | actionSelect.addOptions( |
| 295 | new StringSelectMenuOptionBuilder() |
| 296 | .setLabel(current.enabled ? "Disable" : "Enable") |
| 297 | .setValue("toggleEnabled") |
| 298 | .setDescription( |
| 299 | `Currently ${current.enabled ? "Enabled" : "Disabled"}, click to ${ |
| 300 | current.enabled ? "disable" : "enable" |
| 301 | } this channel` |
| 302 | ) |
| 303 | .setEmoji( |
| 304 | getEmojiByName( |
| 305 | current.enabled ? "CONTROL.TICK" : "CONTROL.CROSS", |
| 306 | "id" |
| 307 | ) as APIMessageComponentEmoji |
| 308 | ) |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 309 | ); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 310 | embed.setDescription( |
| 311 | `**Currently Editing:** ${renderChannel(Object.keys(currentObject)[page]!)}\n\n` + |
| 312 | `${getEmojiByName(current.enabled ? "CONTROL.TICK" : "CONTROL.CROSS")} Currently ${ |
| 313 | current.enabled ? "Enabled" : "Disabled" |
| 314 | }\n` + |
| 315 | `**Name:** \`${current.name}\`\n` + |
| 316 | `**Preview:** ${await convertCurlyBracketString( |
| 317 | current.name, |
| 318 | interaction.user.id, |
| 319 | interaction.user.username, |
| 320 | interaction.guild.name, |
| 321 | interaction.guild.members |
| 322 | )}` + |
| 323 | "\n\n" + |
| 324 | createPageIndicator(Object.keys(config.stats).length, page) |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 325 | ); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 326 | for (const [id, { name, enabled }] of Object.entries(currentObject)) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 327 | pageSelect.addOptions( |
| 328 | new StringSelectMenuOptionBuilder() |
| 329 | .setLabel(`${name} (${renderChannel(id)})`) |
| 330 | .setEmoji( |
| 331 | getEmojiByName(enabled ? "CONTROL.TICK" : "CONTROL.CROSS", "id") as APIMessageComponentEmoji |
| 332 | ) |
| 333 | .setDescription(`${enabled ? "Enabled" : "Disabled"}`) |
| 334 | .setValue(id) |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 335 | ); |
| 336 | } |
| 337 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 338 | |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 339 | await interaction.editReply({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 340 | embeds: [embed], |
| 341 | components: [ |
| 342 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(pageSelect), |
| 343 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(actionSelect), |
| 344 | buttonRow |
| 345 | ] |
| 346 | }); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 347 | |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 348 | let i: StringSelectMenuInteraction | ButtonInteraction; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 349 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 350 | i = (await m.awaitMessageComponent({ |
| 351 | filter: (interaction) => interaction.user.id === interaction.user.id, |
| 352 | time: 60000 |
| 353 | })) as StringSelectMenuInteraction | ButtonInteraction; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 354 | } catch (e) { |
| 355 | closed = true; |
| 356 | continue; |
| 357 | } |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 358 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 359 | if (i.isStringSelectMenu()) { |
| 360 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 361 | case "page": { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 362 | await i.deferUpdate(); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 363 | page = Object.keys(currentObject).indexOf(i.values[0]!); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 364 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 365 | } |
| 366 | case "action": { |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 367 | modified = true; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 368 | switch (i.values[0]!) { |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 369 | case "edit": { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 370 | await showModal(i, current!); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 371 | await interaction.editReply({ |
| 372 | embeds: [ |
| 373 | new EmojiEmbed() |
| 374 | .setTitle("Stats Channel") |
| 375 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 376 | .setStatus("Success") |
| 377 | .setEmoji("SETTINGS.STATS.GREEN") |
| 378 | ], |
| 379 | components: [ |
| 380 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 381 | new ButtonBuilder() |
| 382 | .setLabel("Back") |
| 383 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 384 | .setStyle(ButtonStyle.Primary) |
| 385 | .setCustomId("back") |
| 386 | ) |
| 387 | ] |
| 388 | }); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 389 | let out: Discord.ModalSubmitInteraction | null; |
| 390 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 391 | out = (await modalInteractionCollector( |
| 392 | m, |
| 393 | interaction.user |
| 394 | )) as Discord.ModalSubmitInteraction | null; |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 395 | } catch (e) { |
| 396 | continue; |
| 397 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 398 | if (!out) continue; |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 399 | if (out.isButton()) continue; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 400 | currentObject[Object.keys(currentObject)[page]!]!.name = |
| 401 | out.fields.getTextInputValue("text"); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 402 | break; |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 403 | } |
| 404 | case "toggleEnabled": { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 405 | await i.deferUpdate(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 406 | currentObject[Object.keys(currentObject)[page]!]!.enabled = |
| 407 | !currentObject[Object.keys(currentObject)[page]!]!.enabled; |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 408 | modified = true; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 409 | break; |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 410 | } |
| 411 | case "delete": { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 412 | await i.deferUpdate(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 413 | currentObject = Object.fromEntries( |
| 414 | Object.entries(currentObject).filter(([k]) => k !== Object.keys(currentObject)[page]!) |
| 415 | ); |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 416 | page = Math.min(page, Object.keys(currentObject).length - 1); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 417 | modified = true; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 418 | break; |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 419 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 420 | } |
| 421 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 422 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 423 | } |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 424 | } else { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 425 | await i.deferUpdate(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 426 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 427 | case "back": { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 428 | page--; |
| 429 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 430 | } |
| 431 | case "next": { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 432 | page++; |
| 433 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 434 | } |
| 435 | case "add": { |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 436 | currentObject = await addStatsChannel(interaction, m, currentObject); |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 437 | page = Object.keys(currentObject).length - 1; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 438 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 439 | } |
| 440 | case "save": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 441 | await client.database.guilds.write(interaction.guild.id, { stats: currentObject }); |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 442 | await singleNotify("statsChannelDeleted", interaction.guild.id, true); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 443 | modified = false; |
Skyler Grey | 16ecb17 | 2023-03-05 07:30:32 +0000 | [diff] [blame] | 444 | await client.memory.forceUpdate(interaction.guild.id); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame] | 445 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 446 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 447 | } |
| 448 | } |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 449 | } while (!closed); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 450 | await interaction.deleteReply(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 451 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 452 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 453 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 454 | const member = interaction.member as Discord.GuildMember; |
TheCodedProf | afca98b | 2023-01-17 22:25:43 -0500 | [diff] [blame] | 455 | if (!member.permissions.has("ManageChannels")) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 456 | return "You must have the *Manage Channels* permission to use this command"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 457 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 458 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 459 | |
| 460 | export { command }; |
| 461 | export { callback }; |
Skyler Grey | 16ecb17 | 2023-03-05 07:30:32 +0000 | [diff] [blame] | 462 | export { check }; |