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