pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 2 | import Discord, { |
| 3 | CommandInteraction, |
| 4 | Message, |
| 5 | MessageActionRow, |
| 6 | MessageSelectMenu |
| 7 | } from "discord.js"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 8 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 9 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 10 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 11 | import { WrappedCheck } from "jshaiku"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 12 | import client from "../../utils/client.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | import convertCurlyBracketString from "../../utils/convertCurlyBracketString.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 14 | import { callback as statsChannelAddCallback } from "../../reflex/statsChannelUpdate.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | import singleNotify from "../../utils/singleNotify.js"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 16 | |
| 17 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 18 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | .setName("stats") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 20 | .setDescription( |
| 21 | "Controls channels which update when someone joins or leaves the server" |
| 22 | ) |
| 23 | .addChannelOption((option) => |
| 24 | option.setName("channel").setDescription("The channel to modify") |
| 25 | ) |
| 26 | .addStringOption((option) => |
| 27 | option |
| 28 | .setName("name") |
| 29 | .setDescription( |
| 30 | "The new channel name | Enter any text or use the extra variables like {memberCount}" |
| 31 | ) |
| 32 | .setAutocomplete(true) |
| 33 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 34 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 35 | const callback = async ( |
| 36 | interaction: CommandInteraction |
| 37 | ): Promise<void | unknown> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | singleNotify("statsChannelDeleted", interaction.guild.id, true); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 39 | const m = (await interaction.reply({ |
| 40 | embeds: LoadingEmbed, |
| 41 | ephemeral: true, |
| 42 | fetchReply: true |
| 43 | })) as Message; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 44 | let config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 45 | if (interaction.options.getString("name")) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 46 | let channel; |
| 47 | if (Object.keys(config.getKey("stats")).length >= 25) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 48 | return await interaction.editReply({ |
| 49 | embeds: [ |
| 50 | new EmojiEmbed() |
| 51 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 52 | .setTitle("Stats Channel") |
| 53 | .setDescription( |
| 54 | "You can only have 25 stats channels in a server" |
| 55 | ) |
| 56 | .setStatus("Danger") |
| 57 | ] |
| 58 | }); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 59 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 60 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 61 | channel = interaction.options.getChannel("channel"); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 62 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 63 | return await interaction.editReply({ |
| 64 | embeds: [ |
| 65 | new EmojiEmbed() |
| 66 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 67 | .setTitle("Stats Channel") |
| 68 | .setDescription( |
| 69 | "The channel you provided is not a valid channel" |
| 70 | ) |
| 71 | .setStatus("Danger") |
| 72 | ] |
| 73 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 74 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 75 | channel = channel as Discord.TextChannel; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 76 | if (channel.guild.id !== interaction.guild.id) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 77 | return interaction.editReply({ |
| 78 | embeds: [ |
| 79 | new EmojiEmbed() |
| 80 | .setTitle("Stats Channel") |
| 81 | .setDescription( |
| 82 | "You must choose a channel in this server" |
| 83 | ) |
| 84 | .setStatus("Danger") |
| 85 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 86 | ] |
| 87 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 88 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 89 | let newName = await convertCurlyBracketString( |
| 90 | interaction.options.getString("name"), |
| 91 | null, |
| 92 | null, |
| 93 | interaction.guild.name, |
| 94 | interaction.guild.members |
| 95 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 96 | if (interaction.options.getChannel("channel").type === "GUILD_TEXT") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 97 | newName = newName.toLowerCase().replace(/[\s]/g, "-"); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 98 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 99 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 100 | .setEmoji("CHANNEL.TEXT.EDIT") |
| 101 | .setTitle("Stats Channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 102 | .setDescription( |
| 103 | `Are you sure you want to set <#${ |
| 104 | channel.id |
| 105 | }> to a stats channel?\n\n*Preview: ${newName.replace( |
| 106 | /^ +| $/g, |
| 107 | "" |
| 108 | )}*` |
| 109 | ) |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 110 | .setColor("Warning") |
| 111 | .setInverted(true) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 112 | .send(true); |
| 113 | if (confirmation.cancelled) return; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 114 | if (confirmation.success) { |
| 115 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 116 | const name = interaction.options.getString("name"); |
| 117 | const channel = interaction.options.getChannel("channel"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 118 | await client.database.guilds.write(interaction.guild.id, { |
| 119 | [`stats.${channel.id}`]: { name: name, enabled: true } |
| 120 | }); |
| 121 | const { log, NucleusColors, entry, renderUser, renderChannel } = |
| 122 | client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 123 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 124 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 125 | type: "statsChannelUpdate", |
| 126 | displayName: "Stats Channel Updated", |
| 127 | calculateType: "nucleusSettingsUpdated", |
| 128 | color: NucleusColors.yellow, |
| 129 | emoji: "CHANNEL.TEXT.EDIT", |
| 130 | timestamp: new Date().getTime() |
| 131 | }, |
| 132 | list: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 133 | memberId: entry( |
| 134 | interaction.user.id, |
| 135 | `\`${interaction.user.id}\`` |
| 136 | ), |
| 137 | changedBy: entry( |
| 138 | interaction.user.id, |
| 139 | renderUser(interaction.user) |
| 140 | ), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 141 | channel: entry(channel.id, renderChannel(channel)), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 142 | name: entry( |
| 143 | interaction.options.getString("name"), |
| 144 | `\`${interaction.options.getString("name")}\`` |
| 145 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 146 | }, |
| 147 | hidden: { |
| 148 | guild: interaction.guild.id |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 149 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 150 | }; |
| 151 | log(data); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 152 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 153 | console.log(e); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 154 | return interaction.editReply({ |
| 155 | embeds: [ |
| 156 | new EmojiEmbed() |
| 157 | .setTitle("Stats Channel") |
| 158 | .setDescription( |
| 159 | "Something went wrong and the stats channel could not be set" |
| 160 | ) |
| 161 | .setStatus("Danger") |
| 162 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 163 | ], |
| 164 | components: [] |
| 165 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 166 | } |
| 167 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 168 | return interaction.editReply({ |
| 169 | embeds: [ |
| 170 | new EmojiEmbed() |
| 171 | .setTitle("Stats Channel") |
| 172 | .setDescription("No changes were made") |
| 173 | .setStatus("Success") |
| 174 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 175 | ], |
| 176 | components: [] |
| 177 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 178 | } |
| 179 | await statsChannelAddCallback(client, interaction.member); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 180 | } |
| 181 | while (true) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 182 | config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 183 | const stats = config.getKey("stats"); |
| 184 | const selectMenu = new MessageSelectMenu() |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 185 | .setCustomId("remove") |
| 186 | .setMinValues(1) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 187 | .setMaxValues(Math.max(1, Object.keys(stats).length)); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 188 | await interaction.editReply({ |
| 189 | embeds: [ |
| 190 | new EmojiEmbed() |
| 191 | .setTitle("Stats Channel") |
| 192 | .setDescription( |
| 193 | "The following channels update when someone joins or leaves the server. You can select a channel to remove it from the list." |
| 194 | ) |
| 195 | .setStatus("Success") |
| 196 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 197 | ], |
| 198 | components: [ |
| 199 | new MessageActionRow().addComponents( |
| 200 | Object.keys(stats).length |
| 201 | ? [ |
| 202 | selectMenu |
| 203 | .setPlaceholder( |
| 204 | "Select a stats channel to remove, stopping it updating" |
| 205 | ) |
| 206 | .addOptions( |
| 207 | Object.keys(stats).map((key) => ({ |
| 208 | label: interaction.guild.channels.cache.get( |
| 209 | key |
| 210 | ).name, |
| 211 | value: key, |
| 212 | description: `${stats[key].name}` |
| 213 | })) |
| 214 | ) |
| 215 | ] |
| 216 | : [ |
| 217 | selectMenu |
| 218 | .setPlaceholder( |
| 219 | "The server has no stats channels" |
| 220 | ) |
| 221 | .setDisabled(true) |
| 222 | .setOptions([ |
| 223 | { |
| 224 | label: "*Placeholder*", |
| 225 | value: "placeholder", |
| 226 | description: "No stats channels" |
| 227 | } |
| 228 | ]) |
| 229 | ] |
| 230 | ) |
| 231 | ] |
| 232 | }); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 233 | let i; |
| 234 | try { |
| 235 | i = await m.awaitMessageComponent({ time: 300000 }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 236 | } catch (e) { |
| 237 | break; |
| 238 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 239 | i.deferUpdate(); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 240 | if (i.customId === "remove") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 241 | const toRemove = i.values; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 242 | await client.database.guilds.write( |
| 243 | interaction.guild.id, |
| 244 | null, |
| 245 | toRemove.map((k) => `stats.${k}`) |
| 246 | ); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 247 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 248 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 249 | await interaction.editReply({ |
| 250 | embeds: [m.embeds[0]!.setFooter({ text: "Message closed" })], |
| 251 | components: [] |
| 252 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 253 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 254 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 255 | const check = ( |
| 256 | interaction: CommandInteraction, |
| 257 | _defaultCheck: WrappedCheck |
| 258 | ) => { |
| 259 | const member = interaction.member as Discord.GuildMember; |
| 260 | if (!member.permissions.has("MANAGE_CHANNELS")) |
| 261 | throw "You must have the *Manage Channels* permission to use this command"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 262 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 263 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 264 | |
| 265 | export { command }; |
| 266 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 267 | export { check }; |