From f2db7f016c9a495957cd997b7cb604acd9deeaec Mon Sep 17 00:00:00 2001 From: Macintosh II <95250141+mctaylors@users.noreply.github.com> Date: Wed, 27 Sep 2023 21:25:49 +0300 Subject: [PATCH] Fix UnknownMember warning flood (#115) If a user was muted using the `MuteRole` method and then banned, the UnknownMember warning will flood your logs when `DateTimeOffset.UtcNow > data.MutedUntil` becomes true, because there is no user in the server to unmute. --------- Signed-off-by: Macintosh II <95250141+mctaylors@users.noreply.github.com> --- src/Services/Update/MemberUpdateService.cs | 67 ++++++++++++---------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/src/Services/Update/MemberUpdateService.cs b/src/Services/Update/MemberUpdateService.cs index d4424ec..3e87b79 100644 --- a/src/Services/Update/MemberUpdateService.cs +++ b/src/Services/Update/MemberUpdateService.cs @@ -80,8 +80,17 @@ public sealed partial class MemberUpdateService : BackgroundService var failedResults = new List(); var id = data.Id.ToSnowflake(); - var punishmentsResult = await CheckMemberPunishmentsAsync(guildId, id, data, ct); - failedResults.AddIfFailed(punishmentsResult); + var autoUnbanResult = await TryAutoUnbanAsync(guildId, id, data, ct); + failedResults.AddIfFailed(autoUnbanResult); + + var guildMemberResult = await _guildApi.GetGuildMemberAsync(guildId, id, ct); + if (!guildMemberResult.IsDefined(out var guildMember)) + { + return failedResults.AggregateErrors(); + } + + var autoUnmuteResult = await TryAutoUnmuteAsync(guildId, id, data, ct); + failedResults.AddIfFailed(autoUnmuteResult); if (defaultRole.Value is not 0 && !data.Roles.Contains(defaultRole.Value)) { @@ -90,12 +99,6 @@ public sealed partial class MemberUpdateService : BackgroundService failedResults.AddIfFailed(addResult); } - var guildMemberResult = await _guildApi.GetGuildMemberAsync(guildId, id, ct); - if (!guildMemberResult.IsDefined(out var guildMember)) - { - return failedResults.AggregateErrors(); - } - if (!guildMember.User.IsDefined(out var user)) { failedResults.AddIfFailed(new ArgumentNullError(nameof(guildMember.User))); @@ -117,35 +120,41 @@ public sealed partial class MemberUpdateService : BackgroundService return failedResults.AggregateErrors(); } - private async Task CheckMemberPunishmentsAsync( + private async Task TryAutoUnbanAsync( Snowflake guildId, Snowflake id, MemberData data, CancellationToken ct) { - if (DateTimeOffset.UtcNow > data.BannedUntil) + if (DateTimeOffset.UtcNow <= data.BannedUntil) { - var unbanResult = await _guildApi.RemoveGuildBanAsync( - guildId, id, Messages.PunishmentExpired.EncodeHeader(), ct); - if (unbanResult.IsSuccess) - { - data.BannedUntil = null; - } - - return unbanResult; + return Result.FromSuccess(); } - if (DateTimeOffset.UtcNow > data.MutedUntil) + var unbanResult = await _guildApi.RemoveGuildBanAsync( + guildId, id, Messages.PunishmentExpired.EncodeHeader(), ct); + if (unbanResult.IsSuccess) { - var unmuteResult = await _guildApi.ModifyGuildMemberAsync( - guildId, id, roles: data.Roles.ConvertAll(r => r.ToSnowflake()), - reason: Messages.PunishmentExpired.EncodeHeader(), ct: ct); - if (unmuteResult.IsSuccess) - { - data.MutedUntil = null; - } - - return unmuteResult; + data.BannedUntil = null; } - return Result.FromSuccess(); + return unbanResult; + } + + private async Task TryAutoUnmuteAsync( + Snowflake guildId, Snowflake id, MemberData data, CancellationToken ct) + { + if (DateTimeOffset.UtcNow <= data.MutedUntil) + { + return Result.FromSuccess(); + } + + var unmuteResult = await _guildApi.ModifyGuildMemberAsync( + guildId, id, roles: data.Roles.ConvertAll(r => r.ToSnowflake()), + reason: Messages.PunishmentExpired.EncodeHeader(), ct: ct); + if (unmuteResult.IsSuccess) + { + data.MutedUntil = null; + } + + return unmuteResult; } private async Task FilterNicknameAsync(Snowflake guildId, IUser user, IGuildMember member,