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