fix prune not pruning interaction responses

This commit is contained in:
Erik 2022-09-16 18:58:58 +03:00
parent 3b67b4b227
commit ae0f251d5e
Signed by untrusted user: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB

View File

@ -3,6 +3,7 @@ const { Collection } = require('@discordjs/collection');
const { MessageType } = require('discord.js');
const Arguments = ['users', 'bots', 'humans', 'contains', 'startswith', 'endswith', 'emojis', 'reactions', 'text', 'invites', 'links', 'emojis', 'reactions', 'images', 'attachments'];
const twoWeeks = 2 * 7 * 24 * 60 * 60 * 1000;
class PruneInfraction extends Infraction {
@ -42,15 +43,21 @@ class PruneInfraction extends Infraction {
if (messages.size === 0) return this._fail('C_PRUNE_NOTFETCHED');
this.client.logger.debug(`Pruning ${messages.size} in ${this.guild.name}`);
const hasOld = messages.some((m) => m.createdTimestamp >= Date.now() + 1209600000); //I hope Node.js can handle these large of numbers.. e_e (2 weeks)
const hasUndeletable = messages.some((m) => m.deletable);
this.client.logger.debug(`Has old: ${hasOld}, has undeletable: ${hasUndeletable}`);
const hasOld = messages.some((m) => m.createdTimestamp >= Date.now() + twoWeeks); //I hope Node.js can handle these large of numbers.. e_e (2 weeks)
const hasUndeletable = messages.some((m) => !m.deletable);
this.client.logger.debug(`Has old: ${hasOld} (${messages.filter((m) => m.createdTimestamp >= Date.now() + twoWeeks).size}), has undeletable: ${hasUndeletable} (${messages.filter((m) => !m.deletable).size})`);
messages = messages.filter((m) => m.deletable && m.type !== MessageType.ChatInputCommand);
// Deleting the interaction before it receives a response will cause an error
const ownLastMessage = messages.filter((m) => m.author.id === this.client.user.id && m.type === MessageType.ChatInputCommand)
.sort((a, b) => b.createdTimestamp - a.createdTimestamp)
.first();
messages = messages.filter((m) => m.deletable && m.id !== ownLastMessage?.id); // && m.type !== MessageType.ChatInputCommand);
this.client.logger.debug(`After deletable filter ${messages.filter((m) => m.deletable).size}`);
if (messages.size === 0) return this._fail('C_PRUNE_NOTDELETABLE');
this.client.logger.debug(messages.map((m) => m.author.bot));
const filtered = await this.filterMessages(messages);
this.client.logger.debug(`After filter: ${filtered.size}, pre filter: ${messages.size}`);
this.client.logger.debug(`After filter: ${filtered.size}, pre filter: ${messages.size}, old: ${filtered.filter((m) => m.createdTimestamp >= Date.now() + twoWeeks).size}`);
if (filtered.size === 0) return this._fail('C_PRUNE_NOFILTERRESULTS');
const deleted = await this.deleteMessages(filtered);
@ -111,19 +118,19 @@ class PruneInfraction extends Infraction {
},
OR: (m) => {
return (a.users ? a.users.value.map((u) => u.id).includes(m.author.id) : false)
|| (a.bots ? m.author.bot : false)
|| (a.humans ? !m.author.bot : false)
|| (a.bots?.value ? m.author.bot : false)
|| (a.humans?.value ? !m.author.bot : false)
|| (a.contains ? m.content.toLowerCase().includes(a.contains.value.toLowerCase()) : false)
|| (a.startswith ? m.content.toLowerCase().startsWith(a.startswith.value.toLowerCase()) : false)
|| (a.endswith ? m.content.toLowerCase().endsWith(a.endswith.value.toLowerCase()) : false)
|| (a.text ? m.content.length > 0 : false)
|| (a.invites ? (/discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/([\w-]{2,255})/iu).test(m.content) : false)
|| (a.text?.value ? m.content.length > 0 : false)
|| (a.invites?.value ? (/discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/([\w-]{2,255})/iu).test(m.content) : false)
// eslint-disable-next-line max-len
|| (a.links ? (/(https?:\/\/)?([a-z0-9-]{1,63}\.)?([a-z0-9-]{2,63})\.([a-z0-9-]{2,63})\.?([a-z0-9-]{2,63})?/giu).test(m.content) : false)
|| (a.emojis ? (/<?(a:)?:?(\w{2,32}):(\d{17,19})>?|:\w{2,32}:/u).test(m.content) : false) //does not support unicode emojis... might want to support? idk
|| (a.reactions ? m.reactions.cache.size > 0 : false)
|| (a.images ? m.attachments.some((a) => a.height && a.width) : false)
|| (a.attachments ? m.attachments.size > 0 : false);
|| (a.links?.value ? (/(https?:\/\/)?([a-z0-9-]{1,63}\.)?([a-z0-9-]{2,63})\.([a-z0-9-]{2,63})\.?([a-z0-9-]{2,63})?/giu).test(m.content) : false)
|| (a.emojis?.value ? (/<?(a:)?:?(\w{2,32}):(\d{17,19})>?|:\w{2,32}:/u).test(m.content) : false) //does not support unicode emojis... might want to support? idk
|| (a.reactions?.value ? m.reactions.cache.size > 0 : false)
|| (a.images?.value ? m.attachments.some((a) => a.height && a.width) : false)
|| (a.attachments?.value ? m.attachments.size > 0 : false);
},
NAND: (m) => {
return (a.users ? !a.users.value.map((u) => u.id).includes(m.author.id) : true)
@ -163,6 +170,7 @@ class PruneInfraction extends Infraction {
const method = a.not ? `N${type}` : type;
let found = false;
// Check if any filtering arguments were given
for (const arg of Object.keys(a)) {
if (Arguments.includes(arg)) found = true;
}