pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 1 | import { |
| 2 | ActionRowBuilder, |
| 3 | AttachmentBuilder, |
| 4 | ButtonBuilder, |
| 5 | ButtonInteraction, |
| 6 | ButtonStyle, |
| 7 | ChannelType, |
| 8 | CommandInteraction, |
| 9 | ComponentType, |
| 10 | Guild, |
| 11 | ModalBuilder, |
| 12 | ModalSubmitInteraction, |
| 13 | TextInputBuilder, |
| 14 | TextInputStyle |
| 15 | } from "discord.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 16 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 17 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 18 | import client from "../../utils/client.js"; |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 19 | import config from "../../config/main.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 20 | |
| 21 | const command = (builder: SlashCommandSubcommandBuilder) => |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 22 | builder.setName("stats").setDescription("Gets the bot's stats"); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 23 | |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 24 | const confirm = async (interaction: CommandInteraction) => { |
| 25 | const requiredTexts = [ |
| 26 | "just do it", |
| 27 | "yes, do as i say!", |
| 28 | "clicksminuteper/nucleus", |
| 29 | "i've said it once i'll say it again", |
| 30 | "no, i've changed my mind", |
| 31 | "this incident will be reported", |
| 32 | "coded told me to", |
| 33 | "mini told me to", |
| 34 | "pinea told me to", |
| 35 | "what's a java script", |
| 36 | "it's a feature not a bug", |
| 37 | "that never happened during testing" |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 38 | ]; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 39 | const chosen = requiredTexts[Math.floor(Math.random() * (requiredTexts.length - 1))]!; |
| 40 | |
| 41 | const modal = new ModalBuilder() |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 42 | .addComponents( |
| 43 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 44 | new TextInputBuilder() |
| 45 | .setStyle(TextInputStyle.Short) |
| 46 | .setLabel(`Type "${chosen}" below`) |
| 47 | .setCustomId("confirm") |
| 48 | .setPlaceholder("Guild ID") |
| 49 | .setMinLength(chosen.length) |
| 50 | .setMaxLength(chosen.length) |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 51 | ) |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 52 | ) |
| 53 | .setTitle("Admin Panel") |
| 54 | .setCustomId("adminPanel"); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 55 | await interaction.showModal(modal); |
| 56 | let out: ModalSubmitInteraction; |
| 57 | try { |
| 58 | out = await interaction.awaitModalSubmit({ |
| 59 | filter: (i) => i.customId === "adminPanel" && i.user.id === interaction.user.id, |
| 60 | time: 300000 |
| 61 | }); |
| 62 | } catch { |
| 63 | return; |
| 64 | } |
| 65 | await out.deferUpdate(); |
| 66 | const typed = out.fields.getTextInputValue("confirm"); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 67 | return typed.toLowerCase() === chosen.toLowerCase(); |
| 68 | }; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 69 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 70 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 71 | const description = `**Servers:** ${client.guilds.cache.size}\n` + `**Ping:** \`${client.ws.ping * 2}ms\``; |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 72 | const m = await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | embeds: [ |
| 74 | new EmojiEmbed() |
| 75 | .setTitle("Stats") |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 76 | .setDescription(description) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 77 | .setStatus("Success") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 78 | .setEmoji("SETTINGS.STATS.GREEN") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 79 | ], |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 80 | ephemeral: true, |
| 81 | fetchReply: true |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 82 | }); |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 83 | if (config.owners.includes(interaction.user.id)) { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 84 | await interaction.editReply({ |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 85 | embeds: [ |
| 86 | new EmojiEmbed() |
| 87 | .setTitle("Admin") |
| 88 | .setDescription(description) |
| 89 | .setStatus("Success") |
| 90 | .setEmoji("SETTINGS.STATS.GREEN") |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 91 | ], |
| 92 | components: [ |
| 93 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 94 | new ButtonBuilder().setCustomId("admin").setLabel("Admin Panel").setStyle(ButtonStyle.Primary) |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 95 | ) |
| 96 | ] |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 97 | }); |
| 98 | |
| 99 | const modal = new ModalBuilder() |
| 100 | .addComponents( |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 101 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 102 | new TextInputBuilder() |
| 103 | .setStyle(TextInputStyle.Short) |
| 104 | .setLabel("Guild ID") |
| 105 | .setCustomId("guildID") |
| 106 | .setPlaceholder("Guild ID") |
| 107 | .setMinLength(16) |
| 108 | .setMaxLength(25) |
| 109 | ) |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 110 | ) |
| 111 | .setTitle("Admin Panel") |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 112 | .setCustomId("adminPanel"); |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 113 | let i1: ButtonInteraction; |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 114 | const channel = await client.channels.fetch(interaction.channelId); |
| 115 | if ( |
| 116 | !channel || |
| 117 | [ChannelType.GuildCategory, ChannelType.GroupDM, ChannelType.GuildStageVoice].includes(channel.type) |
| 118 | ) |
| 119 | return; |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 120 | // console.log(interaction) |
| 121 | if (!("awaitMessageComponent" in channel)) return; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 122 | let GuildID = interaction.guildId; |
| 123 | if (!GuildID) { |
| 124 | try { |
| 125 | i1 = await channel!.awaitMessageComponent<ComponentType.Button>({ |
| 126 | filter: (i) => i.customId === "admin" && i.user.id === interaction.user.id && i.message.id === m.id, |
| 127 | time: 300000 |
| 128 | }); |
| 129 | } catch (e) { |
| 130 | console.log(e); |
| 131 | return; |
| 132 | } |
| 133 | await i1.showModal(modal); |
| 134 | let out: ModalSubmitInteraction; |
| 135 | try { |
| 136 | out = await i1.awaitModalSubmit({ |
| 137 | filter: (i) => i.customId === "adminPanel" && i.user.id === interaction.user.id, |
| 138 | time: 300000 |
| 139 | }); |
| 140 | } catch { |
| 141 | return; |
| 142 | } |
| 143 | await out.deferUpdate(); |
| 144 | GuildID = out.fields.getTextInputValue("guildID"); |
| 145 | } else if (!client.guilds.cache.has(GuildID)) { |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 146 | await interaction.editReply({ |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 147 | embeds: [new EmojiEmbed().setTitle("Admin").setDescription("Not in server").setStatus("Danger")], |
| 148 | components: [] |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 149 | }); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 150 | } |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 151 | |
| 152 | await interaction.editReply({ |
| 153 | embeds: [], |
| 154 | components: [ |
| 155 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 156 | new ButtonBuilder().setCustomId("stats").setLabel("Stats").setStyle(ButtonStyle.Primary), |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 157 | new ButtonBuilder().setCustomId("data").setLabel("Guild data").setStyle(ButtonStyle.Secondary), |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 158 | new ButtonBuilder().setCustomId("cache").setLabel("Reset cache").setStyle(ButtonStyle.Success), |
| 159 | new ButtonBuilder().setCustomId("leave").setLabel("Leave").setStyle(ButtonStyle.Danger), |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 160 | new ButtonBuilder().setCustomId("purge").setLabel("Delete data").setStyle(ButtonStyle.Danger) |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 161 | ) |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 162 | ] |
| 163 | }); |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 164 | let i; |
| 165 | try { |
| 166 | i = await m.awaitMessageComponent<ComponentType.Button>({ |
TheCodedProf | 80ad854 | 2023-03-10 12:52:33 -0500 | [diff] [blame] | 167 | filter: (i) => i.user.id === interaction.user.id && i.message.id === m.id, |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 168 | time: 300000 |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 169 | }); |
| 170 | } catch { |
| 171 | return; |
| 172 | } |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 173 | const guild = (await client.guilds.fetch(GuildID)) as Guild | null; |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 174 | if (!guild) { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 175 | await i.deferUpdate(); |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 176 | await interaction.editReply({ |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 177 | embeds: [new EmojiEmbed().setTitle("Admin").setDescription("Not in server").setStatus("Danger")], |
| 178 | components: [] |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 179 | }); |
| 180 | return; |
| 181 | } |
| 182 | if (i.customId === "stats") { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 183 | await i.deferUpdate(); |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 184 | await interaction.editReply({ |
| 185 | embeds: [ |
| 186 | new EmojiEmbed() |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 187 | .setTitle("Stats") |
| 188 | .setDescription( |
| 189 | `**Name:** ${guild.name}\n` + |
| 190 | `**ID:** \`${guild.id}\`\n` + |
| 191 | `**Owner:** ${client.users.cache.get(guild.ownerId)!.tag}\n` + |
| 192 | `**Member Count:** ${guild.memberCount}\n` + |
| 193 | `**Created:** <t:${guild.createdTimestamp}:F>\n` + |
| 194 | `**Added Nucleus:** <t:${guild.members.me!.joinedTimestamp}:R>\n` + |
| 195 | `**Nucleus' Perms:** https://discordapi.com/permissions.html#${guild.members.me!.permissions.valueOf()}\n` |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 196 | ) |
| 197 | .setStatus("Success") |
| 198 | .setEmoji("SETTINGS.STATS.GREEN") |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 199 | ] |
| 200 | }); |
| 201 | } else if (i.customId === "leave") { |
| 202 | if (!(await confirm(interaction))) { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 203 | await interaction.editReply({ |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 204 | embeds: [new EmojiEmbed().setTitle("No changes were made").setStatus("Danger")], |
| 205 | components: [] |
| 206 | }); |
| 207 | return; |
| 208 | } |
| 209 | await guild.leave(); |
| 210 | await interaction.editReply({ |
| 211 | embeds: [ |
| 212 | new EmojiEmbed() |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 213 | .setTitle("Left") |
| 214 | .setDescription(`Left ${guild.name}`) |
| 215 | .setStatus("Success") |
| 216 | .setEmoji("SETTINGS.STATS.GREEN") |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 217 | ], |
| 218 | components: [] |
| 219 | }); |
| 220 | } else if (i.customId === "data") { |
| 221 | await i.deferUpdate(); |
| 222 | // Get all the data and convert to a string |
| 223 | const data = await client.database.guilds.read(guild.id); |
| 224 | const stringified = JSON.stringify(data, null, 2); |
| 225 | const buffer = Buffer.from(stringified); |
| 226 | const attachment = new AttachmentBuilder(buffer).setName("data.json"); |
| 227 | await interaction.editReply({ |
| 228 | embeds: [ |
| 229 | new EmojiEmbed().setTitle("Data").setDescription(`Data for ${guild.name}`).setStatus("Success") |
| 230 | ], |
| 231 | components: [], |
| 232 | files: [attachment] |
| 233 | }); |
| 234 | } else if (i.customId === "purge") { |
| 235 | if (!(await confirm(interaction))) { |
| 236 | await interaction.editReply({ |
| 237 | embeds: [new EmojiEmbed().setTitle("No changes were made").setStatus("Danger")], |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 238 | components: [] |
| 239 | }); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | await client.database.guilds.delete(GuildID); |
| 243 | await client.database.history.delete(GuildID); |
| 244 | await client.database.notes.delete(GuildID); |
| 245 | await client.database.transcripts.deleteAll(GuildID); |
| 246 | await interaction.editReply({ |
| 247 | embeds: [ |
| 248 | new EmojiEmbed() |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 249 | .setTitle("Purge") |
| 250 | .setDescription(`Deleted data for ${guild.name}`) |
| 251 | .setStatus("Success") |
| 252 | .setEmoji("SETTINGS.STATS.GREEN") |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 253 | ], |
| 254 | components: [] |
| 255 | }); |
| 256 | } else if (i.customId === "cache") { |
| 257 | await i.deferUpdate(); |
| 258 | await client.memory.forceUpdate(guild.id); |
| 259 | await interaction.editReply({ |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 260 | embeds: [ |
| 261 | new EmojiEmbed() |
| 262 | .setTitle("Cache") |
| 263 | .setDescription(`Reset cache for ${guild.name}`) |
| 264 | .setStatus("Success") |
| 265 | .setEmoji("SETTINGS.STATS.GREEN") |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 266 | ], |
| 267 | components: [] |
| 268 | }); |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 269 | } |
| 270 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 271 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 272 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 273 | export { command }; |
| 274 | export { callback }; |