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