pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 2 | import Discord, { CommandInteraction, Message, ActionRowBuilder, SelectMenuBuilder } 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"; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 5 | import { 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> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [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; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 30 | let config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 31 | if (interaction.options.getString("name")) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 32 | let channel; |
| 33 | if (Object.keys(config.getKey("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 { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 45 | channel = interaction.options.getChannel("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; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [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( |
| 70 | interaction.options.getString("name"), |
| 71 | null, |
| 72 | null, |
| 73 | interaction.guild.name, |
| 74 | interaction.guild.members |
| 75 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 76 | if (interaction.options.getChannel("channel").type === "GUILD_TEXT") { |
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) |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 80 | .setEmoji("CHANNEL.TEXT.EDIT", "CHANNEL.TEXT.DELETE") |
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) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 90 | .send(true); |
| 91 | if (confirmation.cancelled) return; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 92 | if (confirmation.success) { |
| 93 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 94 | const name = interaction.options.getString("name"); |
| 95 | const channel = interaction.options.getChannel("channel"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 96 | await client.database.guilds.write(interaction.guild.id, { |
| 97 | [`stats.${channel.id}`]: { name: name, enabled: true } |
| 98 | }); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 99 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 100 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 101 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | type: "statsChannelUpdate", |
| 103 | displayName: "Stats Channel Updated", |
| 104 | calculateType: "nucleusSettingsUpdated", |
| 105 | color: NucleusColors.yellow, |
| 106 | emoji: "CHANNEL.TEXT.EDIT", |
| 107 | timestamp: new Date().getTime() |
| 108 | }, |
| 109 | list: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 110 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 111 | changedBy: entry(interaction.user.id, renderUser(interaction.user)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 112 | channel: entry(channel.id, renderChannel(channel)), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 113 | name: entry( |
| 114 | interaction.options.getString("name"), |
| 115 | `\`${interaction.options.getString("name")}\`` |
| 116 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 117 | }, |
| 118 | hidden: { |
| 119 | guild: interaction.guild.id |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 120 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 121 | }; |
| 122 | log(data); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 123 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 124 | console.log(e); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 125 | return interaction.editReply({ |
| 126 | embeds: [ |
| 127 | new EmojiEmbed() |
| 128 | .setTitle("Stats Channel") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 129 | .setDescription("Something went wrong and the stats channel could not be set") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 130 | .setStatus("Danger") |
| 131 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 132 | ], |
| 133 | components: [] |
| 134 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 135 | } |
| 136 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 137 | return interaction.editReply({ |
| 138 | embeds: [ |
| 139 | new EmojiEmbed() |
| 140 | .setTitle("Stats Channel") |
| 141 | .setDescription("No changes were made") |
| 142 | .setStatus("Success") |
| 143 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 144 | ], |
| 145 | components: [] |
| 146 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 147 | } |
| 148 | await statsChannelAddCallback(client, interaction.member); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 149 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 150 | let timedOut = false; |
| 151 | while (!timedOut) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 152 | config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 153 | const stats = config.getKey("stats"); |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 154 | const selectMenu = new SelectMenuBuilder() |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 155 | .setCustomId("remove") |
| 156 | .setMinValues(1) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 157 | .setMaxValues(Math.max(1, Object.keys(stats).length)); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 158 | await interaction.editReply({ |
| 159 | embeds: [ |
| 160 | new EmojiEmbed() |
| 161 | .setTitle("Stats Channel") |
| 162 | .setDescription( |
| 163 | "The following channels update when someone joins or leaves the server. You can select a channel to remove it from the list." |
| 164 | ) |
| 165 | .setStatus("Success") |
| 166 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 167 | ], |
| 168 | components: [ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 169 | new ActionRowBuilder().addComponents( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 170 | Object.keys(stats).length |
| 171 | ? [ |
| 172 | selectMenu |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 173 | .setPlaceholder("Select a stats channel to remove, stopping it updating") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 174 | .addOptions( |
| 175 | Object.keys(stats).map((key) => ({ |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 176 | label: interaction.guild.channels.cache.get(key).name, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 177 | value: key, |
| 178 | description: `${stats[key].name}` |
| 179 | })) |
| 180 | ) |
| 181 | ] |
| 182 | : [ |
| 183 | selectMenu |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 184 | .setPlaceholder("The server has no stats channels") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 185 | .setDisabled(true) |
| 186 | .setOptions([ |
| 187 | { |
| 188 | label: "*Placeholder*", |
| 189 | value: "placeholder", |
| 190 | description: "No stats channels" |
| 191 | } |
| 192 | ]) |
| 193 | ] |
| 194 | ) |
| 195 | ] |
| 196 | }); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 197 | let i; |
| 198 | try { |
| 199 | i = await m.awaitMessageComponent({ time: 300000 }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 200 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 201 | timedOut = true; |
| 202 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 203 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 204 | i.deferUpdate(); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 205 | if (i.customId === "remove") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 206 | const toRemove = i.values; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 207 | await client.database.guilds.write( |
| 208 | interaction.guild.id, |
| 209 | null, |
| 210 | toRemove.map((k) => `stats.${k}`) |
| 211 | ); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 212 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 213 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 214 | await interaction.editReply({ |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 215 | embeds: [m.embeds[0]!.setFooter({ text: "Message timed out" })], |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 216 | components: [] |
| 217 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 218 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 219 | |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 220 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 221 | const member = interaction.member as Discord.GuildMember; |
| 222 | if (!member.permissions.has("MANAGE_CHANNELS")) |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 223 | throw new Error("You must have the *Manage Channels* permission to use this command"); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 224 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 225 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 226 | |
| 227 | export { command }; |
| 228 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 229 | export { check }; |