Fix bot not sending infraction in DMs when removing from server #21

Merged
Navy.gif merged 2 commits from development into alpha 2024-10-23 12:39:49 +02:00
18 changed files with 27 additions and 36 deletions

View File

@ -575,7 +575,18 @@ class ModerationManager implements Initialisable, CallbackClient
response.infraction.totalPoints = await this.calculatePoints(targetUser, guild); response.infraction.totalPoints = await this.calculatePoints(targetUser, guild);
response.infraction.totalPoints += points; response.infraction.totalPoints += points;
} }
const dmBefore = [ 'BAN', 'KICK', 'SOFTBAN' ].includes(infraction.type);
if (dmBefore)
await infraction.handle();
const result = await response.infraction.execute(); 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 // Infraction doesn't have an ID until it is executed, hence this after execute
// if (response.infraction.targetType === 'USER') // if (response.infraction.targetType === 'USER')
@ -638,7 +649,7 @@ class ModerationManager implements Initialisable, CallbackClient
throw new Error(`[inf: ${infraction.id}] Missing executor ${infraction.executor}`); throw new Error(`[inf: ${infraction.id}] Missing executor ${infraction.executor}`);
try try
{ {
const result = await new undoClass(this.#client, this.#logger, { const inf = new undoClass(this.#client, this.#logger, {
type: undoClass.Type, type: undoClass.Type,
reason: `AUTO-${Constants.InfractionOpposites[infraction.type]} from Case ${infraction.case}`, reason: `AUTO-${Constants.InfractionOpposites[infraction.type]} from Case ${infraction.case}`,
channel, channel,
@ -648,9 +659,18 @@ class ModerationManager implements Initialisable, CallbackClient
guild, guild,
target, target,
executor 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) if (result.error)
this.#logger.error(result); this.#logger.error(result);
else
// Finally save the infraction to the db
await inf.save();
} }
catch (err) catch (err)
{ {

View File

@ -89,7 +89,6 @@ class AddroleInfraction extends Infraction
return this._fail('INFRACTION_ERROR'); return this._fail('INFRACTION_ERROR');
} }
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -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}`); 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'); return this._fail('INFRACTION_ERROR');
} }
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -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}`); 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'); return this._fail('INFRACTION_ERROR');
} }
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -183,10 +183,7 @@ class LockdownInfraction extends Infraction
}); });
await permissions.set(newOverwrites, this._reason); await permissions.set(newOverwrites, this._reason);
await this.handle();
return this._succeed(); return this._succeed();
} }
async verify () async verify ()
@ -196,7 +193,6 @@ class LockdownInfraction extends Infraction
if (!perms || missing?.length) if (!perms || missing?.length)
return this._fail(this.guild.format('INFRACTION_LOCKDOWN_MISSING_PERMS', { missing: missing?.join('**, **') || '' }), null, true); return this._fail(this.guild.format('INFRACTION_LOCKDOWN_MISSING_PERMS', { missing: missing?.join('**, **') || '' }), null, true);
return super._verify(); return super._verify();
} }
} }

View File

@ -171,8 +171,6 @@ class MuteInfraction extends Infraction
this.client.moderation.removeCallback(callback.payload!); this.client.moderation.removeCallback(callback.payload!);
} }
// if(callbacks.size > 0) callbacks.map((c) => this.client.moderationManager._removeExpiration(c));
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -97,7 +97,6 @@ class NicknameInfraction extends Infraction
return this._fail('INFRACTION_ERROR'); return this._fail('INFRACTION_ERROR');
} }
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -60,7 +60,6 @@ class NoteInfraction extends Infraction
async execute () async execute ()
{ {
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -91,7 +91,6 @@ class PruneInfraction extends Infraction
if (deleted === 0) if (deleted === 0)
return this._fail('C_PRUNE_NODELETE'); return this._fail('C_PRUNE_NODELETE');
await this.handle();
return this._succeed(); return this._succeed();
// return this._succeed('C_PRUNE_AMOUNT', { // return this._succeed('C_PRUNE_AMOUNT', {
// hasOld: deleted > amount ? hasOld : false, // hasOld: deleted > amount ? hasOld : false,

View File

@ -79,7 +79,6 @@ class RemoveroleInfraction extends Infraction
return this._fail('INFRACTION_ERROR'); return this._fail('INFRACTION_ERROR');
} }
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -76,7 +76,6 @@ class SlowmodeInfraction extends Infraction
return this._fail('INFRACTION_ERROR'); return this._fail('INFRACTION_ERROR');
} }
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -76,7 +76,6 @@ class SoftbanInfraction extends Infraction
{ {
return this._fail('INFRACTION_ERROR'); return this._fail('INFRACTION_ERROR');
} }
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -76,7 +76,6 @@ class UnbanInfraction extends Infraction
if (callbacks.length > 0) if (callbacks.length > 0)
callbacks.map((c) => this.client.moderation.removeCallback(c.payload!)); callbacks.map((c) => this.client.moderation.removeCallback(c.payload!));
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -125,7 +125,6 @@ class UnlockdownInfraction extends Infraction
if (latest) if (latest)
await this.client.moderation.removeCallback(latest); await this.client.moderation.removeCallback(latest);
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -66,7 +66,6 @@ class UnmuteInfraction extends Infraction
} }
} }
async execute () async execute ()
{ {
let removedRoles: Snowflake[] | null = [], let removedRoles: Snowflake[] | null = [],
@ -108,7 +107,7 @@ class UnmuteInfraction extends Infraction
// Idk which would be the ideal solution, // Idk which would be the ideal solution,
// to attempt removing the default role anyway or throw an error due to unexpected state // to attempt removing the default role anyway or throw an error due to unexpected state
// doing this for now // doing this for now
if (!role) if (!role && muteType && muteType < 2)
{ {
if (!settings.mute?.role) if (!settings.mute?.role)
return this._fail('COMMAND_MUTE_NOMUTEROLE', true); return this._fail('COMMAND_MUTE_NOMUTEROLE', true);
@ -192,7 +191,6 @@ class UnmuteInfraction extends Infraction
if (callback) if (callback)
this.client.moderation.removeCallback(callback.payload!); this.client.moderation.removeCallback(callback.payload!);
await this.handle();
return this._succeed(); return this._succeed();
} }
} }

View File

@ -37,7 +37,6 @@ class VckickInfraction extends Infraction
constructor (client: DiscordClient, logger: LoggerClient, opts: VcKickData) constructor (client: DiscordClient, logger: LoggerClient, opts: VcKickData)
{ {
if (opts.fetched) if (opts.fetched)
super(client, logger, opts); super(client, logger, opts);
else else
@ -63,12 +62,10 @@ class VckickInfraction extends Infraction
throw new Error('Guild member required'); throw new Error('Guild member required');
this.member = opts.target; this.member = opts.target;
} }
} }
async execute () async execute ()
{ {
try try
{ {
await this.member!.voice.disconnect(this._reason); await this.member!.voice.disconnect(this._reason);
@ -78,18 +75,14 @@ class VckickInfraction extends Infraction
return this._fail('INFRACTION_ERROR'); return this._fail('INFRACTION_ERROR');
} }
await this.handle();
return this._succeed(); return this._succeed();
} }
async verify () async verify ()
{ {
if (!this.member!.voice.channel) if (!this.member!.voice.channel)
return this._fail('C_VCKICK_NOCHANNEL'); return this._fail('C_VCKICK_NOCHANNEL');
return super._verify(); return super._verify();
} }
} }

View File

@ -66,7 +66,6 @@ class WarnInfraction extends Infraction
async execute () async execute ()
{ {
await this.handle();
return this._succeed(); return this._succeed();
} }

View File

@ -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) if (this.#duration)
await this.#client.moderation.handleTimedInfraction(this.json); await this.#client.moderation.handleTimedInfraction(this.json);
return this.save();
} }
execute (): Promise<InfractionSuccess | InfractionFail> execute (): Promise<InfractionSuccess | InfractionFail>
@ -281,6 +275,10 @@ class Infraction
async save () 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 { json } = this;
const filter: {id: string, _id?: ObjectId} = { id: this.id }; const filter: {id: string, _id?: ObjectId} = { id: this.id };
if (this.#mongoId) if (this.#mongoId)