From 34cd19edb038b29fd81789e53644290511ce987f Mon Sep 17 00:00:00 2001 From: Navy Date: Wed, 23 Oct 2024 13:36:59 +0300 Subject: [PATCH] Fix bot not sending infraction in DMs when removing from server --- .../components/managers/ModerationManager.ts | 24 +++++++++++++++++-- src/client/infractions/Addrole.ts | 1 - src/client/infractions/Ban.ts | 1 - src/client/infractions/Kick.ts | 1 - src/client/infractions/Lockdown.ts | 4 ---- src/client/infractions/Mute.ts | 2 -- src/client/infractions/Nickname.ts | 1 - src/client/infractions/Note.ts | 1 - src/client/infractions/Prune.ts | 1 - src/client/infractions/Removerole.ts | 1 - src/client/infractions/Slowmode.ts | 1 - src/client/infractions/Softban.ts | 1 - src/client/infractions/Unban.ts | 1 - src/client/infractions/Unlockdown.ts | 1 - src/client/infractions/Unmute.ts | 4 +--- src/client/infractions/Vckick.ts | 7 ------ src/client/infractions/Warn.ts | 1 - src/client/interfaces/Infraction.ts | 10 ++++---- 18 files changed, 27 insertions(+), 36 deletions(-) diff --git a/src/client/components/managers/ModerationManager.ts b/src/client/components/managers/ModerationManager.ts index df1f19d..1806e48 100644 --- a/src/client/components/managers/ModerationManager.ts +++ b/src/client/components/managers/ModerationManager.ts @@ -575,7 +575,18 @@ class ModerationManager implements Initialisable, CallbackClient response.infraction.totalPoints = await this.calculatePoints(targetUser, guild); response.infraction.totalPoints += points; } + + const dmBefore = [ 'BAN', 'KICK', 'SOFTBAN' ].includes(infraction.type); + + if (dmBefore) + await infraction.handle(); const result = await response.infraction.execute(); + if (!result.error) + { + if (!dmBefore) + await infraction.handle(); + await infraction.save(); + } // Infraction doesn't have an ID until it is executed, hence this after execute // if (response.infraction.targetType === 'USER') @@ -638,7 +649,7 @@ class ModerationManager implements Initialisable, CallbackClient throw new Error(`[inf: ${infraction.id}] Missing executor ${infraction.executor}`); try { - const result = await new undoClass(this.#client, this.#logger, { + const inf = new undoClass(this.#client, this.#logger, { type: undoClass.Type, reason: `AUTO-${Constants.InfractionOpposites[infraction.type]} from Case ${infraction.case}`, channel, @@ -648,9 +659,18 @@ class ModerationManager implements Initialisable, CallbackClient guild, target, executor - }).execute(); + }); + + // Logging, must be done before execute so DMs are sent before they're removed from the server + await inf.handle(); + + // Carry out infraction + const result = await inf.execute(); if (result.error) this.#logger.error(result); + else + // Finally save the infraction to the db + await inf.save(); } catch (err) { diff --git a/src/client/infractions/Addrole.ts b/src/client/infractions/Addrole.ts index a69dfa4..9aff3a3 100644 --- a/src/client/infractions/Addrole.ts +++ b/src/client/infractions/Addrole.ts @@ -89,7 +89,6 @@ class AddroleInfraction extends Infraction return this._fail('INFRACTION_ERROR'); } - await this.handle(); return this._succeed(); } diff --git a/src/client/infractions/Ban.ts b/src/client/infractions/Ban.ts index d655105..1de4bd2 100644 --- a/src/client/infractions/Ban.ts +++ b/src/client/infractions/Ban.ts @@ -84,7 +84,6 @@ class BanInfraction extends Infraction this.logger.error(`Failed to ban ${(this.target as UserWrapper).tag} (${this.target!.id}) from ${this.guild.name} (${this.guild.id})\n${error}`); return this._fail('INFRACTION_ERROR'); } - await this.handle(); return this._succeed(); } diff --git a/src/client/infractions/Kick.ts b/src/client/infractions/Kick.ts index 95ab6a7..cdc4a5d 100644 --- a/src/client/infractions/Kick.ts +++ b/src/client/infractions/Kick.ts @@ -76,7 +76,6 @@ class KickInfraction extends Infraction this.logger.error(`Failed to kick ${(this.target as UserWrapper).tag} (${this.target!.id}) from ${this.guild.name} (${this.guild.id})\n${error}`); return this._fail('INFRACTION_ERROR'); } - await this.handle(); return this._succeed(); } diff --git a/src/client/infractions/Lockdown.ts b/src/client/infractions/Lockdown.ts index 8a00e95..863fe68 100644 --- a/src/client/infractions/Lockdown.ts +++ b/src/client/infractions/Lockdown.ts @@ -183,10 +183,7 @@ class LockdownInfraction extends Infraction }); await permissions.set(newOverwrites, this._reason); - - await this.handle(); return this._succeed(); - } async verify () @@ -196,7 +193,6 @@ class LockdownInfraction extends Infraction if (!perms || missing?.length) return this._fail(this.guild.format('INFRACTION_LOCKDOWN_MISSING_PERMS', { missing: missing?.join('**, **') || '' }), null, true); return super._verify(); - } } diff --git a/src/client/infractions/Mute.ts b/src/client/infractions/Mute.ts index efb4e30..3e2849d 100644 --- a/src/client/infractions/Mute.ts +++ b/src/client/infractions/Mute.ts @@ -171,8 +171,6 @@ class MuteInfraction extends Infraction this.client.moderation.removeCallback(callback.payload!); } - // if(callbacks.size > 0) callbacks.map((c) => this.client.moderationManager._removeExpiration(c)); - await this.handle(); return this._succeed(); } diff --git a/src/client/infractions/Nickname.ts b/src/client/infractions/Nickname.ts index d0c38e5..81214ba 100644 --- a/src/client/infractions/Nickname.ts +++ b/src/client/infractions/Nickname.ts @@ -97,7 +97,6 @@ class NicknameInfraction extends Infraction return this._fail('INFRACTION_ERROR'); } - await this.handle(); return this._succeed(); } diff --git a/src/client/infractions/Note.ts b/src/client/infractions/Note.ts index 31ec985..7e07cfd 100644 --- a/src/client/infractions/Note.ts +++ b/src/client/infractions/Note.ts @@ -60,7 +60,6 @@ class NoteInfraction extends Infraction async execute () { - await this.handle(); return this._succeed(); } diff --git a/src/client/infractions/Prune.ts b/src/client/infractions/Prune.ts index beeef96..66ef107 100644 --- a/src/client/infractions/Prune.ts +++ b/src/client/infractions/Prune.ts @@ -91,7 +91,6 @@ class PruneInfraction extends Infraction if (deleted === 0) return this._fail('C_PRUNE_NODELETE'); - await this.handle(); return this._succeed(); // return this._succeed('C_PRUNE_AMOUNT', { // hasOld: deleted > amount ? hasOld : false, diff --git a/src/client/infractions/Removerole.ts b/src/client/infractions/Removerole.ts index 26c4428..64df193 100644 --- a/src/client/infractions/Removerole.ts +++ b/src/client/infractions/Removerole.ts @@ -79,7 +79,6 @@ class RemoveroleInfraction extends Infraction return this._fail('INFRACTION_ERROR'); } - await this.handle(); return this._succeed(); } diff --git a/src/client/infractions/Slowmode.ts b/src/client/infractions/Slowmode.ts index 645b78f..fd15d8f 100644 --- a/src/client/infractions/Slowmode.ts +++ b/src/client/infractions/Slowmode.ts @@ -76,7 +76,6 @@ class SlowmodeInfraction extends Infraction return this._fail('INFRACTION_ERROR'); } - await this.handle(); return this._succeed(); } diff --git a/src/client/infractions/Softban.ts b/src/client/infractions/Softban.ts index 8596669..aa3225f 100644 --- a/src/client/infractions/Softban.ts +++ b/src/client/infractions/Softban.ts @@ -76,7 +76,6 @@ class SoftbanInfraction extends Infraction { return this._fail('INFRACTION_ERROR'); } - await this.handle(); return this._succeed(); } diff --git a/src/client/infractions/Unban.ts b/src/client/infractions/Unban.ts index a809a13..d671d14 100644 --- a/src/client/infractions/Unban.ts +++ b/src/client/infractions/Unban.ts @@ -76,7 +76,6 @@ class UnbanInfraction extends Infraction if (callbacks.length > 0) callbacks.map((c) => this.client.moderation.removeCallback(c.payload!)); - await this.handle(); return this._succeed(); } diff --git a/src/client/infractions/Unlockdown.ts b/src/client/infractions/Unlockdown.ts index 54026ed..33b58e9 100644 --- a/src/client/infractions/Unlockdown.ts +++ b/src/client/infractions/Unlockdown.ts @@ -125,7 +125,6 @@ class UnlockdownInfraction extends Infraction if (latest) await this.client.moderation.removeCallback(latest); - await this.handle(); return this._succeed(); } diff --git a/src/client/infractions/Unmute.ts b/src/client/infractions/Unmute.ts index f483396..5b4aa89 100644 --- a/src/client/infractions/Unmute.ts +++ b/src/client/infractions/Unmute.ts @@ -66,7 +66,6 @@ class UnmuteInfraction extends Infraction } } - async execute () { let removedRoles: Snowflake[] | null = [], @@ -108,7 +107,7 @@ class UnmuteInfraction extends Infraction // Idk which would be the ideal solution, // to attempt removing the default role anyway or throw an error due to unexpected state // doing this for now - if (!role) + if (!role && muteType && muteType < 2) { if (!settings.mute?.role) return this._fail('COMMAND_MUTE_NOMUTEROLE', true); @@ -192,7 +191,6 @@ class UnmuteInfraction extends Infraction if (callback) this.client.moderation.removeCallback(callback.payload!); - await this.handle(); return this._succeed(); } } diff --git a/src/client/infractions/Vckick.ts b/src/client/infractions/Vckick.ts index bb2caf2..50bd3ac 100644 --- a/src/client/infractions/Vckick.ts +++ b/src/client/infractions/Vckick.ts @@ -37,7 +37,6 @@ class VckickInfraction extends Infraction constructor (client: DiscordClient, logger: LoggerClient, opts: VcKickData) { - if (opts.fetched) super(client, logger, opts); else @@ -63,12 +62,10 @@ class VckickInfraction extends Infraction throw new Error('Guild member required'); this.member = opts.target; } - } async execute () { - try { await this.member!.voice.disconnect(this._reason); @@ -78,18 +75,14 @@ class VckickInfraction extends Infraction return this._fail('INFRACTION_ERROR'); } - await this.handle(); return this._succeed(); - } async verify () { - if (!this.member!.voice.channel) return this._fail('C_VCKICK_NOCHANNEL'); return super._verify(); - } } diff --git a/src/client/infractions/Warn.ts b/src/client/infractions/Warn.ts index 1552216..a9a2e81 100644 --- a/src/client/infractions/Warn.ts +++ b/src/client/infractions/Warn.ts @@ -66,7 +66,6 @@ class WarnInfraction extends Infraction async execute () { - await this.handle(); return this._succeed(); } diff --git a/src/client/interfaces/Infraction.ts b/src/client/interfaces/Infraction.ts index 6ae1d82..66ada15 100644 --- a/src/client/interfaces/Infraction.ts +++ b/src/client/interfaces/Infraction.ts @@ -264,14 +264,8 @@ class Infraction } } - // Remove the role structures as they will cause problems when serialising for database - if (this.#data.roles) - delete this.#data.roles; - if (this.#duration) await this.#client.moderation.handleTimedInfraction(this.json); - - return this.save(); } execute (): Promise @@ -281,6 +275,10 @@ class Infraction async save () { + // Remove the role structures as they will cause problems when serialising for database + if (this.#data.roles) + delete this.#data.roles; + const { json } = this; const filter: {id: string, _id?: ObjectId} = { id: this.id }; if (this.#mongoId) -- 2.45.2