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