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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 182 - (hide 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 rakin 78 import { BanOptions, CommandInteraction, Emoji, GuildChannel, GuildMember, Interaction, Message, TextChannel, User } from 'discord.js';
2 rakin 51 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 rakin 153 import { shouldNotModerate } from '../../utils/util';
12 rakin 51
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 rakin 78 let user: User | undefined | null;
34     let msgCount = 0, channel: GuildChannel = msg.channel! as GuildChannel;
35 rakin 51
36     if (options.isInteraction) {
37 rakin 78 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 rakin 51 }
58     else {
59     try {
60 rakin 78 user = await getUser(client, msg as Message, options);
61 rakin 51
62 rakin 78 if (!user) {
63 rakin 51 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 rakin 78
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 rakin 51
90 rakin 78 return;
91     }
92    
93 rakin 153 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 rakin 51 let count = 0;
114 rakin 78 (global as any).deletingMessages = true;
115 rakin 51
116 rakin 78 let message = await msg.reply({
117 rakin 51 embeds: [
118     new MessageEmbed()
119     .setColor('GOLD')
120 rakin 78 .setDescription((await fetchEmoji('loading'))?.toString() + ' Deleting messages...')
121 rakin 51 ]
122     });
123    
124 rakin 78 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 rakin 182 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 rakin 78 await (channel as TextChannel).bulkDelete(fetched);
136 rakin 182 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 rakin 78 await new Promise(r => setTimeout(r, 900));
150     }
151     while (fetched.size >= 2);
152 rakin 51 }
153 rakin 78 else {
154     let fetched = 0;
155     let safeLimit = 0, safeLimit2 = 0;
156 rakin 51
157 rakin 78 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 rakin 80 if (message!.id === m.id || (Date.now() - m.createdTimestamp) > (2 * 7 * 24 * 60 * 60 * 1000))
178 rakin 78 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 rakin 51 const messageOptions = {
212     embeds: [
213     new MessageEmbed()
214     .setColor('GREEN')
215 rakin 78 .setDescription((await fetchEmoji('check') as Emoji).toString() + " Deleted " + count + " message(s)" + (user ? " from user " + user.tag : ''))
216 rakin 51 ]
217     };
218    
219     if (msg instanceof CommandInteraction) {
220     await msg.editReply(messageOptions);
221     }
222     else {
223     await message!.edit(messageOptions);
224     }
225 rakin 78
226 rakin 108 setTimeout(async () => {
227 rakin 117 try {
228     if (msg instanceof Message)
229     await msg.delete();
230     }
231     catch (e) {
232     console.log(e);
233     }
234 rakin 182
235 rakin 117 try {
236     await message!.delete();
237     }
238     catch (e) {
239     console.log(e);
240     }
241 rakin 108 }, 5500);
242    
243 rakin 78 (global as any).deletingMessages = false;
244 rakin 51 }
245 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26