blob: af7beb331069847358b1aea6d450cc127e6b4d1f [file] [log] [blame]
pineafan8b4b17f2022-02-27 20:42:52 +00001import Discord, { CommandInteraction, GuildChannel, GuildMember, TextChannel } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
3import { WrappedCheck } from "jshaiku";
pineafan8b4b17f2022-02-27 20:42:52 +00004import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01005import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00006import keyValueList from "../../utils/generateKeyValueList.js";
7import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan4edb7762022-06-26 19:21:04 +01008import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00009
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
12 .setName("purge")
pineafan8b4b17f2022-02-27 20:42:52 +000013 .setDescription("Bulk deletes messages in a channel")
14 .addIntegerOption(option => option
15 .setName("amount")
16 .setDescription("The amount of messages to delete")
17 .setRequired(false)
18 .setMinValue(1)
pineafan1dc15722022-03-14 21:27:34 +000019 .setMaxValue(100))
pineafan8b4b17f2022-02-27 20:42:52 +000020 .addUserOption(option => option.setName("user").setDescription("The user to purge messages from").setRequired(false))
21 .addStringOption(option => option.setName("reason").setDescription("The reason for the purge").setRequired(false))
pineafan4f164f32022-02-26 22:07:12 +000022
pineafan6702cef2022-06-13 17:52:37 +010023const callback = async (interaction: CommandInteraction): Promise<any> => {
pineafan5d1908e2022-02-28 21:34:47 +000024 let user = interaction.options.getMember("user") as GuildMember ?? null
pineafan377794f2022-04-18 19:01:01 +010025 let channel = (interaction.channel as GuildChannel)
pineafan8b4b17f2022-02-27 20:42:52 +000026 if (!(["GUILD_TEXT", "GUILD_NEWS", "GUILD_NEWS_THREAD", "GUILD_PUBLIC_THREAD", "GUILD_PRIVATE_THREAD"].includes(channel.type.toString()))) {
27 return await interaction.reply({
28 embeds: [
pineafan4edb7762022-06-26 19:21:04 +010029 new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000030 .setEmoji("CHANNEL.PURGE.RED")
31 .setTitle("Purge")
32 .setDescription("You cannot purge this channel")
33 .setStatus("Danger")
34 ],
35 components: [],
36 ephemeral: true,
37 })
38 }
39 // TODO:[Modals] Replace this with a modal
40 if ( !interaction.options.getInteger("amount") ) {
41 await interaction.reply({
42 embeds: [
pineafan4edb7762022-06-26 19:21:04 +010043 new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000044 .setEmoji("CHANNEL.PURGE.RED")
45 .setTitle("Purge")
46 .setDescription("Select how many messages to delete")
47 .setStatus("Danger")
48 ],
49 components: [],
50 ephemeral: true,
51 fetchReply: true
52 })
pineafan5d1908e2022-02-28 21:34:47 +000053 let deleted = [] as Discord.Message[]
pineafan8b4b17f2022-02-27 20:42:52 +000054 while (true) {
55 let m = await interaction.editReply({
56 embeds: [
pineafan4edb7762022-06-26 19:21:04 +010057 new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000058 .setEmoji("CHANNEL.PURGE.RED")
59 .setTitle("Purge")
60 .setDescription("Select how many messages to delete. You can continue clicking until all messages are cleared.")
61 .setStatus("Danger")
62 ],
63 components: [
64 new Discord.MessageActionRow().addComponents([
65 new Discord.MessageButton()
66 .setCustomId("1")
67 .setLabel("1")
68 .setStyle("SECONDARY"),
69 new Discord.MessageButton()
70 .setCustomId("3")
71 .setLabel("3")
72 .setStyle("SECONDARY"),
73 new Discord.MessageButton()
74 .setCustomId("5")
75 .setLabel("5")
76 .setStyle("SECONDARY")
77 ]),
78 new Discord.MessageActionRow().addComponents([
79 new Discord.MessageButton()
80 .setCustomId("10")
81 .setLabel("10")
82 .setStyle("SECONDARY"),
83 new Discord.MessageButton()
84 .setCustomId("25")
85 .setLabel("25")
86 .setStyle("SECONDARY"),
87 new Discord.MessageButton()
88 .setCustomId("50")
89 .setLabel("50")
90 .setStyle("SECONDARY")
91 ]),
92 new Discord.MessageActionRow().addComponents([
93 new Discord.MessageButton()
94 .setCustomId("done")
95 .setLabel("Done")
96 .setStyle("SUCCESS")
97 .setEmoji(getEmojiByName("CONTROL.TICK", "id"))
98 ])
99 ]
100 })
101 let component;
102 try {
pineafanc6158ab2022-06-17 16:34:07 +0100103 component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 300000});
pineafan8b4b17f2022-02-27 20:42:52 +0000104 } catch (e) { break; }
105 component.deferUpdate();
106 if (component.customId === "done") break;
107 let amount;
108 try { amount = parseInt(component.customId); } catch { break; }
pineafan5d1908e2022-02-28 21:34:47 +0000109 let messages;
pineafane625d782022-05-09 18:04:32 +0100110 await (interaction.channel as TextChannel).messages.fetch({limit: amount}).then(async (ms) => {
pineafan5d1908e2022-02-28 21:34:47 +0000111 if (user) {
112 ms = ms.filter(m => m.author.id === user.id)
113 }
114 messages = await (channel as TextChannel).bulkDelete(ms, true);
115 })
pineafane625d782022-05-09 18:04:32 +0100116 if (messages) {
117 deleted = deleted.concat(messages.map(m => m))
118 }
pineafan8b4b17f2022-02-27 20:42:52 +0000119 }
120 if (deleted.length === 0) return await interaction.editReply({
121 embeds: [
pineafan4edb7762022-06-26 19:21:04 +0100122 new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000123 .setEmoji("CHANNEL.PURGE.RED")
124 .setTitle("Purge")
125 .setDescription("No messages were deleted")
126 .setStatus("Danger")
127 ],
128 components: []
129 })
pineafan4edb7762022-06-26 19:21:04 +0100130 if (user) {
131 try { await client.database.history.create("purge", interaction.guild.id, user, interaction.options.getString("reason"), null, null, deleted.length) } catch {}
132 }
pineafan5d1908e2022-02-28 21:34:47 +0000133 let attachmentObject;
134 try {
pineafan4edb7762022-06-26 19:21:04 +0100135 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger
pineafane625d782022-05-09 18:04:32 +0100136 let data = {
137 meta: {
138 type: 'channelPurge',
139 displayName: 'Channel Purged',
140 calculateType: 'messageDelete',
141 color: NucleusColors.red,
142 emoji: "PUNISH.BAN.RED",
143 timestamp: new Date().getTime()
144 },
145 list: {
pineafanda6e5342022-07-03 10:03:16 +0100146 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
pineafane625d782022-05-09 18:04:32 +0100147 purgedBy: entry(interaction.user.id, renderUser(interaction.user)),
148 channel: entry(interaction.channel.id, renderChannel(interaction.channel)),
149 messagesCleared: entry(deleted.length, deleted.length),
150 },
151 hidden: {
152 guild: interaction.guild.id
153 }
154 }
pineafan4edb7762022-06-26 19:21:04 +0100155 log(data);
pineafan5d1908e2022-02-28 21:34:47 +0000156 let out = ""
157 deleted.reverse().forEach(message => {
158 out += `${message.author.username}#${message.author.discriminator} (${message.author.id}) [${new Date(message.createdTimestamp).toISOString()}]\n`
159 let lines = message.content.split("\n")
160 lines.forEach(line => {out += `> ${line}\n`})
161 out += `\n\n`
162 })
163 attachmentObject = {
164 attachment: Buffer.from(out),
165 name: `purge-${channel.id}-${Date.now()}.txt`,
166 description: "Purge log"
167 }
168 } catch {}
pineafan4edb7762022-06-26 19:21:04 +0100169 let m = await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000170 .setEmoji(`CHANNEL.PURGE.GREEN`)
171 .setTitle(`Purge`)
172 .setDescription("Messages cleared")
173 .setStatus("Success")
174 ], components: [new Discord.MessageActionRow().addComponents([
175 new Discord.MessageButton()
176 .setCustomId("download")
177 .setLabel("Download transcript")
178 .setStyle("SUCCESS")
179 .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id"))
180 ])]})
181 let component;
182 try {
pineafanc6158ab2022-06-17 16:34:07 +0100183 component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 300000});
pineafan5d1908e2022-02-28 21:34:47 +0000184 } catch {}
185 if (component && component.customId === "download") {
pineafan4edb7762022-06-26 19:21:04 +0100186 interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000187 .setEmoji("CHANNEL.PURGE.GREEN")
188 .setTitle(`Purge`)
189 .setDescription("Uploaded")
190 .setStatus("Success")
191 ], components: [], files: [attachmentObject]})
192 } else {
pineafan4edb7762022-06-26 19:21:04 +0100193 interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000194 .setEmoji("CHANNEL.PURGE.GREEN")
195 .setTitle(`Purge`)
196 .setDescription("Messages cleared")
197 .setStatus("Success")
198 ], components: []})
199 }
200 return
pineafan8b4b17f2022-02-27 20:42:52 +0000201 } else {
pineafan377794f2022-04-18 19:01:01 +0100202 let confirmation = await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +0000203 .setEmoji("CHANNEL.PURGE.RED")
204 .setTitle("Purge")
205 .setDescription(keyValueList({
pineafane625d782022-05-09 18:04:32 +0100206 "channel": `<#${channel.id}>`,
pineafan8b4b17f2022-02-27 20:42:52 +0000207 "amount": interaction.options.getInteger("amount").toString(),
208 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
209 }))
210 .setColor("Danger")
pineafan377794f2022-04-18 19:01:01 +0100211 .send()
pineafan02ba0232022-07-24 22:16:15 +0100212 if (confirmation.cancelled) return
pineafan377794f2022-04-18 19:01:01 +0100213 if (confirmation.success) {
pineafan5d1908e2022-02-28 21:34:47 +0000214 let messages;
pineafan8b4b17f2022-02-27 20:42:52 +0000215 try {
pineafan377794f2022-04-18 19:01:01 +0100216 if (!user) {
217 let toDelete = await (interaction.channel as TextChannel).messages.fetch({limit: interaction.options.getInteger("amount")})
218 messages = await (channel as TextChannel).bulkDelete(toDelete, true);
219 } else {
220 let toDelete = (await (await (interaction.channel as TextChannel).messages.fetch({limit: 100}))
221 .filter(m => m.author.id === user.id)).first(interaction.options.getInteger("amount"))
222 messages = await (channel as TextChannel).bulkDelete(toDelete, true);
223 }
pineafan5d1908e2022-02-28 21:34:47 +0000224 } catch(e) {
pineafan4edb7762022-06-26 19:21:04 +0100225 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000226 .setEmoji("CHANNEL.PURGE.RED")
227 .setTitle(`Purge`)
228 .setDescription("Something went wrong and no messages were deleted")
229 .setStatus("Danger")
230 ], components: []})
231 }
pineafan4edb7762022-06-26 19:21:04 +0100232 if (user) {
233 try { await client.database.history.create("purge", interaction.guild.id, user, interaction.options.getString("reason"), null, null, messages.size) } catch {}
234 }
pineafan5d1908e2022-02-28 21:34:47 +0000235 let attachmentObject;
236 try {
pineafan4edb7762022-06-26 19:21:04 +0100237 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger
pineafane625d782022-05-09 18:04:32 +0100238 let data = {
239 meta: {
240 type: 'channelPurge',
241 displayName: 'Channel Purged',
242 calculateType: 'messageDelete',
243 color: NucleusColors.red,
244 emoji: "PUNISH.BAN.RED",
245 timestamp: new Date().getTime()
246 },
247 list: {
pineafanda6e5342022-07-03 10:03:16 +0100248 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
pineafane625d782022-05-09 18:04:32 +0100249 purgedBy: entry(interaction.user.id, renderUser(interaction.user)),
250 channel: entry(interaction.channel.id, renderChannel(interaction.channel)),
251 messagesCleared: entry(messages.size, messages.size),
252 },
253 hidden: {
254 guild: interaction.guild.id
255 }
256 }
pineafan4edb7762022-06-26 19:21:04 +0100257 log(data);
pineafan5d1908e2022-02-28 21:34:47 +0000258 let out = ""
259 messages.reverse().forEach(message => {
260 out += `${message.author.username}#${message.author.discriminator} (${message.author.id}) [${new Date(message.createdTimestamp).toISOString()}]\n`
261 let lines = message.content.split("\n")
262 lines.forEach(line => {out += `> ${line}\n`})
263 out += `\n\n`
264 })
265 attachmentObject = {
266 attachment: Buffer.from(out),
267 name: `purge-${channel.id}-${Date.now()}.txt`,
268 description: `Purge log`
269 }
270 } catch {}
pineafan4edb7762022-06-26 19:21:04 +0100271 let m = await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000272 .setEmoji(`CHANNEL.PURGE.GREEN`)
273 .setTitle(`Purge`)
274 .setDescription("Messages cleared")
275 .setStatus("Success")
276 ], components: [new Discord.MessageActionRow().addComponents([
277 new Discord.MessageButton()
278 .setCustomId("download")
279 .setLabel("Download transcript")
280 .setStyle("SUCCESS")
281 .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id"))
282 ])]})
283 let component;
284 try {
pineafanc6158ab2022-06-17 16:34:07 +0100285 component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 300000});
pineafan5d1908e2022-02-28 21:34:47 +0000286 } catch {}
287 if (component && component.customId === "download") {
pineafan4edb7762022-06-26 19:21:04 +0100288 interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000289 .setEmoji("CHANNEL.PURGE.GREEN")
290 .setTitle(`Purge`)
pineafan377794f2022-04-18 19:01:01 +0100291 .setDescription("Transcript uploaded above")
pineafan5d1908e2022-02-28 21:34:47 +0000292 .setStatus("Success")
293 ], components: [], files: [attachmentObject]})
294 } else {
pineafan4edb7762022-06-26 19:21:04 +0100295 interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000296 .setEmoji("CHANNEL.PURGE.GREEN")
297 .setTitle(`Purge`)
298 .setDescription("Messages cleared")
299 .setStatus("Success")
300 ], components: []})
301 }
pineafan8b4b17f2022-02-27 20:42:52 +0000302 } else {
pineafan4edb7762022-06-26 19:21:04 +0100303 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000304 .setEmoji("CHANNEL.PURGE.GREEN")
305 .setTitle(`Purge`)
306 .setDescription("No changes were made")
307 .setStatus("Success")
308 ], components: []})
309 }
310 }
pineafan4f164f32022-02-26 22:07:12 +0000311}
312
313const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan663dc472022-05-10 18:13:47 +0100314 let member = (interaction.member as GuildMember)
PineappleFan5fe720d2022-05-19 12:01:49 +0100315 let me = (interaction.guild.me as GuildMember)
pineafan8b4b17f2022-02-27 20:42:52 +0000316 // Allow the owner to purge
pineafan663dc472022-05-10 18:13:47 +0100317 if (member.id == interaction.guild.ownerId) return true
pineafan8b4b17f2022-02-27 20:42:52 +0000318 // Check if the user has manage_messages permission
pineafan4edb7762022-06-26 19:21:04 +0100319 if (! member.permissions.has("MANAGE_MESSAGES")) throw "You do not have the Manage messages permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000320 // Check if nucleus has the manage_messages permission
pineafan4edb7762022-06-26 19:21:04 +0100321 if (! me.permissions.has("MANAGE_MESSAGES")) throw "I do not have the Manage messages permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000322 // Allow warn
323 return true
pineafan4f164f32022-02-26 22:07:12 +0000324}
325
pineafan8b4b17f2022-02-27 20:42:52 +0000326export { command, callback, check };