blob: a1c8fe3dce82eacd0c42afdaade6ae5bda6e486d [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";
pineafan377794f2022-04-18 19:01:01 +01005import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00006import keyValueList from "../../utils/generateKeyValueList.js";
7import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan377794f2022-04-18 19:01:01 +01008import { create, areTicketsEnabled } from "../../automations/createModActionTicket.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
pineafan8b4b17f2022-02-27 20:42:52 +000023const callback = async (interaction: CommandInteraction) => {
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: [
pineafan377794f2022-04-18 19:01:01 +010029 new generateEmojiEmbed()
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: [
pineafan377794f2022-04-18 19:01:01 +010043 new generateEmojiEmbed()
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: [
pineafan377794f2022-04-18 19:01:01 +010057 new generateEmojiEmbed()
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 {
103 component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 2.5 * 60 * 1000});
104 } 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: [
pineafan377794f2022-04-18 19:01:01 +0100122 new generateEmojiEmbed()
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 })
pineafan5d1908e2022-02-28 21:34:47 +0000130 let attachmentObject;
131 try {
pineafane625d782022-05-09 18:04:32 +0100132 // @ts-ignore
133 const { log, NucleusColors, entry, renderUser, renderChannel } = interaction.user.client.logger
134 let data = {
135 meta: {
136 type: 'channelPurge',
137 displayName: 'Channel Purged',
138 calculateType: 'messageDelete',
139 color: NucleusColors.red,
140 emoji: "PUNISH.BAN.RED",
141 timestamp: new Date().getTime()
142 },
143 list: {
144 id: entry(interaction.user.id, `\`${interaction.user.id}\``),
145 purgedBy: entry(interaction.user.id, renderUser(interaction.user)),
146 channel: entry(interaction.channel.id, renderChannel(interaction.channel)),
147 messagesCleared: entry(deleted.length, deleted.length),
148 },
149 hidden: {
150 guild: interaction.guild.id
151 }
152 }
153 log(data, interaction.user.client);
pineafan5d1908e2022-02-28 21:34:47 +0000154 let out = ""
155 deleted.reverse().forEach(message => {
156 out += `${message.author.username}#${message.author.discriminator} (${message.author.id}) [${new Date(message.createdTimestamp).toISOString()}]\n`
157 let lines = message.content.split("\n")
158 lines.forEach(line => {out += `> ${line}\n`})
159 out += `\n\n`
160 })
161 attachmentObject = {
162 attachment: Buffer.from(out),
163 name: `purge-${channel.id}-${Date.now()}.txt`,
164 description: "Purge log"
165 }
166 } catch {}
pineafan377794f2022-04-18 19:01:01 +0100167 let m = await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000168 .setEmoji(`CHANNEL.PURGE.GREEN`)
169 .setTitle(`Purge`)
170 .setDescription("Messages cleared")
171 .setStatus("Success")
172 ], components: [new Discord.MessageActionRow().addComponents([
173 new Discord.MessageButton()
174 .setCustomId("download")
175 .setLabel("Download transcript")
176 .setStyle("SUCCESS")
177 .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id"))
178 ])]})
179 let component;
180 try {
181 component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 2.5 * 60 * 1000});
182 } catch {}
183 if (component && component.customId === "download") {
pineafan377794f2022-04-18 19:01:01 +0100184 interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000185 .setEmoji("CHANNEL.PURGE.GREEN")
186 .setTitle(`Purge`)
187 .setDescription("Uploaded")
188 .setStatus("Success")
189 ], components: [], files: [attachmentObject]})
190 } else {
pineafan377794f2022-04-18 19:01:01 +0100191 interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000192 .setEmoji("CHANNEL.PURGE.GREEN")
193 .setTitle(`Purge`)
194 .setDescription("Messages cleared")
195 .setStatus("Success")
196 ], components: []})
197 }
198 return
pineafan8b4b17f2022-02-27 20:42:52 +0000199 } else {
pineafan377794f2022-04-18 19:01:01 +0100200 let confirmation = await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +0000201 .setEmoji("CHANNEL.PURGE.RED")
202 .setTitle("Purge")
203 .setDescription(keyValueList({
pineafane625d782022-05-09 18:04:32 +0100204 "channel": `<#${channel.id}>`,
pineafan8b4b17f2022-02-27 20:42:52 +0000205 "amount": interaction.options.getInteger("amount").toString(),
206 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
207 }))
208 .setColor("Danger")
209 // pluralize("day", interaction.options.getInteger("amount"))
210 // const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
pineafan377794f2022-04-18 19:01:01 +0100211 .send()
212 if (confirmation.success) {
pineafan5d1908e2022-02-28 21:34:47 +0000213 let messages;
pineafan8b4b17f2022-02-27 20:42:52 +0000214 try {
pineafan377794f2022-04-18 19:01:01 +0100215 if (!user) {
216 let toDelete = await (interaction.channel as TextChannel).messages.fetch({limit: interaction.options.getInteger("amount")})
217 messages = await (channel as TextChannel).bulkDelete(toDelete, true);
218 } else {
219 let toDelete = (await (await (interaction.channel as TextChannel).messages.fetch({limit: 100}))
220 .filter(m => m.author.id === user.id)).first(interaction.options.getInteger("amount"))
221 messages = await (channel as TextChannel).bulkDelete(toDelete, true);
222 }
pineafan5d1908e2022-02-28 21:34:47 +0000223 } catch(e) {
pineafan377794f2022-04-18 19:01:01 +0100224 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000225 .setEmoji("CHANNEL.PURGE.RED")
226 .setTitle(`Purge`)
227 .setDescription("Something went wrong and no messages were deleted")
228 .setStatus("Danger")
229 ], components: []})
230 }
pineafan5d1908e2022-02-28 21:34:47 +0000231 let attachmentObject;
232 try {
pineafane625d782022-05-09 18:04:32 +0100233 // @ts-ignore
234 const { log, NucleusColors, entry, renderUser, renderChannel } = interaction.user.client.logger
235 let data = {
236 meta: {
237 type: 'channelPurge',
238 displayName: 'Channel Purged',
239 calculateType: 'messageDelete',
240 color: NucleusColors.red,
241 emoji: "PUNISH.BAN.RED",
242 timestamp: new Date().getTime()
243 },
244 list: {
245 id: entry(interaction.user.id, `\`${interaction.user.id}\``),
246 purgedBy: entry(interaction.user.id, renderUser(interaction.user)),
247 channel: entry(interaction.channel.id, renderChannel(interaction.channel)),
248 messagesCleared: entry(messages.size, messages.size),
249 },
250 hidden: {
251 guild: interaction.guild.id
252 }
253 }
254 log(data, interaction.user.client);
pineafan5d1908e2022-02-28 21:34:47 +0000255 let out = ""
256 messages.reverse().forEach(message => {
257 out += `${message.author.username}#${message.author.discriminator} (${message.author.id}) [${new Date(message.createdTimestamp).toISOString()}]\n`
258 let lines = message.content.split("\n")
259 lines.forEach(line => {out += `> ${line}\n`})
260 out += `\n\n`
261 })
262 attachmentObject = {
263 attachment: Buffer.from(out),
264 name: `purge-${channel.id}-${Date.now()}.txt`,
265 description: `Purge log`
266 }
267 } catch {}
pineafan377794f2022-04-18 19:01:01 +0100268 let m = await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000269 .setEmoji(`CHANNEL.PURGE.GREEN`)
270 .setTitle(`Purge`)
271 .setDescription("Messages cleared")
272 .setStatus("Success")
273 ], components: [new Discord.MessageActionRow().addComponents([
274 new Discord.MessageButton()
275 .setCustomId("download")
276 .setLabel("Download transcript")
277 .setStyle("SUCCESS")
278 .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id"))
279 ])]})
280 let component;
281 try {
282 component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 2.5 * 60 * 1000});
283 } catch {}
284 if (component && component.customId === "download") {
pineafan377794f2022-04-18 19:01:01 +0100285 interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000286 .setEmoji("CHANNEL.PURGE.GREEN")
287 .setTitle(`Purge`)
pineafan377794f2022-04-18 19:01:01 +0100288 .setDescription("Transcript uploaded above")
pineafan5d1908e2022-02-28 21:34:47 +0000289 .setStatus("Success")
290 ], components: [], files: [attachmentObject]})
291 } else {
pineafan377794f2022-04-18 19:01:01 +0100292 interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000293 .setEmoji("CHANNEL.PURGE.GREEN")
294 .setTitle(`Purge`)
295 .setDescription("Messages cleared")
296 .setStatus("Success")
297 ], components: []})
298 }
pineafan8b4b17f2022-02-27 20:42:52 +0000299 } else {
pineafan377794f2022-04-18 19:01:01 +0100300 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000301 .setEmoji("CHANNEL.PURGE.GREEN")
302 .setTitle(`Purge`)
303 .setDescription("No changes were made")
304 .setStatus("Success")
305 ], components: []})
306 }
307 }
pineafan4f164f32022-02-26 22:07:12 +0000308}
309
310const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan8b4b17f2022-02-27 20:42:52 +0000311 // Allow the owner to purge
312 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
313 // Check if the user has manage_messages permission
314 if (! (interaction.member as GuildMember).permissions.has("MANAGE_MESSAGES")) throw "You do not have the `manage_messages` permission";
315 // Check if nucleus has the manage_messages permission
316 if (! interaction.guild.me.permissions.has("MANAGE_MESSAGES")) throw "I do not have the `manage_messages` permission";
317 // Allow warn
318 return true
pineafan4f164f32022-02-26 22:07:12 +0000319}
320
pineafan8b4b17f2022-02-27 20:42:52 +0000321export { command, callback, check };