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