/[sudobot]/trunk/src/commands/moderation/ClearCommand.ts
ViewVC logotype

Contents of /trunk/src/commands/moderation/ClearCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 182 - (show annotations)
Mon Jul 29 17:28:55 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 7941 byte(s)
fix(commamds): clear command not working without message count
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 import { shouldNotModerate } from '../../utils/util';
12
13 export default class ClearCommand extends BaseCommand {
14 supportsInteractions: boolean = true;
15
16 constructor() {
17 super('clear', 'moderation', []);
18 }
19
20 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
21 if (!options.isInteraction && options.args[0] === undefined) {
22 await msg.reply({
23 embeds: [
24 new MessageEmbed()
25 .setColor('#f14a60')
26 .setDescription('This command requires at least one argument.')
27 ]
28 });
29
30 return;
31 }
32
33 let user: User | undefined | null;
34 let msgCount = 0, channel: GuildChannel = msg.channel! as GuildChannel;
35
36 if (options.isInteraction) {
37 if (options.options.getUser('user'))
38 user = <User> options.options.getUser('user');
39
40 console.log(user?.tag);
41
42 if (options.options.getChannel('channel')) {
43 channel = <GuildChannel> options.options.getChannel('channel');
44
45 if (channel.type !== 'GUILD_TEXT' && channel.type !== 'GUILD_NEWS' && channel.type !== 'GUILD_PUBLIC_THREAD' && channel.type !== 'GUILD_PRIVATE_THREAD') {
46 await msg.reply({
47 content: 'Invalid channel given.'
48 });
49
50 return;
51 }
52 }
53
54 if (options.options.getInteger('count')) {
55 msgCount = <number> options.options.getInteger('count');
56 }
57 }
58 else {
59 try {
60 user = await getUser(client, msg as Message, options);
61
62 if (!user) {
63 throw new Error();
64 }
65 }
66 catch (e) {
67 console.log(e);
68
69 await msg.reply({
70 embeds: [
71 new MessageEmbed()
72 .setColor('#f14a60')
73 .setDescription('Invalid user given.')
74 ]
75 });
76
77 return;
78 }
79 }
80
81 if (msgCount === 0 && !user) {
82 await msg.reply({
83 embeds: [
84 new MessageEmbed()
85 .setColor('#f14a60')
86 .setDescription('You have to specify either the message count or the user.')
87 ]
88 });
89
90 return;
91 }
92
93 if (user) {
94 try {
95 const member = await msg.guild?.members.fetch(user.id);
96
97 if (member && shouldNotModerate(client, member)) {
98 await msg.reply({
99 embeds: [
100 { description: "Cannot clear messages from this user: Operation not permitted" }
101 ]
102 });
103
104 return;
105 }
106 }
107 catch (e) {
108 console.log(e);
109 return;
110 }
111 }
112
113 let count = 0;
114 (global as any).deletingMessages = true;
115
116 let message = await msg.reply({
117 embeds: [
118 new MessageEmbed()
119 .setColor('GOLD')
120 .setDescription((await fetchEmoji('loading'))?.toString() + ' Deleting messages...')
121 ]
122 });
123
124 if (msg instanceof CommandInteraction)
125 message = <Message> await msg.fetchReply();
126
127 if (msgCount === 0 && user) {
128 console.log(user?.tag);
129
130 let fetched;
131
132 do {
133 fetched = await (channel as TextChannel).messages.fetch({ limit: 100 });
134 fetched = await fetched.filter(m => m.author.id === user!.id && m.id !== message!.id && (Date.now() - m.createdTimestamp) <= (2 * 7 * 24 * 60 * 60 * 1000));
135 await (channel as TextChannel).bulkDelete(fetched);
136 count += fetched.size;
137
138 /*for await (const [id, m] of fetched.entries()) {
139 try {
140 await m.delete();
141 count++;
142 }
143 catch (e) {
144 console.log('Error deleting message', e);
145 }
146 }
147 */
148
149 await new Promise(r => setTimeout(r, 900));
150 }
151 while (fetched.size >= 2);
152 }
153 else {
154 let fetched = 0;
155 let safeLimit = 0, safeLimit2 = 0;
156
157 do {
158 if (count >= msgCount || safeLimit >= 50) {
159 break;
160 }
161
162 try {
163 const data = await (channel as TextChannel).messages.fetch({ limit: 100 });
164
165 fetched = 0;
166
167 for await (const [id, m] of data.entries()) {
168 try {
169 if (count >= msgCount || safeLimit2 > 200) {
170 break;
171 }
172
173 if (user && m.author?.id !== user?.id) {
174 continue;
175 }
176
177 if (message!.id === m.id || (Date.now() - m.createdTimestamp) > (2 * 7 * 24 * 60 * 60 * 1000))
178 continue;
179
180 if (m.deletable) {
181 console.log('here', user?.tag);
182
183 await m.delete();
184
185 fetched++;
186 count++;
187 safeLimit2++;
188 }
189
190 if (count % 10 === 0) {
191 await new Promise(r => setTimeout(r, 1100));
192 }
193 }
194 catch(e) {
195 console.log(e);
196 safeLimit2 += 100;
197 }
198 }
199 }
200 catch(e) {
201 console.log(e);
202
203 break;
204 }
205
206 safeLimit++;
207 }
208 while (fetched >= 2);
209 }
210
211 const messageOptions = {
212 embeds: [
213 new MessageEmbed()
214 .setColor('GREEN')
215 .setDescription((await fetchEmoji('check') as Emoji).toString() + " Deleted " + count + " message(s)" + (user ? " from user " + user.tag : ''))
216 ]
217 };
218
219 if (msg instanceof CommandInteraction) {
220 await msg.editReply(messageOptions);
221 }
222 else {
223 await message!.edit(messageOptions);
224 }
225
226 setTimeout(async () => {
227 try {
228 if (msg instanceof Message)
229 await msg.delete();
230 }
231 catch (e) {
232 console.log(e);
233 }
234
235 try {
236 await message!.delete();
237 }
238 catch (e) {
239 console.log(e);
240 }
241 }, 5500);
242
243 (global as any).deletingMessages = false;
244 }
245 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26