PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../utils/defaults.js"; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 2 | import Discord, { CommandInteraction, Message, ActionRowBuilder, StringSelectMenuBuilder, ButtonBuilder, ButtonStyle, StringSelectMenuOptionBuilder, APIMessageComponentEmoji, MessageComponentInteraction, TextInputBuilder } from "discord.js"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 3 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
TheCodedProf | afca98b | 2023-01-17 22:25:43 -0500 | [diff] [blame] | 4 | import type { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
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"; |
| 11 | import type { GuildConfig } from "../../utils/database.js"; |
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 | type ChangesType = Record<string, { name?: string; enabled?: boolean; }> |
| 19 | |
| 20 | const applyChanges = (baseObject: GuildConfig['stats'], changes: ChangesType): GuildConfig['stats'] => { |
| 21 | for (const [id, { name, enabled }] of Object.entries(changes)) { |
| 22 | if (!baseObject[id]) baseObject[id] = { name: "", enabled: false}; |
| 23 | if (name) baseObject[id]!.name = name; |
| 24 | if (enabled) baseObject[id]!.enabled = enabled; |
| 25 | } |
| 26 | return baseObject; |
| 27 | } |
| 28 | |
| 29 | |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 30 | const callback = async (interaction: CommandInteraction) => { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 31 | try{ |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 32 | if (!interaction.guild) return; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 33 | const { renderChannel } = client.logger; |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 34 | let closed = false; |
| 35 | let page = 0; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 36 | const m: Message = await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true }); |
| 37 | let changes: ChangesType = {}; |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 38 | do { |
| 39 | const config = await client.database.guilds.read(interaction.guild.id); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 40 | const stats = config.stats; |
| 41 | let currentID = ""; |
| 42 | let current: { |
| 43 | name: string; |
| 44 | enabled: boolean; |
| 45 | } = { |
| 46 | name: "", |
| 47 | enabled: false |
| 48 | }; |
| 49 | let description = ""; |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 50 | let pageSelect = new StringSelectMenuBuilder() |
| 51 | .setCustomId("page") |
| 52 | .setPlaceholder("Select a stats channel to manage") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 53 | .setDisabled(Object.keys(stats).length === 0) |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 54 | .setMinValues(1) |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 55 | .setMaxValues(1); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 56 | let actionSelect = new StringSelectMenuBuilder() |
| 57 | .setCustomId("action") |
| 58 | .setPlaceholder("Perform an action") |
| 59 | .setMinValues(1) |
| 60 | .setMaxValues(1) |
| 61 | .setDisabled(Object.keys(stats).length === 0) |
| 62 | .addOptions( |
| 63 | new StringSelectMenuOptionBuilder() |
| 64 | .setLabel("Edit") |
| 65 | .setValue("edit") |
| 66 | .setDescription("Edit the name of this stats channel") |
| 67 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji), |
| 68 | new StringSelectMenuOptionBuilder() |
| 69 | .setLabel("Delete") |
| 70 | .setValue("delete") |
| 71 | .setDescription("Delete this stats channel") |
| 72 | .setEmoji(getEmojiByName("TICKETS.ISSUE", "id") as APIMessageComponentEmoji) |
| 73 | ); |
| 74 | if (Object.keys(stats).length === 0) { |
| 75 | description = "You do not have any stats channels set up yet" |
| 76 | pageSelect.addOptions(new StringSelectMenuOptionBuilder().setLabel("No stats channels").setValue("none")) |
| 77 | } else { |
| 78 | currentID = Object.keys(stats)[page]! |
| 79 | current = stats[currentID]!; |
| 80 | current = applyChanges({ [currentID]: current }, changes)[currentID]!; |
| 81 | // Propogate pageSelect with list of stats channels |
| 82 | for (const [id, { name, enabled }] of Object.entries(stats)) { |
| 83 | pageSelect.addOptions( |
| 84 | new StringSelectMenuOptionBuilder() |
| 85 | .setLabel(name) |
| 86 | .setValue(id) |
| 87 | .setDescription(`Enabled: ${enabled}`) |
| 88 | ); |
| 89 | } |
| 90 | actionSelect.addOptions(new StringSelectMenuOptionBuilder() |
| 91 | .setLabel(current.enabled ? "Disable" : "Enable") |
| 92 | .setValue("toggleEnabled") |
| 93 | .setDescription(`Currently ${current.enabled ? "Enabled" : "Disabled"}, click to ${current.enabled ? "disable" : "enable"} this channel`) |
| 94 | .setEmoji(getEmojiByName(current.enabled ? "CONTROL.TICK" : "CONTROL.CROSS", "id") as APIMessageComponentEmoji) |
| 95 | ); |
| 96 | description = `**Currently Editing:** ${renderChannel(currentID)}\n\n` + |
| 97 | `${getEmojiByName(current.enabled ? "CONTROL.TICK" : "CONTROL.CROSS")} Currently ${current.enabled ? "Enabled" : "Disabled"}\n` + |
| 98 | `**Name:** \`${current.name}\`\n` + |
| 99 | `**Preview:** ${await convertCurlyBracketString(current.name, interaction.user.id, interaction.user.username, interaction.guild.name, interaction.guild.members)}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 100 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 101 | const row = new ActionRowBuilder<ButtonBuilder>() |
| 102 | .addComponents( |
| 103 | new ButtonBuilder() |
| 104 | .setCustomId("back") |
| 105 | .setStyle(ButtonStyle.Primary) |
| 106 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
| 107 | .setDisabled(page === 0), |
| 108 | new ButtonBuilder() |
| 109 | .setCustomId("next") |
| 110 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id") as APIMessageComponentEmoji) |
| 111 | .setStyle(ButtonStyle.Primary) |
| 112 | .setDisabled(page === Object.keys(stats).length - 1), |
| 113 | new ButtonBuilder() |
| 114 | .setCustomId("add") |
| 115 | .setLabel("Create new") |
| 116 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id") as APIMessageComponentEmoji) |
| 117 | .setStyle(ButtonStyle.Secondary) |
| 118 | .setDisabled(Object.keys(stats).length >= 24), |
| 119 | new ButtonBuilder() |
| 120 | .setCustomId("save") |
| 121 | .setLabel("Save") |
| 122 | .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji) |
| 123 | .setStyle(ButtonStyle.Success) |
| 124 | .setDisabled(Object.keys(changes).length === 0), |
| 125 | ); |
| 126 | |
| 127 | let embed = new EmojiEmbed() |
| 128 | .setTitle("Stats Channels") |
| 129 | .setDescription(description + "\n\n" + createPageIndicator(Object.keys(stats).length, page)) |
| 130 | .setEmoji("SETTINGS.STATS.GREEN") |
| 131 | .setStatus("Success") |
| 132 | |
| 133 | interaction.editReply({ |
| 134 | embeds: [embed], |
| 135 | components: [ |
| 136 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(pageSelect), |
| 137 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(actionSelect), |
| 138 | row |
| 139 | ] |
| 140 | }); |
| 141 | let i: MessageComponentInteraction; |
| 142 | try { |
| 143 | i = await m.awaitMessageComponent({ filter: (i) => i.user.id === interaction.user.id, time: 30000 }); |
| 144 | } catch (e) { |
| 145 | closed = true; |
| 146 | continue; |
| 147 | } |
| 148 | if (i.isStringSelectMenu()) { |
| 149 | switch(i.customId) { |
| 150 | case "page": |
| 151 | page = Object.keys(stats).indexOf(i.values[0]!); |
| 152 | i.deferUpdate(); |
| 153 | break; |
| 154 | case "action": |
| 155 | if(!changes[currentID]) changes[currentID] = {}; |
| 156 | switch(i.values[0]!) { |
| 157 | case "edit": |
| 158 | await i.showModal( |
| 159 | new Discord.ModalBuilder() |
| 160 | .setCustomId("modal") |
| 161 | .setTitle(`Stats channel name`) |
| 162 | .addComponents( |
| 163 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 164 | new TextInputBuilder() |
| 165 | .setCustomId("ex1") |
| 166 | .setLabel("Server Info (1/3)") |
| 167 | .setPlaceholder( |
| 168 | `{serverName} - This server's name\n\n` + |
| 169 | `These placeholders will be replaced with the server's name, etc..` |
| 170 | ) |
| 171 | .setMaxLength(1) |
| 172 | .setRequired(false) |
| 173 | .setStyle(Discord.TextInputStyle.Paragraph) |
| 174 | ), |
| 175 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 176 | new TextInputBuilder() |
| 177 | .setCustomId("ex2") |
| 178 | .setLabel("Member Counts (2/3) - {MemberCount:...}") |
| 179 | .setPlaceholder( |
| 180 | `{:all} - Total member count\n` + |
| 181 | `{:humans} - Total non-bot users\n` + |
| 182 | `{:bots} - Number of bots\n` |
| 183 | ) |
| 184 | .setMaxLength(1) |
| 185 | .setRequired(false) |
| 186 | .setStyle(Discord.TextInputStyle.Paragraph) |
| 187 | ), |
| 188 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 189 | new TextInputBuilder() |
| 190 | .setCustomId("ex3") |
| 191 | .setLabel("Latest Member (3/3) - {member:...}") |
| 192 | .setPlaceholder( |
| 193 | `{:name} - The members name\n` |
| 194 | ) |
| 195 | .setMaxLength(1) |
| 196 | .setRequired(false) |
| 197 | .setStyle(Discord.TextInputStyle.Paragraph) |
| 198 | ), |
| 199 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 200 | new TextInputBuilder() |
| 201 | .setCustomId("text") |
| 202 | .setLabel("Channel name input") |
| 203 | .setMaxLength(1000) |
| 204 | .setRequired(true) |
| 205 | .setStyle(Discord.TextInputStyle.Short) |
| 206 | .setValue(current.name) |
| 207 | ) |
| 208 | ) |
| 209 | ); |
| 210 | await interaction.editReply({ |
| 211 | embeds: [ |
| 212 | new EmojiEmbed() |
| 213 | .setTitle("Stats Channel") |
| 214 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 215 | .setStatus("Success") |
| 216 | .setEmoji("SETTINGS.STATS.GREEN") |
| 217 | ], |
| 218 | components: [ |
| 219 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 220 | new ButtonBuilder() |
| 221 | .setLabel("Back") |
| 222 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 223 | .setStyle(ButtonStyle.Primary) |
| 224 | .setCustomId("back") |
| 225 | ) |
| 226 | ] |
| 227 | }); |
| 228 | let out: Discord.ModalSubmitInteraction | null; |
| 229 | try { |
| 230 | out = await modalInteractionCollector( |
| 231 | m, |
| 232 | (m) => m.channel!.id === interaction.channel!.id, |
| 233 | (_) => true |
| 234 | ) as Discord.ModalSubmitInteraction | null; |
| 235 | } catch (e) { |
| 236 | continue; |
| 237 | } |
| 238 | if (!out) continue |
| 239 | if (!out.fields) continue |
| 240 | if (out.isButton()) continue; |
| 241 | const newString = out.fields.getTextInputValue("text"); |
| 242 | if (!newString) continue; |
| 243 | changes[currentID]!.name = newString; |
| 244 | break; |
| 245 | case "delete": |
| 246 | changes[currentID] = {}; |
| 247 | i.deferUpdate(); |
| 248 | break; |
| 249 | case "toggleEnabled": |
| 250 | changes[currentID]!.enabled = !stats[currentID]!.enabled; |
| 251 | i.deferUpdate(); |
| 252 | break; |
| 253 | } |
| 254 | break; |
| 255 | } |
| 256 | } else if (i.isButton()) { |
| 257 | i.deferUpdate(); |
| 258 | switch(i.customId) { |
| 259 | case "back": |
| 260 | page--; |
| 261 | break; |
| 262 | case "next": |
| 263 | page++; |
| 264 | break; |
| 265 | case "add": |
| 266 | break; |
| 267 | case "save": |
| 268 | let changed = applyChanges(config.stats, changes); |
| 269 | singleNotify("statsChannelDeleted", interaction.guild.id, true) |
| 270 | config.stats = changed; |
| 271 | changes = {} |
| 272 | await client.database.guilds.write(interaction.guildId!, config); |
| 273 | } |
| 274 | } |
| 275 | console.log(changes, config.stats); |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 276 | } while (!closed); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 277 | } catch(e) { |
| 278 | console.log(e) |
| 279 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 280 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 281 | |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 282 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 283 | const member = interaction.member as Discord.GuildMember; |
TheCodedProf | afca98b | 2023-01-17 22:25:43 -0500 | [diff] [blame] | 284 | if (!member.permissions.has("ManageChannels")) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 285 | return "You must have the *Manage Channels* permission to use this command"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 286 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 287 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 288 | |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 289 | |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 290 | export { command }; |
| 291 | export { callback }; |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 292 | export { check }; |