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