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