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