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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 80 by rakin, Mon Jul 29 17:28:30 2024 UTC revision 344 by rakin, Mon Jul 29 17:29:40 2024 UTC
# Line 1  Line 1 
1  import { BanOptions, CommandInteraction, Emoji, GuildChannel, GuildMember, Interaction, Message, TextChannel, User } from 'discord.js';  import { CommandInteraction, Emoji, GuildChannel, Message, TextChannel, User, Permissions } from 'discord.js';
2  import BaseCommand from '../../utils/structures/BaseCommand';  import BaseCommand from '../../utils/structures/BaseCommand';
3  import DiscordClient from '../../client/Client';  import DiscordClient from '../../client/Client';
4  import CommandOptions from '../../types/CommandOptions';  import CommandOptions from '../../types/CommandOptions';
5  import InteractionOptions from '../../types/InteractionOptions';  import InteractionOptions from '../../types/InteractionOptions';
6  import MessageEmbed from '../../client/MessageEmbed';  import MessageEmbed from '../../client/MessageEmbed';
7  import getUser from '../../utils/getUser';  import getUser from '../../utils/getUser';
 import getMember from '../../utils/getMember';  
 import History from '../../automod/History';  
8  import { fetchEmoji } from '../../utils/Emoji';  import { fetchEmoji } from '../../utils/Emoji';
9    import { hasPermission, shouldNotModerate } from '../../utils/util';
10    
11  export default class ClearCommand extends BaseCommand {  export default class ClearCommand extends BaseCommand {
12      supportsInteractions: boolean = true;      supportsInteractions: boolean = true;
13        permissions = [Permissions.FLAGS.MANAGE_MESSAGES];
14    
15      constructor() {      constructor() {
16          super('clear', 'moderation', []);          super('clear', 'moderation', []);
# Line 89  export default class ClearCommand extend Line 89  export default class ClearCommand extend
89              return;              return;
90          }          }
91    
92            if (user) {
93                try {
94                    const member = await msg.guild?.members.fetch(user.id);
95    
96                    if (member && !(await hasPermission(client, member, msg, null, "You don't have permission to clear messages from this user.")))
97                        return;
98    
99                    if (member && shouldNotModerate(client, member)) {
100                        await msg.reply({
101                            embeds: [
102                                { description: "Cannot clear messages from this user: Operation not permitted" }
103                            ]
104                        });
105                        
106                        return;
107                    }
108                }
109                catch (e) {
110                    console.log(e);
111                }
112            }
113    
114          let count = 0;          let count = 0;
115          (global as any).deletingMessages = true;          (global as any).deletingMessages = true;
116    
# Line 110  export default class ClearCommand extend Line 132  export default class ClearCommand extend
132    
133              do {              do {
134                  fetched = await (channel as TextChannel).messages.fetch({ limit: 100 });                  fetched = await (channel as TextChannel).messages.fetch({ limit: 100 });
135                  fetched = await fetched.filter(m => m.author.id === user!.id && m.id !== message!.id && (Date.now() - m.createdTimestamp) <= (2 * 7 * 24 * 60 * 60));                  fetched = await fetched.filter(m => m.author.id === user!.id && m.id !== message!.id && (Date.now() - m.createdTimestamp) <= (2 * 7 * 24 * 60 * 60 * 1000));
136                  await (channel as TextChannel).bulkDelete(fetched);                  await (channel as TextChannel).bulkDelete(fetched);
137                    count += fetched.size;
138    
139                    /*for await (const [id, m] of fetched.entries()) {
140                        try {
141                            await m.delete();
142                            count++;
143                        }
144                        catch (e) {
145                            console.log('Error deleting message', e);
146                        }
147                    }
148                    */
149                    
150                  await new Promise(r => setTimeout(r, 900));                  await new Promise(r => setTimeout(r, 900));
                 count += await fetched.size;  
151              }              }
152              while (fetched.size >= 2);              while (fetched.size >= 2);
153          }          }
# Line 190  export default class ClearCommand extend Line 224  export default class ClearCommand extend
224              await message!.edit(messageOptions);              await message!.edit(messageOptions);
225          }          }
226    
227            setTimeout(async () => {
228                try {
229                    if (msg instanceof Message)
230                        await msg.delete();
231                }
232                catch (e) {
233                    console.log(e);                
234                }
235                
236                try {
237                    await message!.delete();
238                }
239                catch (e) {
240                    console.log(e);                
241                }
242            }, 5500);
243    
244          (global as any).deletingMessages = false;          (global as any).deletingMessages = false;
245      }      }
 }  
246    }

Legend:
Removed from v.80  
changed lines
  Added in v.344

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26