blob: 96902583a730d7aaec397204406a577b379868ca [file] [log] [blame]
pineafan1e462ab2023-03-07 21:34:06 +00001import {
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";
TheCodedProff86ba092023-01-27 17:10:07 -050016import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafan4edb7762022-06-26 19:21:04 +010017import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan63fc5e22022-08-04 22:04:10 +010018import client from "../../utils/client.js";
TheCodedProfe92b9b52023-03-06 17:07:34 -050019import config from "../../config/main.js";
pineafan4f164f32022-02-26 22:07:12 +000020
21const command = (builder: SlashCommandSubcommandBuilder) =>
Skyler Grey75ea9172022-08-06 10:22:23 +010022 builder.setName("stats").setDescription("Gets the bot's stats");
pineafan4f164f32022-02-26 22:07:12 +000023
TheCodedProf35e73712023-03-10 17:35:35 -050024const 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"
TheCodedProfca29ebb2023-03-10 17:40:09 -050038 ];
TheCodedProf35e73712023-03-10 17:35:35 -050039 const chosen = requiredTexts[Math.floor(Math.random() * (requiredTexts.length - 1))]!;
40
41 const modal = new ModalBuilder()
TheCodedProfca29ebb2023-03-10 17:40:09 -050042 .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)
TheCodedProf35e73712023-03-10 17:35:35 -050051 )
TheCodedProfca29ebb2023-03-10 17:40:09 -050052 )
53 .setTitle("Admin Panel")
54 .setCustomId("adminPanel");
TheCodedProf35e73712023-03-10 17:35:35 -050055 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");
TheCodedProfca29ebb2023-03-10 17:40:09 -050067 return typed.toLowerCase() === chosen.toLowerCase();
68};
TheCodedProf35e73712023-03-10 17:35:35 -050069
pineafanbd02b4a2022-08-05 22:01:38 +010070const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan1e462ab2023-03-07 21:34:06 +000071 const description = `**Servers:** ${client.guilds.cache.size}\n` + `**Ping:** \`${client.ws.ping * 2}ms\``;
TheCodedProfe92b9b52023-03-06 17:07:34 -050072 const m = await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010073 embeds: [
74 new EmojiEmbed()
75 .setTitle("Stats")
TheCodedProfe92b9b52023-03-06 17:07:34 -050076 .setDescription(description)
Skyler Grey75ea9172022-08-06 10:22:23 +010077 .setStatus("Success")
TheCodedProf4a6d5712023-01-19 15:54:40 -050078 .setEmoji("SETTINGS.STATS.GREEN")
Skyler Grey75ea9172022-08-06 10:22:23 +010079 ],
TheCodedProfe92b9b52023-03-06 17:07:34 -050080 ephemeral: true,
81 fetchReply: true
pineafan377794f2022-04-18 19:01:01 +010082 });
TheCodedProfe92b9b52023-03-06 17:07:34 -050083 if (config.owners.includes(interaction.user.id)) {
Skyler Greyf4f21c42023-03-08 14:36:29 +000084 await interaction.editReply({
TheCodedProfe92b9b52023-03-06 17:07:34 -050085 embeds: [
86 new EmojiEmbed()
87 .setTitle("Admin")
88 .setDescription(description)
89 .setStatus("Success")
90 .setEmoji("SETTINGS.STATS.GREEN")
pineafan1e462ab2023-03-07 21:34:06 +000091 ],
92 components: [
93 new ActionRowBuilder<ButtonBuilder>().addComponents(
TheCodedProf35e73712023-03-10 17:35:35 -050094 new ButtonBuilder().setCustomId("admin").setLabel("Admin Panel").setStyle(ButtonStyle.Primary)
pineafan1e462ab2023-03-07 21:34:06 +000095 )
96 ]
TheCodedProfe92b9b52023-03-06 17:07:34 -050097 });
98
99 const modal = new ModalBuilder()
100 .addComponents(
pineafan1e462ab2023-03-07 21:34:06 +0000101 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 )
TheCodedProfe92b9b52023-03-06 17:07:34 -0500110 )
111 .setTitle("Admin Panel")
pineafan1e462ab2023-03-07 21:34:06 +0000112 .setCustomId("adminPanel");
TheCodedProfe92b9b52023-03-06 17:07:34 -0500113 let i1: ButtonInteraction;
pineafan1e462ab2023-03-07 21:34:06 +0000114 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;
TheCodedProfe92b9b52023-03-06 17:07:34 -0500120 // console.log(interaction)
121 if (!("awaitMessageComponent" in channel)) return;
TheCodedProf35e73712023-03-10 17:35:35 -0500122 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)) {
TheCodedProfe92b9b52023-03-06 17:07:34 -0500146 await interaction.editReply({
pineafan1e462ab2023-03-07 21:34:06 +0000147 embeds: [new EmojiEmbed().setTitle("Admin").setDescription("Not in server").setStatus("Danger")],
148 components: []
TheCodedProfe92b9b52023-03-06 17:07:34 -0500149 });
pineafan1e462ab2023-03-07 21:34:06 +0000150 }
TheCodedProfe92b9b52023-03-06 17:07:34 -0500151
152 await interaction.editReply({
153 embeds: [],
154 components: [
155 new ActionRowBuilder<ButtonBuilder>().addComponents(
156 new ButtonBuilder().setCustomId("stats").setLabel("Stats").setStyle(ButtonStyle.Primary),
TheCodedProfe92b9b52023-03-06 17:07:34 -0500157 new ButtonBuilder().setCustomId("data").setLabel("Guild data").setStyle(ButtonStyle.Secondary),
TheCodedProf35e73712023-03-10 17:35:35 -0500158 new ButtonBuilder().setCustomId("cache").setLabel("Reset cache").setStyle(ButtonStyle.Success),
159 new ButtonBuilder().setCustomId("leave").setLabel("Leave").setStyle(ButtonStyle.Danger),
TheCodedProfca29ebb2023-03-10 17:40:09 -0500160 new ButtonBuilder().setCustomId("purge").setLabel("Delete data").setStyle(ButtonStyle.Danger)
TheCodedProfe92b9b52023-03-06 17:07:34 -0500161 )
pineafan1e462ab2023-03-07 21:34:06 +0000162 ]
163 });
TheCodedProfe92b9b52023-03-06 17:07:34 -0500164 let i;
165 try {
166 i = await m.awaitMessageComponent<ComponentType.Button>({
TheCodedProf80ad8542023-03-10 12:52:33 -0500167 filter: (i) => i.user.id === interaction.user.id && i.message.id === m.id,
TheCodedProfe92b9b52023-03-06 17:07:34 -0500168 time: 300000
pineafan1e462ab2023-03-07 21:34:06 +0000169 });
170 } catch {
171 return;
172 }
pineafan1e462ab2023-03-07 21:34:06 +0000173 const guild = (await client.guilds.fetch(GuildID)) as Guild | null;
TheCodedProfe92b9b52023-03-06 17:07:34 -0500174 if (!guild) {
TheCodedProf35e73712023-03-10 17:35:35 -0500175 await i.deferUpdate();
TheCodedProfe92b9b52023-03-06 17:07:34 -0500176 await interaction.editReply({
pineafan1e462ab2023-03-07 21:34:06 +0000177 embeds: [new EmojiEmbed().setTitle("Admin").setDescription("Not in server").setStatus("Danger")],
178 components: []
TheCodedProfe92b9b52023-03-06 17:07:34 -0500179 });
180 return;
181 }
182 if (i.customId === "stats") {
TheCodedProf35e73712023-03-10 17:35:35 -0500183 await i.deferUpdate();
TheCodedProfe92b9b52023-03-06 17:07:34 -0500184 await interaction.editReply({
185 embeds: [
186 new EmojiEmbed()
TheCodedProfca29ebb2023-03-10 17:40:09 -0500187 .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`
TheCodedProfe92b9b52023-03-06 17:07:34 -0500196 )
197 .setStatus("Success")
198 .setEmoji("SETTINGS.STATS.GREEN")
TheCodedProfca29ebb2023-03-10 17:40:09 -0500199 ]
200 });
201 } else if (i.customId === "leave") {
202 if (!(await confirm(interaction))) {
TheCodedProf35e73712023-03-10 17:35:35 -0500203 await interaction.editReply({
TheCodedProfca29ebb2023-03-10 17:40:09 -0500204 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()
TheCodedProfe92b9b52023-03-06 17:07:34 -0500213 .setTitle("Left")
214 .setDescription(`Left ${guild.name}`)
215 .setStatus("Success")
216 .setEmoji("SETTINGS.STATS.GREEN")
TheCodedProfca29ebb2023-03-10 17:40:09 -0500217 ],
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")],
TheCodedProf35e73712023-03-10 17:35:35 -0500238 components: []
239 });
TheCodedProfca29ebb2023-03-10 17:40:09 -0500240 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()
TheCodedProfe92b9b52023-03-06 17:07:34 -0500249 .setTitle("Purge")
250 .setDescription(`Deleted data for ${guild.name}`)
251 .setStatus("Success")
252 .setEmoji("SETTINGS.STATS.GREEN")
TheCodedProfca29ebb2023-03-10 17:40:09 -0500253 ],
254 components: []
255 });
256 } else if (i.customId === "cache") {
257 await i.deferUpdate();
258 await client.memory.forceUpdate(guild.id);
259 await interaction.editReply({
TheCodedProfe92b9b52023-03-06 17:07:34 -0500260 embeds: [
261 new EmojiEmbed()
262 .setTitle("Cache")
263 .setDescription(`Reset cache for ${guild.name}`)
264 .setStatus("Success")
265 .setEmoji("SETTINGS.STATS.GREEN")
pineafan1e462ab2023-03-07 21:34:06 +0000266 ],
267 components: []
268 });
TheCodedProfe92b9b52023-03-06 17:07:34 -0500269 }
270 }
pineafan63fc5e22022-08-04 22:04:10 +0100271};
pineafan4f164f32022-02-26 22:07:12 +0000272
pineafan4f164f32022-02-26 22:07:12 +0000273export { command };
274export { callback };