/[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 51 - (hide annotations)
Mon Jul 29 17:28:23 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 3070 byte(s)
Release version 2.0
1 rakin 51 import { BanOptions, CommandInteraction, Emoji, GuildMember, Interaction, Message, TextChannel, User } 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    
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     let member: GuildMember | undefined | null;
33    
34     if (options.isInteraction) {
35     member = <GuildMember> await options.options.getMember('member');
36     }
37     else {
38     try {
39     member = await getMember(msg as Message, options);
40    
41     if (!member) {
42     throw new Error();
43     }
44     }
45     catch (e) {
46     console.log(e);
47    
48     await msg.reply({
49     embeds: [
50     new MessageEmbed()
51     .setColor('#f14a60')
52     .setDescription('Invalid user given.')
53     ]
54     });
55    
56     return;
57     }
58     }
59    
60     let fetched;
61     let count = 0;
62    
63     const message = await msg.reply({
64     embeds: [
65     new MessageEmbed()
66     .setColor('GOLD')
67     .setDescription('Deleting messages...')
68     ]
69     });
70    
71     do {
72     fetched = await msg.channel!.messages.fetch({ limit: 100 });
73     fetched = await fetched.filter(m => m.author.id === member!.id);
74     await (msg.channel as TextChannel).bulkDelete(fetched);
75     count += await fetched.size;
76     }
77     while (fetched.size >= 2);
78    
79     const messageOptions = {
80     embeds: [
81     new MessageEmbed()
82     .setColor('GREEN')
83     .setDescription((await fetchEmoji('check') as Emoji).toString() + " Deleted " + count + " message(s) from user " + member.user.tag)
84     ]
85     };
86    
87     if (msg instanceof CommandInteraction) {
88     await msg.editReply(messageOptions);
89     }
90     else {
91     await message!.edit(messageOptions);
92     }
93     }
94     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26