Skip to content

Commit 21f9eb2

Browse files
committed
Merge branch 'main' of https://github.com/Kkkermit/Testify
2 parents 0e3febe + 7513631 commit 21f9eb2

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/commands/LightModeration/clear.js

+26-14
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,8 @@ module.exports = {
2424
}
2525

2626
await interaction.deferReply({ ephemeral: true });
27-
28-
let messages;
29-
if (user) {
30-
messages = await interaction.channel.messages.fetch()
31-
.then(messages => messages.filter(m => m.author.id === user.id))
32-
.then(messages => messages.first(parseInt(amount)));
33-
} else {
34-
messages = await interaction.channel.messages.fetch({ limit: parseInt(amount) });
35-
}
36-
37-
await interaction.channel.bulkDelete(messages, true);
38-
39-
const deletedMessages = await interaction.channel.bulkDelete(messages, true);
4027

41-
const deletedSize = deletedMessages.size;
28+
const deletedSize = await deleteMessages(interaction.channel, parseInt(amount), user);
4229

4330
const deletedUser = user ? user.username : 'everyone';
4431

@@ -54,4 +41,29 @@ module.exports = {
5441
return interaction.followUp({ embeds: [clearEmbed], ephemeral: true });
5542

5643
}
44+
}
45+
46+
async function deleteMessages(channel, totalToDelete, user) {
47+
let remaining = totalToDelete;
48+
let count;
49+
if (user) {
50+
let messages;
51+
while (remaining > 0) {
52+
messages = await channel.messages.fetch()
53+
.then(messages => messages.filter(m => m.author.id === user.id))
54+
.then(messages => messages.first(parseInt(remaining)));
55+
const deleteCount = remaining > 100 ? 100 : remaining;
56+
// the true here forces the bot to delete msgs that older then 14 days.
57+
count += await channel.bulkDelete(messages, true);
58+
remaining -= deleteCount;
59+
}
60+
return count;
61+
}
62+
while (remaining > 0) {
63+
const deleteCount = remaining > 100 ? 100 : remaining;
64+
// the true here forces the bot to delete msgs that older then 14 days.
65+
count += await channel.bulkDelete(deleteCount, true);
66+
remaining -= deleteCount;
67+
}
68+
return count;
5769
}

0 commit comments

Comments
 (0)