TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 1 | import { ActionRowBuilder, AttachmentBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, ChannelType, CommandInteraction, ComponentType, Guild, ModalBuilder, ModalSubmitInteraction, TextInputBuilder, TextInputStyle } from "discord.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 2 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 3 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 4 | import client from "../../utils/client.js"; |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 5 | import config from "../../config/main.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 6 | |
| 7 | const command = (builder: SlashCommandSubcommandBuilder) => |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 8 | builder.setName("stats").setDescription("Gets the bot's stats"); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 9 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 10 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 11 | const description = `**Servers:** ${client.guilds.cache.size}\n` + `**Ping:** \`${client.ws.ping * 2}ms\`` |
| 12 | const m = await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 13 | embeds: [ |
| 14 | new EmojiEmbed() |
| 15 | .setTitle("Stats") |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 16 | .setDescription(description) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 17 | .setStatus("Success") |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 18 | .setEmoji("SETTINGS.STATS.GREEN") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 19 | ], |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 20 | ephemeral: true, |
| 21 | fetchReply: true |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 22 | }); |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 23 | if (config.owners.includes(interaction.user.id)) { |
| 24 | interaction.editReply({ |
| 25 | embeds: [ |
| 26 | new EmojiEmbed() |
| 27 | .setTitle("Admin") |
| 28 | .setDescription(description) |
| 29 | .setStatus("Success") |
| 30 | .setEmoji("SETTINGS.STATS.GREEN") |
| 31 | ], components: [new ActionRowBuilder<ButtonBuilder>().addComponents(new ButtonBuilder().setCustomId("admin").setLabel("Admin Panel").setStyle(ButtonStyle.Primary))] |
| 32 | }); |
| 33 | |
| 34 | const modal = new ModalBuilder() |
| 35 | .addComponents( |
| 36 | new ActionRowBuilder<TextInputBuilder>() |
| 37 | .addComponents( |
| 38 | new TextInputBuilder() |
| 39 | .setStyle(TextInputStyle.Short) |
| 40 | .setLabel("Guild ID") |
| 41 | .setCustomId("guildID") |
| 42 | .setPlaceholder("Guild ID") |
| 43 | .setMinLength(16) |
| 44 | .setMaxLength(25) |
| 45 | ) |
| 46 | ) |
| 47 | .setTitle("Admin Panel") |
| 48 | .setCustomId("adminPanel") |
| 49 | let i1: ButtonInteraction; |
| 50 | const channel = await client.channels.fetch(interaction.channelId) |
| 51 | if(!channel || [ChannelType.GuildCategory, ChannelType.GroupDM, ChannelType.GuildStageVoice].includes(channel.type)) return; |
| 52 | // console.log(interaction) |
| 53 | if (!("awaitMessageComponent" in channel)) return; |
| 54 | try { |
| 55 | i1 = await channel!.awaitMessageComponent<ComponentType.Button>({ |
| 56 | filter: (i) => i.customId === "admin" && i.user.id === interaction.user.id, |
| 57 | time: 300000 |
| 58 | }); |
| 59 | } catch (e) { console.log(e); return } |
| 60 | await i1.showModal(modal) |
| 61 | let out: ModalSubmitInteraction; |
| 62 | try { |
| 63 | out = await i1.awaitModalSubmit({ |
| 64 | filter: (i) => i.customId === "adminPanel" && i.user.id === interaction.user.id, |
| 65 | time: 300000 |
| 66 | }) |
| 67 | } catch { return } |
| 68 | out.deferUpdate(); |
| 69 | const GuildID = out.fields.getTextInputValue("guildID"); |
| 70 | if (!client.guilds.cache.has(GuildID)) { |
| 71 | await interaction.editReply({ |
| 72 | embeds: [ |
| 73 | new EmojiEmbed() |
| 74 | .setTitle("Admin") |
| 75 | .setDescription("Not in server") |
| 76 | .setStatus("Danger") |
| 77 | ], components: [] |
| 78 | }); |
| 79 | }; |
| 80 | |
| 81 | await interaction.editReply({ |
| 82 | embeds: [], |
| 83 | components: [ |
| 84 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 85 | new ButtonBuilder().setCustomId("stats").setLabel("Stats").setStyle(ButtonStyle.Primary), |
| 86 | new ButtonBuilder().setCustomId("leave").setLabel("Leave").setStyle(ButtonStyle.Danger), |
| 87 | new ButtonBuilder().setCustomId("data").setLabel("Guild data").setStyle(ButtonStyle.Secondary), |
| 88 | new ButtonBuilder().setCustomId("purge").setLabel("Delete data").setStyle(ButtonStyle.Danger), |
| 89 | new ButtonBuilder().setCustomId("cache").setLabel("Reset cache").setStyle(ButtonStyle.Success) |
| 90 | ) |
| 91 | ]}); |
| 92 | let i; |
| 93 | try { |
| 94 | i = await m.awaitMessageComponent<ComponentType.Button>({ |
| 95 | filter: (i) => i.user.id === interaction.user.id, |
| 96 | time: 300000 |
| 97 | }) |
| 98 | } catch { return } |
| 99 | i.deferUpdate(); |
| 100 | const guild = await client.guilds.fetch(GuildID) as Guild | null; |
| 101 | if (!guild) { |
| 102 | await interaction.editReply({ |
| 103 | embeds: [ |
| 104 | new EmojiEmbed() |
| 105 | .setTitle("Admin") |
| 106 | .setDescription("Not in server") |
| 107 | .setStatus("Danger") |
| 108 | ], components: [] |
| 109 | }); |
| 110 | return; |
| 111 | } |
| 112 | if (i.customId === "stats") { |
| 113 | await interaction.editReply({ |
| 114 | embeds: [ |
| 115 | new EmojiEmbed() |
| 116 | .setTitle("Stats") |
| 117 | .setDescription( |
| 118 | `**Name:** ${guild.name}\n` + |
| 119 | `**ID:** \`${guild.id}\`\n` + |
| 120 | `**Owner:** ${client.users.cache.get(guild.ownerId)!.tag}\n` + |
| 121 | `**Member Count:** ${guild.memberCount}\n` + |
| 122 | `**Created:** <t:${guild.createdTimestamp}:F>\n` + |
| 123 | `**Added Nucleus:** <t:${guild.members.me!.joinedTimestamp}:R>\n` + |
| 124 | `**Nucleus' Perms:** https://discordapi.com/permissions.html#${guild.members.me!.permissions.valueOf()}\n` |
| 125 | ) |
| 126 | .setStatus("Success") |
| 127 | .setEmoji("SETTINGS.STATS.GREEN") |
| 128 | ] |
| 129 | }) |
| 130 | } else if (i.customId === "leave") { |
| 131 | await guild.leave(); |
| 132 | await interaction.editReply({ |
| 133 | embeds: [ |
| 134 | new EmojiEmbed() |
| 135 | .setTitle("Left") |
| 136 | .setDescription(`Left ${guild.name}`) |
| 137 | .setStatus("Success") |
| 138 | .setEmoji("SETTINGS.STATS.GREEN") |
| 139 | ], components: [] |
| 140 | }) |
| 141 | } else if (i.customId === "data") { |
| 142 | // Get all the data and convert to a string |
| 143 | const data = await client.database.guilds.read(guild.id); |
| 144 | const stringified = JSON.stringify(data, null, 2); |
| 145 | const buffer = Buffer.from(stringified); |
| 146 | const attachment = new AttachmentBuilder(buffer).setName("data.json"); |
| 147 | await interaction.editReply({ |
| 148 | embeds: [ |
| 149 | new EmojiEmbed().setTitle("Data").setDescription(`Data for ${guild.name}`).setStatus("Success") |
| 150 | ], components: [], |
| 151 | files: [attachment] |
| 152 | }) |
| 153 | } else if (i.customId === "purge") { |
| 154 | await client.database.guilds.delete(GuildID); |
| 155 | await client.database.history.delete(GuildID); |
| 156 | await client.database.notes.delete(GuildID); |
| 157 | await client.database.transcripts.deleteAll(GuildID); |
| 158 | await interaction.editReply({ |
| 159 | embeds: [ |
| 160 | new EmojiEmbed() |
| 161 | .setTitle("Purge") |
| 162 | .setDescription(`Deleted data for ${guild.name}`) |
| 163 | .setStatus("Success") |
| 164 | .setEmoji("SETTINGS.STATS.GREEN") |
| 165 | ], components: [] |
| 166 | }) |
| 167 | } else if (i.customId === "cache") { |
| 168 | await client.memory.forceUpdate(guild.id); |
| 169 | await interaction.editReply({ |
| 170 | embeds: [ |
| 171 | new EmojiEmbed() |
| 172 | .setTitle("Cache") |
| 173 | .setDescription(`Reset cache for ${guild.name}`) |
| 174 | .setStatus("Success") |
| 175 | .setEmoji("SETTINGS.STATS.GREEN") |
| 176 | ], components: [] |
| 177 | }) |
| 178 | } |
| 179 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 180 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 181 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 182 | export { command }; |
| 183 | export { callback }; |