/[sudobot]/branches/2.x/src/commands/moderation/ClearCommand.ts
ViewVC logotype

Annotation of /branches/2.x/src/commands/moderation/ClearCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26