/[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 117 - (hide annotations)
Mon Jul 29 17:28:39 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 7061 byte(s)
Improved message deletion
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    
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 rakin 78 let user: User | undefined | null;
33     let msgCount = 0, channel: GuildChannel = msg.channel! as GuildChannel;
34 rakin 51
35     if (options.isInteraction) {
36 rakin 78 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 rakin 51 }
57     else {
58     try {
59 rakin 78 user = await getUser(client, msg as Message, options);
60 rakin 51
61 rakin 78 if (!user) {
62 rakin 51 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 rakin 78
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 rakin 51
89 rakin 78 return;
90     }
91    
92 rakin 51 let count = 0;
93 rakin 78 (global as any).deletingMessages = true;
94 rakin 51
95 rakin 78 let message = await msg.reply({
96 rakin 51 embeds: [
97     new MessageEmbed()
98     .setColor('GOLD')
99 rakin 78 .setDescription((await fetchEmoji('loading'))?.toString() + ' Deleting messages...')
100 rakin 51 ]
101     });
102    
103 rakin 78 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 rakin 80 fetched = await fetched.filter(m => m.author.id === user!.id && m.id !== message!.id && (Date.now() - m.createdTimestamp) <= (2 * 7 * 24 * 60 * 60));
114 rakin 78 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 rakin 51 }
120 rakin 78 else {
121     let fetched = 0;
122     let safeLimit = 0, safeLimit2 = 0;
123 rakin 51
124 rakin 78 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 rakin 80 if (message!.id === m.id || (Date.now() - m.createdTimestamp) > (2 * 7 * 24 * 60 * 60 * 1000))
145 rakin 78 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 rakin 51 const messageOptions = {
179     embeds: [
180     new MessageEmbed()
181     .setColor('GREEN')
182 rakin 78 .setDescription((await fetchEmoji('check') as Emoji).toString() + " Deleted " + count + " message(s)" + (user ? " from user " + user.tag : ''))
183 rakin 51 ]
184     };
185    
186     if (msg instanceof CommandInteraction) {
187     await msg.editReply(messageOptions);
188     }
189     else {
190     await message!.edit(messageOptions);
191     }
192 rakin 78
193 rakin 108 setTimeout(async () => {
194 rakin 117 try {
195     if (msg instanceof Message)
196     await msg.delete();
197     }
198     catch (e) {
199     console.log(e);
200     }
201    
202     try {
203     await message!.delete();
204     }
205     catch (e) {
206     console.log(e);
207     }
208 rakin 108 }, 5500);
209    
210 rakin 78 (global as any).deletingMessages = false;
211 rakin 51 }
212     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26