pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
pineafan | bd02b4a | 2022-08-05 22:01:38 +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"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +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") |
| 15 | .setDescription("Controls channels which update when someone joins or leaves the server") |
| 16 | .addChannelOption(option => option.setName("channel").setDescription("The channel to modify")) |
| 17 | .addStringOption(option => option.setName("name").setDescription("The new channel name | Enter any text or use the extra variables like {memberCount}").setAutocomplete(true)); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 18 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 19 | const callback = async (interaction: CommandInteraction): Promise<void | unknown> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | singleNotify("statsChannelDeleted", interaction.guild.id, true); |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 21 | const m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}) as Message; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 22 | let config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 23 | if (interaction.options.getString("name")) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 24 | let channel; |
| 25 | if (Object.keys(config.getKey("stats")).length >= 25) { |
| 26 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 27 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 28 | .setTitle("Stats Channel") |
| 29 | .setDescription("You can only have 25 stats channels in a server") |
| 30 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | ]}); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 32 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 33 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 34 | channel = interaction.options.getChannel("channel"); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 35 | } catch { |
| 36 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 37 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 38 | .setTitle("Stats Channel") |
| 39 | .setDescription("The channel you provided is not a valid channel") |
| 40 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 41 | ]}); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 42 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | channel = channel as Discord.TextChannel; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 44 | if (channel.guild.id !== interaction.guild.id) { |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 45 | return interaction.editReply({embeds: [new EmojiEmbed() |
| 46 | .setTitle("Stats Channel") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 47 | .setDescription("You must choose a channel in this server") |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 48 | .setStatus("Danger") |
| 49 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 50 | ]}); |
| 51 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 52 | let newName = await convertCurlyBracketString(interaction.options.getString("name"), null, null, interaction.guild.name, interaction.guild.members); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 53 | if (interaction.options.getChannel("channel").type === "GUILD_TEXT") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | newName = newName.toLowerCase().replace(/[\s]/g, "-"); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 55 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 56 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 57 | .setEmoji("CHANNEL.TEXT.EDIT") |
| 58 | .setTitle("Stats Channel") |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 59 | .setDescription(`Are you sure you want to set <#${channel.id}> to a stats channel?\n\n*Preview: ${newName.replace(/^ +| $/g, "")}*`) |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 60 | .setColor("Warning") |
| 61 | .setInverted(true) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 62 | .send(true); |
| 63 | if (confirmation.cancelled) return; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 64 | if (confirmation.success) { |
| 65 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 66 | const name = interaction.options.getString("name"); |
| 67 | const channel = interaction.options.getChannel("channel"); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 68 | await client.database.guilds.write(interaction.guild.id, {[`stats.${channel.id}`]: {name: name, enabled: true}}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 69 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
| 70 | const data = { |
| 71 | meta:{ |
| 72 | type: "statsChannelUpdate", |
| 73 | displayName: "Stats Channel Updated", |
| 74 | calculateType: "nucleusSettingsUpdated", |
| 75 | color: NucleusColors.yellow, |
| 76 | emoji: "CHANNEL.TEXT.EDIT", |
| 77 | timestamp: new Date().getTime() |
| 78 | }, |
| 79 | list: { |
| 80 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 81 | changedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 82 | channel: entry(channel.id, renderChannel(channel)), |
| 83 | name: entry(interaction.options.getString("name"), `\`${interaction.options.getString("name")}\``) |
| 84 | }, |
| 85 | hidden: { |
| 86 | guild: interaction.guild.id |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 87 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 88 | }; |
| 89 | log(data); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 90 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 91 | console.log(e); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 92 | return interaction.editReply({embeds: [new EmojiEmbed() |
| 93 | .setTitle("Stats Channel") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 94 | .setDescription("Something went wrong and the stats channel could not be set") |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 95 | .setStatus("Danger") |
| 96 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 97 | ], components: []}); |
| 98 | } |
| 99 | } else { |
| 100 | return interaction.editReply({embeds: [new EmojiEmbed() |
| 101 | .setTitle("Stats Channel") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | .setDescription("No changes were made") |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 103 | .setStatus("Success") |
| 104 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 105 | ], components: []}); |
| 106 | } |
| 107 | await statsChannelAddCallback(client, interaction.member); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 108 | } |
| 109 | while (true) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 110 | config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 111 | const stats = config.getKey("stats"); |
| 112 | const selectMenu = new MessageSelectMenu() |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 113 | .setCustomId("remove") |
| 114 | .setMinValues(1) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 115 | .setMaxValues(Math.max(1, Object.keys(stats).length)); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 116 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 117 | .setTitle("Stats Channel") |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 118 | .setDescription("The following channels update when someone joins or leaves the server. You can select a channel to remove it from the list.") |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 119 | .setStatus("Success") |
| 120 | .setEmoji("CHANNEL.TEXT.CREATE") |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 121 | ], components: [ |
| 122 | new MessageActionRow().addComponents(Object.keys(stats).length ? [ |
| 123 | selectMenu.setPlaceholder("Select a stats channel to remove, stopping it updating").addOptions(Object.keys(stats).map(key => ({ |
| 124 | label: interaction.guild.channels.cache.get(key).name, |
| 125 | value: key, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 126 | description: `${stats[key].name}` |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 127 | }))) |
| 128 | ] : [selectMenu.setPlaceholder("The server has no stats channels").setDisabled(true).setOptions([ |
| 129 | {label: "*Placeholder*", value: "placeholder", description: "No stats channels"} |
| 130 | ])]) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 131 | ]}); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 132 | let i; |
| 133 | try { |
| 134 | i = await m.awaitMessageComponent({ time: 300000 }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 135 | } catch (e) { break; } |
| 136 | i.deferUpdate(); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 137 | if (i.customId === "remove") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 138 | const toRemove = i.values; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 139 | await client.database.guilds.write(interaction.guild.id, null, toRemove.map(k => `stats.${k}`)); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 140 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 141 | } |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 142 | await interaction.editReply({embeds: [(m.embeds[0] as Discord.MessageEmbed).setFooter({text: "Message closed"})], components: []}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 143 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 144 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 145 | const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 146 | const member = (interaction.member as Discord.GuildMember); |
| 147 | if (!member.permissions.has("MANAGE_CHANNELS")) throw "You must have the *Manage Channels* permission to use this command"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 148 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 149 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 150 | |
| 151 | export { command }; |
| 152 | export { callback }; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 153 | export { check }; |