pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 2 | import Discord, { CommandInteraction, Message, MessageActionRow, MessageSelectMenu } 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 | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 6 | import { WrappedCheck } from "jshaiku"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 7 | import client from "../../utils/client.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 8 | import convertCurlyBracketString from "../../utils/convertCurlyBracketString.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 9 | import { callback as statsChannelAddCallback } from "../../reflex/statsChannelUpdate.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 10 | import singleNotify from "../../utils/singleNotify.js"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 11 | |
| 12 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 13 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | .setName("stats") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 15 | .setDescription("Controls channels which update when someone joins or leaves the server") |
| 16 | .addChannelOption((option) => option.setName("channel").setDescription("The channel to modify")) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 17 | .addStringOption((option) => |
| 18 | option |
| 19 | .setName("name") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 20 | .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] | 21 | .setAutocomplete(true) |
| 22 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 23 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 24 | const callback = async (interaction: CommandInteraction): Promise<void | unknown> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [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; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 31 | let config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 32 | if (interaction.options.getString("name")) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 33 | let channel; |
| 34 | if (Object.keys(config.getKey("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 { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 46 | channel = interaction.options.getChannel("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; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [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( |
| 71 | interaction.options.getString("name"), |
| 72 | null, |
| 73 | null, |
| 74 | interaction.guild.name, |
| 75 | interaction.guild.members |
| 76 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 77 | if (interaction.options.getChannel("channel").type === "GUILD_TEXT") { |
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) |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 81 | .setEmoji("CHANNEL.TEXT.EDIT") |
| 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) |
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 { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 95 | const name = interaction.options.getString("name"); |
| 96 | const channel = interaction.options.getChannel("channel"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 97 | await client.database.guilds.write(interaction.guild.id, { |
| 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( |
| 115 | interaction.options.getString("name"), |
| 116 | `\`${interaction.options.getString("name")}\`` |
| 117 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 118 | }, |
| 119 | hidden: { |
| 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 | } |
| 149 | await statsChannelAddCallback(client, interaction.member); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 150 | } |
| 151 | while (true) { |
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"); |
| 154 | const selectMenu = new MessageSelectMenu() |
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: [ |
| 169 | new MessageActionRow().addComponents( |
| 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) { |
| 201 | break; |
| 202 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 203 | i.deferUpdate(); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 204 | if (i.customId === "remove") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 205 | const toRemove = i.values; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 206 | await client.database.guilds.write( |
| 207 | interaction.guild.id, |
| 208 | null, |
| 209 | toRemove.map((k) => `stats.${k}`) |
| 210 | ); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 211 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 212 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 213 | await interaction.editReply({ |
| 214 | embeds: [m.embeds[0]!.setFooter({ text: "Message closed" })], |
| 215 | components: [] |
| 216 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 217 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 218 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 219 | const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 220 | const member = interaction.member as Discord.GuildMember; |
| 221 | if (!member.permissions.has("MANAGE_CHANNELS")) |
| 222 | throw "You must have the *Manage Channels* permission to use this command"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 223 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 224 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 225 | |
| 226 | export { command }; |
| 227 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 228 | export { check }; |