/[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 78 - (hide annotations)
Mon Jul 29 17:28:30 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 6544 byte(s)
Upgraded clear command
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     fetched = await fetched.filter(m => m.author.id === user!.id && m.id !== message!.id);
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 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     if (message!.id === m.id)
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    
164     safeLimit2 += 100;
165     }
166     }
167     }
168     catch(e) {
169     console.log(e);
170    
171     break;
172     }
173    
174     safeLimit++;
175     }
176     while (fetched >= 2);
177     }
178    
179 rakin 51 const messageOptions = {
180     embeds: [
181     new MessageEmbed()
182     .setColor('GREEN')
183 rakin 78 .setDescription((await fetchEmoji('check') as Emoji).toString() + " Deleted " + count + " message(s)" + (user ? " from user " + user.tag : ''))
184 rakin 51 ]
185     };
186    
187     if (msg instanceof CommandInteraction) {
188     await msg.editReply(messageOptions);
189     }
190     else {
191     await message!.edit(messageOptions);
192     }
193 rakin 78
194     (global as any).deletingMessages = false;
195 rakin 51 }
196     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26