PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../utils/defaults.js"; |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 2 | import Discord, { CommandInteraction, Message, ActionRowBuilder, StringSelectMenuBuilder, ButtonBuilder, ButtonStyle, StringSelectMenuOptionBuilder, APIMessageComponentEmoji, TextInputBuilder, StringSelectMenuInteraction, ButtonInteraction } 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"; |
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 | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 19 | const showModal = async (interaction: StringSelectMenuInteraction, current: { enabled: boolean; name: string; }) => { |
| 20 | await interaction.showModal( |
| 21 | new Discord.ModalBuilder() |
| 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 | |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 74 | const callback = async (interaction: CommandInteraction) => { |
| 75 | if (!interaction.guild) return; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 76 | const { renderChannel } = client.logger; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 77 | const m: Message = await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true }); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 78 | let page = 0; |
| 79 | let closed = false; |
| 80 | const config = await client.database.guilds.read(interaction.guild.id); |
| 81 | const currentObject = config.stats; |
| 82 | let modified = false; |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 83 | do { |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 84 | let embed = new EmojiEmbed() |
| 85 | .setTitle("Stats Settings") |
| 86 | .setEmoji("SETTINGS.STATS.GREEN") |
| 87 | .setStatus("Success"); |
| 88 | const noStatsChannels = Object.keys(currentObject).length === 0; |
| 89 | let current: { enabled: boolean; name: string; }; |
| 90 | |
| 91 | const pageSelect = new StringSelectMenuBuilder() |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 92 | .setCustomId("page") |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 93 | .setPlaceholder("Select a stats channel to manage"); |
| 94 | const actionSelect = new StringSelectMenuBuilder() |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 95 | .setCustomId("action") |
| 96 | .setPlaceholder("Perform an action") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 97 | .addOptions( |
| 98 | new StringSelectMenuOptionBuilder() |
| 99 | .setLabel("Edit") |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 100 | .setDescription("Edit the stats channel") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 101 | .setValue("edit") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 102 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji), |
| 103 | new StringSelectMenuOptionBuilder() |
| 104 | .setLabel("Delete") |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 105 | .setDescription("Delete the stats channel") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 106 | .setValue("delete") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 107 | .setEmoji(getEmojiByName("TICKETS.ISSUE", "id") as APIMessageComponentEmoji) |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 108 | ); |
| 109 | const buttonRow = new ActionRowBuilder<ButtonBuilder>() |
| 110 | .addComponents( |
| 111 | new ButtonBuilder() |
| 112 | .setCustomId("back") |
| 113 | .setStyle(ButtonStyle.Primary) |
| 114 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
| 115 | .setDisabled(page === 0), |
| 116 | new ButtonBuilder() |
| 117 | .setCustomId("next") |
| 118 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id") as APIMessageComponentEmoji) |
| 119 | .setStyle(ButtonStyle.Primary) |
| 120 | .setDisabled(page === Object.keys(currentObject).length - 1), |
| 121 | new ButtonBuilder() |
| 122 | .setCustomId("add") |
| 123 | .setLabel("Create new") |
| 124 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id") as APIMessageComponentEmoji) |
| 125 | .setStyle(ButtonStyle.Secondary) |
| 126 | .setDisabled(Object.keys(currentObject).length >= 24), |
| 127 | new ButtonBuilder() |
| 128 | .setCustomId("save") |
| 129 | .setLabel("Save") |
| 130 | .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji) |
| 131 | .setStyle(ButtonStyle.Success) |
| 132 | .setDisabled(modified), |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 133 | ); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 134 | if (noStatsChannels) { |
| 135 | embed.setDescription("No stats channels have been set up yet. Use the button below to add one.\n\n" + |
| 136 | createPageIndicator(1, 1, undefined, true) |
| 137 | ); |
| 138 | pageSelect.setDisabled(true); |
| 139 | actionSelect.setDisabled(true); |
| 140 | pageSelect.addOptions(new StringSelectMenuOptionBuilder() |
| 141 | .setLabel("No stats channels") |
| 142 | .setValue("none") |
| 143 | ); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 144 | } else { |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 145 | page = Math.min(page, Object.keys(currentObject).length - 1); |
| 146 | current = currentObject[Object.keys(config.stats)[page]!]! |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 147 | actionSelect.addOptions(new StringSelectMenuOptionBuilder() |
| 148 | .setLabel(current.enabled ? "Disable" : "Enable") |
| 149 | .setValue("toggleEnabled") |
| 150 | .setDescription(`Currently ${current.enabled ? "Enabled" : "Disabled"}, click to ${current.enabled ? "disable" : "enable"} this channel`) |
| 151 | .setEmoji(getEmojiByName(current.enabled ? "CONTROL.TICK" : "CONTROL.CROSS", "id") as APIMessageComponentEmoji) |
| 152 | ); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 153 | |
| 154 | embed.setDescription(`**Currently Editing:** ${renderChannel(Object.keys(currentObject)[page]!)}\n\n` + |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 155 | `${getEmojiByName(current.enabled ? "CONTROL.TICK" : "CONTROL.CROSS")} Currently ${current.enabled ? "Enabled" : "Disabled"}\n` + |
| 156 | `**Name:** \`${current.name}\`\n` + |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 157 | `**Preview:** ${await convertCurlyBracketString(current.name, interaction.user.id, interaction.user.username, interaction.guild.name, interaction.guild.members)}` + '\n\n' + |
| 158 | createPageIndicator(Object.keys(config.stats).length, page) |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 159 | ); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 160 | for (const [id, { name, enabled }] of Object.entries(currentObject)) { |
| 161 | pageSelect.addOptions(new StringSelectMenuOptionBuilder() |
| 162 | .setLabel(`${name} (${renderChannel(id)})`) |
| 163 | .setEmoji(getEmojiByName(enabled ? "CONTROL.TICK" : "CONTROL.CROSS", "id") as APIMessageComponentEmoji) |
| 164 | .setDescription(`${enabled ? "Enabled" : "Disabled"}`) |
| 165 | .setValue(id) |
| 166 | ); |
| 167 | } |
| 168 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 169 | |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 170 | interaction.editReply({embeds: [embed], components: [ |
| 171 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(pageSelect), |
| 172 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(actionSelect), |
| 173 | buttonRow |
| 174 | ]}); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 175 | |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 176 | let i: StringSelectMenuInteraction | ButtonInteraction; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 177 | try { |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 178 | 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] | 179 | } catch (e) { |
| 180 | closed = true; |
| 181 | continue; |
| 182 | } |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 183 | |
| 184 | if(i.isStringSelectMenu()) { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 185 | switch(i.customId) { |
| 186 | case "page": |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 187 | page = Object.keys(currentObject).indexOf(i.values[0]!); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 188 | break; |
| 189 | case "action": |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 190 | modified = true; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 191 | switch(i.values[0]!) { |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 192 | case "edit": { |
| 193 | showModal(i, current!) |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 194 | await interaction.editReply({ |
| 195 | embeds: [ |
| 196 | new EmojiEmbed() |
| 197 | .setTitle("Stats Channel") |
| 198 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 199 | .setStatus("Success") |
| 200 | .setEmoji("SETTINGS.STATS.GREEN") |
| 201 | ], |
| 202 | components: [ |
| 203 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 204 | new ButtonBuilder() |
| 205 | .setLabel("Back") |
| 206 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 207 | .setStyle(ButtonStyle.Primary) |
| 208 | .setCustomId("back") |
| 209 | ) |
| 210 | ] |
| 211 | }); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 212 | break; |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 213 | } |
| 214 | case "toggleEnabled": { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 215 | i.deferUpdate(); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 216 | currentObject[Object.keys(currentObject)[page]!]!.enabled = !currentObject[Object.keys(currentObject)[page]!]!.enabled; |
| 217 | modified = true; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 218 | break; |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 219 | } |
| 220 | case "delete": { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 221 | i.deferUpdate(); |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 222 | delete currentObject[Object.keys(currentObject)[page]!]; |
| 223 | modified = true; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 224 | break; |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 225 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 226 | } |
| 227 | break; |
| 228 | } |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 229 | } else { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 230 | i.deferUpdate(); |
| 231 | switch(i.customId) { |
| 232 | case "back": |
| 233 | page--; |
| 234 | break; |
| 235 | case "next": |
| 236 | page++; |
| 237 | break; |
| 238 | case "add": |
| 239 | break; |
| 240 | case "save": |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 241 | client.database.guilds.write(interaction.guild.id, {stats: currentObject}); |
| 242 | singleNotify("statsChannelDeleted", interaction.guild.id, true); |
| 243 | modified = false; |
| 244 | break; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 245 | } |
| 246 | } |
TheCodedProf | c2acbcc | 2023-01-20 17:23:51 -0500 | [diff] [blame^] | 247 | |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 248 | } while (!closed); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 249 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 250 | |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 251 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 252 | const member = interaction.member as Discord.GuildMember; |
TheCodedProf | afca98b | 2023-01-17 22:25:43 -0500 | [diff] [blame] | 253 | if (!member.permissions.has("ManageChannels")) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 254 | return "You must have the *Manage Channels* permission to use this command"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 255 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 256 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 257 | |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 258 | |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 259 | export { command }; |
| 260 | export { callback }; |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 261 | export { check }; |