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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 153 - (show annotations)
Mon Jul 29 17:28:48 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 7593 byte(s)
Add Non-moderable role (#34)

* feat(utils): add shouldNotModerate() function

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26