forked from Galactic/galactic-bot
actually fixed it
This commit is contained in:
parent
ae0f251d5e
commit
449a6dc408
@ -108,6 +108,10 @@ class InteractionWrapper {
|
||||
// return this.client.localeLoader.format(language, index, parameters, code);
|
||||
// }
|
||||
|
||||
get id() {
|
||||
return this.interaction.id;
|
||||
}
|
||||
|
||||
get guildId() {
|
||||
return this.interaction.guildId;
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
const { Infraction } = require('../interfaces/');
|
||||
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;
|
||||
@ -30,7 +29,7 @@ class PruneInfraction extends Infraction {
|
||||
hyperlink: opts.hyperlink
|
||||
});
|
||||
|
||||
this.channel = opts.target;
|
||||
// this.channel = opts.target;
|
||||
}
|
||||
|
||||
}
|
||||
@ -41,27 +40,18 @@ class PruneInfraction extends Infraction {
|
||||
// +1 to so we can avoid deleting the command interaction
|
||||
let messages = await this.fetchMessages(message, amount < 100 ? amount + 1 : amount); //Collection, not array.
|
||||
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() + 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})`);
|
||||
|
||||
// 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}`);
|
||||
messages = messages.filter((m) => m.deletable && m.interaction?.id !== this.messageId);
|
||||
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}, 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);
|
||||
this.client.logger.debug(`Deleted ${deleted} messages`);
|
||||
if (deleted === 0) return this._fail('C_PRUNE_NODELETE');
|
||||
|
||||
await this.handle();
|
||||
@ -79,7 +69,7 @@ class PruneInfraction extends Infraction {
|
||||
const bulkDelete = async (messages) => {
|
||||
try {
|
||||
const deleteThese = messages.splice(0, 100);
|
||||
const result = await this.channel.bulkDelete(deleteThese, true); //Filtering old just incase, d.js uses Snowflake times instead of our Client's timestamp.
|
||||
const result = await this.target.bulkDelete(deleteThese, true); //Filtering old just incase, d.js uses Snowflake times instead of our Client's timestamp.
|
||||
if (result && result.size > 0) amount += result.size;
|
||||
} catch (error) {
|
||||
this.client.logger.error(error.stack);
|
||||
@ -186,14 +176,15 @@ class PruneInfraction extends Infraction {
|
||||
async fetchMessages(message, amount) {
|
||||
|
||||
let fetched = new Collection();
|
||||
const msgMgr = this.channel.messages;
|
||||
const msgMgr = this.target.messages;
|
||||
const after = this.arguments.after?.value || null;
|
||||
|
||||
const fetch = async (lastMessage, amount) => {
|
||||
const messages = await msgMgr.fetch({
|
||||
limit: amount > 100 ? 100 : amount,
|
||||
before: lastMessage,
|
||||
after
|
||||
after,
|
||||
cache: true // So deleted messages get logged
|
||||
});
|
||||
|
||||
fetched = fetched.concat(messages);
|
||||
@ -215,10 +206,10 @@ class PruneInfraction extends Infraction {
|
||||
async verify() {
|
||||
|
||||
const me = await this.guild.resolveMember(this.client.user);
|
||||
const perms = this.channel.permissionsFor(me);
|
||||
const perms = this.target.permissionsFor(me);
|
||||
if (perms.missing('ViewChannel', 'ReadMessageHistory').length) return this._fail('C_PRUNE_MISSING_ACCESS');
|
||||
if (perms.missing('ManageMessages').length) return this._fail('C_PRUNE_INSUFFICIENTPERMISSIONS');
|
||||
if (!this.channel.messages) return this._fail('C_PRUNE_NON_TEXTCHANNEL');
|
||||
if (!this.target.messages) return this._fail('C_PRUNE_NON_TEXTCHANNEL');
|
||||
|
||||
return super._verify();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user