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