blob: c2bf9a181d772864de721e0aeecdc26b9891064f [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 -050024
25const 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
pineafanbd02b4a2022-08-05 22:01:38 +010072const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan1e462ab2023-03-07 21:34:06 +000073 const description = `**Servers:** ${client.guilds.cache.size}\n` + `**Ping:** \`${client.ws.ping * 2}ms\``;
TheCodedProfe92b9b52023-03-06 17:07:34 -050074 const m = await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010075 embeds: [
76 new EmojiEmbed()
77 .setTitle("Stats")
TheCodedProfe92b9b52023-03-06 17:07:34 -050078 .setDescription(description)
Skyler Grey75ea9172022-08-06 10:22:23 +010079 .setStatus("Success")
TheCodedProf4a6d5712023-01-19 15:54:40 -050080 .setEmoji("SETTINGS.STATS.GREEN")
Skyler Grey75ea9172022-08-06 10:22:23 +010081 ],
TheCodedProfe92b9b52023-03-06 17:07:34 -050082 ephemeral: true,
83 fetchReply: true
pineafan377794f2022-04-18 19:01:01 +010084 });
TheCodedProfe92b9b52023-03-06 17:07:34 -050085 if (config.owners.includes(interaction.user.id)) {
Skyler Greyf4f21c42023-03-08 14:36:29 +000086 await interaction.editReply({
TheCodedProfe92b9b52023-03-06 17:07:34 -050087 embeds: [
88 new EmojiEmbed()
89 .setTitle("Admin")
90 .setDescription(description)
91 .setStatus("Success")
92 .setEmoji("SETTINGS.STATS.GREEN")
pineafan1e462ab2023-03-07 21:34:06 +000093 ],
94 components: [
95 new ActionRowBuilder<ButtonBuilder>().addComponents(
TheCodedProf35e73712023-03-10 17:35:35 -050096 new ButtonBuilder().setCustomId("admin").setLabel("Admin Panel").setStyle(ButtonStyle.Primary)
pineafan1e462ab2023-03-07 21:34:06 +000097 )
98 ]
TheCodedProfe92b9b52023-03-06 17:07:34 -050099 });
100
101 const modal = new ModalBuilder()
102 .addComponents(
pineafan1e462ab2023-03-07 21:34:06 +0000103 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 )
TheCodedProfe92b9b52023-03-06 17:07:34 -0500112 )
113 .setTitle("Admin Panel")
pineafan1e462ab2023-03-07 21:34:06 +0000114 .setCustomId("adminPanel");
TheCodedProfe92b9b52023-03-06 17:07:34 -0500115 let i1: ButtonInteraction;
pineafan1e462ab2023-03-07 21:34:06 +0000116 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;
TheCodedProfe92b9b52023-03-06 17:07:34 -0500122 // console.log(interaction)
123 if (!("awaitMessageComponent" in channel)) return;
TheCodedProf35e73712023-03-10 17:35:35 -0500124 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)) {
TheCodedProfe92b9b52023-03-06 17:07:34 -0500148 await interaction.editReply({
pineafan1e462ab2023-03-07 21:34:06 +0000149 embeds: [new EmojiEmbed().setTitle("Admin").setDescription("Not in server").setStatus("Danger")],
150 components: []
TheCodedProfe92b9b52023-03-06 17:07:34 -0500151 });
pineafan1e462ab2023-03-07 21:34:06 +0000152 }
TheCodedProfe92b9b52023-03-06 17:07:34 -0500153
154 await interaction.editReply({
155 embeds: [],
156 components: [
157 new ActionRowBuilder<ButtonBuilder>().addComponents(
158 new ButtonBuilder().setCustomId("stats").setLabel("Stats").setStyle(ButtonStyle.Primary),
TheCodedProfe92b9b52023-03-06 17:07:34 -0500159 new ButtonBuilder().setCustomId("data").setLabel("Guild data").setStyle(ButtonStyle.Secondary),
TheCodedProf35e73712023-03-10 17:35:35 -0500160 new ButtonBuilder().setCustomId("cache").setLabel("Reset cache").setStyle(ButtonStyle.Success),
161 new ButtonBuilder().setCustomId("leave").setLabel("Leave").setStyle(ButtonStyle.Danger),
TheCodedProfe92b9b52023-03-06 17:07:34 -0500162 new ButtonBuilder().setCustomId("purge").setLabel("Delete data").setStyle(ButtonStyle.Danger),
TheCodedProfe92b9b52023-03-06 17:07:34 -0500163 )
pineafan1e462ab2023-03-07 21:34:06 +0000164 ]
165 });
TheCodedProfe92b9b52023-03-06 17:07:34 -0500166 let i;
167 try {
168 i = await m.awaitMessageComponent<ComponentType.Button>({
TheCodedProf80ad8542023-03-10 12:52:33 -0500169 filter: (i) => i.user.id === interaction.user.id && i.message.id === m.id,
TheCodedProfe92b9b52023-03-06 17:07:34 -0500170 time: 300000
pineafan1e462ab2023-03-07 21:34:06 +0000171 });
172 } catch {
173 return;
174 }
pineafan1e462ab2023-03-07 21:34:06 +0000175 const guild = (await client.guilds.fetch(GuildID)) as Guild | null;
TheCodedProfe92b9b52023-03-06 17:07:34 -0500176 if (!guild) {
TheCodedProf35e73712023-03-10 17:35:35 -0500177 await i.deferUpdate();
TheCodedProfe92b9b52023-03-06 17:07:34 -0500178 await interaction.editReply({
pineafan1e462ab2023-03-07 21:34:06 +0000179 embeds: [new EmojiEmbed().setTitle("Admin").setDescription("Not in server").setStatus("Danger")],
180 components: []
TheCodedProfe92b9b52023-03-06 17:07:34 -0500181 });
182 return;
183 }
184 if (i.customId === "stats") {
TheCodedProf35e73712023-03-10 17:35:35 -0500185 await i.deferUpdate();
TheCodedProfe92b9b52023-03-06 17:07:34 -0500186 await interaction.editReply({
187 embeds: [
188 new EmojiEmbed()
TheCodedProf35e73712023-03-10 17:35:35 -0500189 .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`
TheCodedProfe92b9b52023-03-06 17:07:34 -0500198 )
199 .setStatus("Success")
200 .setEmoji("SETTINGS.STATS.GREEN")
TheCodedProf35e73712023-03-10 17:35:35 -0500201 ]
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()
TheCodedProfe92b9b52023-03-06 17:07:34 -0500219 .setTitle("Left")
220 .setDescription(`Left ${guild.name}`)
221 .setStatus("Success")
222 .setEmoji("SETTINGS.STATS.GREEN")
TheCodedProf35e73712023-03-10 17:35:35 -0500223 ],
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()
TheCodedProfe92b9b52023-03-06 17:07:34 -0500259 .setTitle("Purge")
260 .setDescription(`Deleted data for ${guild.name}`)
261 .setStatus("Success")
262 .setEmoji("SETTINGS.STATS.GREEN")
TheCodedProf35e73712023-03-10 17:35:35 -0500263 ],
264 components: []
265 });
266 } else if (i.customId === "cache") {
267 await i.deferUpdate();
268 await client.memory.forceUpdate(guild.id);
269 await interaction.editReply({
TheCodedProfe92b9b52023-03-06 17:07:34 -0500270 embeds: [
271 new EmojiEmbed()
272 .setTitle("Cache")
273 .setDescription(`Reset cache for ${guild.name}`)
274 .setStatus("Success")
275 .setEmoji("SETTINGS.STATS.GREEN")
pineafan1e462ab2023-03-07 21:34:06 +0000276 ],
277 components: []
278 });
TheCodedProfe92b9b52023-03-06 17:07:34 -0500279 }
280 }
pineafan63fc5e22022-08-04 22:04:10 +0100281};
pineafan4f164f32022-02-26 22:07:12 +0000282
pineafan4f164f32022-02-26 22:07:12 +0000283export { command };
284export { callback };