/[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 186 - (show annotations)
Mon Jul 29 17:28:56 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 7923 byte(s)
fix: mention reading strategy (#41)

* fix(commands): ban related commands not working when banning users outside of the server

* fix(utils): reading users/members in the wrong way
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 }
110 }
111
112 let count = 0;
113 (global as any).deletingMessages = true;
114
115 let message = await msg.reply({
116 embeds: [
117 new MessageEmbed()
118 .setColor('GOLD')
119 .setDescription((await fetchEmoji('loading'))?.toString() + ' Deleting messages...')
120 ]
121 });
122
123 if (msg instanceof CommandInteraction)
124 message = <Message> await msg.fetchReply();
125
126 if (msgCount === 0 && user) {
127 console.log(user?.tag);
128
129 let fetched;
130
131 do {
132 fetched = await (channel as TextChannel).messages.fetch({ limit: 100 });
133 fetched = await fetched.filter(m => m.author.id === user!.id && m.id !== message!.id && (Date.now() - m.createdTimestamp) <= (2 * 7 * 24 * 60 * 60 * 1000));
134 await (channel as TextChannel).bulkDelete(fetched);
135 count += fetched.size;
136
137 /*for await (const [id, m] of fetched.entries()) {
138 try {
139 await m.delete();
140 count++;
141 }
142 catch (e) {
143 console.log('Error deleting message', e);
144 }
145 }
146 */
147
148 await new Promise(r => setTimeout(r, 900));
149 }
150 while (fetched.size >= 2);
151 }
152 else {
153 let fetched = 0;
154 let safeLimit = 0, safeLimit2 = 0;
155
156 do {
157 if (count >= msgCount || safeLimit >= 50) {
158 break;
159 }
160
161 try {
162 const data = await (channel as TextChannel).messages.fetch({ limit: 100 });
163
164 fetched = 0;
165
166 for await (const [id, m] of data.entries()) {
167 try {
168 if (count >= msgCount || safeLimit2 > 200) {
169 break;
170 }
171
172 if (user && m.author?.id !== user?.id) {
173 continue;
174 }
175
176 if (message!.id === m.id || (Date.now() - m.createdTimestamp) > (2 * 7 * 24 * 60 * 60 * 1000))
177 continue;
178
179 if (m.deletable) {
180 console.log('here', user?.tag);
181
182 await m.delete();
183
184 fetched++;
185 count++;
186 safeLimit2++;
187 }
188
189 if (count % 10 === 0) {
190 await new Promise(r => setTimeout(r, 1100));
191 }
192 }
193 catch(e) {
194 console.log(e);
195 safeLimit2 += 100;
196 }
197 }
198 }
199 catch(e) {
200 console.log(e);
201
202 break;
203 }
204
205 safeLimit++;
206 }
207 while (fetched >= 2);
208 }
209
210 const messageOptions = {
211 embeds: [
212 new MessageEmbed()
213 .setColor('GREEN')
214 .setDescription((await fetchEmoji('check') as Emoji).toString() + " Deleted " + count + " message(s)" + (user ? " from user " + user.tag : ''))
215 ]
216 };
217
218 if (msg instanceof CommandInteraction) {
219 await msg.editReply(messageOptions);
220 }
221 else {
222 await message!.edit(messageOptions);
223 }
224
225 setTimeout(async () => {
226 try {
227 if (msg instanceof Message)
228 await msg.delete();
229 }
230 catch (e) {
231 console.log(e);
232 }
233
234 try {
235 await message!.delete();
236 }
237 catch (e) {
238 console.log(e);
239 }
240 }, 5500);
241
242 (global as any).deletingMessages = false;
243 }
244 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26