1 |
import { BanOptions, CommandInteraction, Emoji, GuildChannel, GuildMember, Interaction, Message, TextChannel, User } from 'discord.js'; |
2 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
3 |
import DiscordClient from '../../client/Client'; |
4 |
import CommandOptions from '../../types/CommandOptions'; |
5 |
import InteractionOptions from '../../types/InteractionOptions'; |
6 |
import MessageEmbed from '../../client/MessageEmbed'; |
7 |
import getUser from '../../utils/getUser'; |
8 |
import getMember from '../../utils/getMember'; |
9 |
import History from '../../automod/History'; |
10 |
import { fetchEmoji } from '../../utils/Emoji'; |
11 |
|
12 |
export default class ClearCommand extends BaseCommand { |
13 |
supportsInteractions: boolean = true; |
14 |
|
15 |
constructor() { |
16 |
super('clear', 'moderation', []); |
17 |
} |
18 |
|
19 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
20 |
if (!options.isInteraction && options.args[0] === undefined) { |
21 |
await msg.reply({ |
22 |
embeds: [ |
23 |
new MessageEmbed() |
24 |
.setColor('#f14a60') |
25 |
.setDescription('This command requires at least one argument.') |
26 |
] |
27 |
}); |
28 |
|
29 |
return; |
30 |
} |
31 |
|
32 |
let user: User | undefined | null; |
33 |
let msgCount = 0, channel: GuildChannel = msg.channel! as GuildChannel; |
34 |
|
35 |
if (options.isInteraction) { |
36 |
if (options.options.getUser('user')) |
37 |
user = <User> options.options.getUser('user'); |
38 |
|
39 |
console.log(user?.tag); |
40 |
|
41 |
if (options.options.getChannel('channel')) { |
42 |
channel = <GuildChannel> options.options.getChannel('channel'); |
43 |
|
44 |
if (channel.type !== 'GUILD_TEXT' && channel.type !== 'GUILD_NEWS' && channel.type !== 'GUILD_PUBLIC_THREAD' && channel.type !== 'GUILD_PRIVATE_THREAD') { |
45 |
await msg.reply({ |
46 |
content: 'Invalid channel given.' |
47 |
}); |
48 |
|
49 |
return; |
50 |
} |
51 |
} |
52 |
|
53 |
if (options.options.getInteger('count')) { |
54 |
msgCount = <number> options.options.getInteger('count'); |
55 |
} |
56 |
} |
57 |
else { |
58 |
try { |
59 |
user = await getUser(client, msg as Message, options); |
60 |
|
61 |
if (!user) { |
62 |
throw new Error(); |
63 |
} |
64 |
} |
65 |
catch (e) { |
66 |
console.log(e); |
67 |
|
68 |
await msg.reply({ |
69 |
embeds: [ |
70 |
new MessageEmbed() |
71 |
.setColor('#f14a60') |
72 |
.setDescription('Invalid user given.') |
73 |
] |
74 |
}); |
75 |
|
76 |
return; |
77 |
} |
78 |
} |
79 |
|
80 |
if (msgCount === 0 && !user) { |
81 |
await msg.reply({ |
82 |
embeds: [ |
83 |
new MessageEmbed() |
84 |
.setColor('#f14a60') |
85 |
.setDescription('You have to specify either the message count or the user.') |
86 |
] |
87 |
}); |
88 |
|
89 |
return; |
90 |
} |
91 |
|
92 |
let count = 0; |
93 |
(global as any).deletingMessages = true; |
94 |
|
95 |
let message = await msg.reply({ |
96 |
embeds: [ |
97 |
new MessageEmbed() |
98 |
.setColor('GOLD') |
99 |
.setDescription((await fetchEmoji('loading'))?.toString() + ' Deleting messages...') |
100 |
] |
101 |
}); |
102 |
|
103 |
if (msg instanceof CommandInteraction) |
104 |
message = <Message> await msg.fetchReply(); |
105 |
|
106 |
if (msgCount === 0 && user) { |
107 |
console.log(user?.tag); |
108 |
|
109 |
let fetched; |
110 |
|
111 |
do { |
112 |
fetched = await (channel as TextChannel).messages.fetch({ limit: 100 }); |
113 |
fetched = await fetched.filter(m => m.author.id === user!.id && m.id !== message!.id && (Date.now() - m.createdTimestamp) <= (2 * 7 * 24 * 60 * 60)); |
114 |
await (channel as TextChannel).bulkDelete(fetched); |
115 |
await new Promise(r => setTimeout(r, 900)); |
116 |
count += await fetched.size; |
117 |
} |
118 |
while (fetched.size >= 2); |
119 |
} |
120 |
else { |
121 |
let fetched = 0; |
122 |
let safeLimit = 0, safeLimit2 = 0; |
123 |
|
124 |
do { |
125 |
if (count >= msgCount || safeLimit >= 50) { |
126 |
break; |
127 |
} |
128 |
|
129 |
try { |
130 |
const data = await (channel as TextChannel).messages.fetch({ limit: 100 }); |
131 |
|
132 |
fetched = 0; |
133 |
|
134 |
for await (const [id, m] of data.entries()) { |
135 |
try { |
136 |
if (count >= msgCount || safeLimit2 > 200) { |
137 |
break; |
138 |
} |
139 |
|
140 |
if (user && m.author?.id !== user?.id) { |
141 |
continue; |
142 |
} |
143 |
|
144 |
if (message!.id === m.id || (Date.now() - m.createdTimestamp) > (2 * 7 * 24 * 60 * 60 * 1000)) |
145 |
continue; |
146 |
|
147 |
if (m.deletable) { |
148 |
console.log('here', user?.tag); |
149 |
|
150 |
await m.delete(); |
151 |
|
152 |
fetched++; |
153 |
count++; |
154 |
safeLimit2++; |
155 |
} |
156 |
|
157 |
if (count % 10 === 0) { |
158 |
await new Promise(r => setTimeout(r, 1100)); |
159 |
} |
160 |
} |
161 |
catch(e) { |
162 |
console.log(e); |
163 |
safeLimit2 += 100; |
164 |
} |
165 |
} |
166 |
} |
167 |
catch(e) { |
168 |
console.log(e); |
169 |
|
170 |
break; |
171 |
} |
172 |
|
173 |
safeLimit++; |
174 |
} |
175 |
while (fetched >= 2); |
176 |
} |
177 |
|
178 |
const messageOptions = { |
179 |
embeds: [ |
180 |
new MessageEmbed() |
181 |
.setColor('GREEN') |
182 |
.setDescription((await fetchEmoji('check') as Emoji).toString() + " Deleted " + count + " message(s)" + (user ? " from user " + user.tag : '')) |
183 |
] |
184 |
}; |
185 |
|
186 |
if (msg instanceof CommandInteraction) { |
187 |
await msg.editReply(messageOptions); |
188 |
} |
189 |
else { |
190 |
await message!.edit(messageOptions); |
191 |
} |
192 |
|
193 |
setTimeout(async () => { |
194 |
if (msg instanceof Message) |
195 |
await msg.delete(); |
196 |
|
197 |
await message!.delete(); |
198 |
}, 5500); |
199 |
|
200 |
(global as any).deletingMessages = false; |
201 |
} |
202 |
} |