blob: 058695cd914a10fcfd0541e2a0e216e8094b61b7 [file] [log] [blame]
TheCodedProfe92b9b52023-03-06 17:07:34 -05001import { ActionRowBuilder, AttachmentBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, ChannelType, CommandInteraction, ComponentType, Guild, ModalBuilder, ModalSubmitInteraction, TextInputBuilder, TextInputStyle } from "discord.js";
TheCodedProff86ba092023-01-27 17:10:07 -05002import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafan4edb7762022-06-26 19:21:04 +01003import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan63fc5e22022-08-04 22:04:10 +01004import client from "../../utils/client.js";
TheCodedProfe92b9b52023-03-06 17:07:34 -05005import config from "../../config/main.js";
pineafan4f164f32022-02-26 22:07:12 +00006
7const command = (builder: SlashCommandSubcommandBuilder) =>
Skyler Grey75ea9172022-08-06 10:22:23 +01008 builder.setName("stats").setDescription("Gets the bot's stats");
pineafan4f164f32022-02-26 22:07:12 +00009
pineafanbd02b4a2022-08-05 22:01:38 +010010const callback = async (interaction: CommandInteraction): Promise<void> => {
TheCodedProfe92b9b52023-03-06 17:07:34 -050011 const description = `**Servers:** ${client.guilds.cache.size}\n` + `**Ping:** \`${client.ws.ping * 2}ms\``
12 const m = await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010013 embeds: [
14 new EmojiEmbed()
15 .setTitle("Stats")
TheCodedProfe92b9b52023-03-06 17:07:34 -050016 .setDescription(description)
Skyler Grey75ea9172022-08-06 10:22:23 +010017 .setStatus("Success")
TheCodedProf4a6d5712023-01-19 15:54:40 -050018 .setEmoji("SETTINGS.STATS.GREEN")
Skyler Grey75ea9172022-08-06 10:22:23 +010019 ],
TheCodedProfe92b9b52023-03-06 17:07:34 -050020 ephemeral: true,
21 fetchReply: true
pineafan377794f2022-04-18 19:01:01 +010022 });
TheCodedProfe92b9b52023-03-06 17:07:34 -050023 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 }
pineafan63fc5e22022-08-04 22:04:10 +0100180};
pineafan4f164f32022-02-26 22:07:12 +0000181
pineafan4f164f32022-02-26 22:07:12 +0000182export { command };
183export { callback };