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>
This commit is contained in:
Macintxsh 2023-09-27 21:25:49 +03:00 committed by GitHub
parent 1ab5a640a9
commit f2db7f016c
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23

View file

@ -80,8 +80,17 @@ public sealed partial class MemberUpdateService : BackgroundService
var failedResults = new List<Result>(); var failedResults = new List<Result>();
var id = data.Id.ToSnowflake(); var id = data.Id.ToSnowflake();
var punishmentsResult = await CheckMemberPunishmentsAsync(guildId, id, data, ct); var autoUnbanResult = await TryAutoUnbanAsync(guildId, id, data, ct);
failedResults.AddIfFailed(punishmentsResult); 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)) if (defaultRole.Value is not 0 && !data.Roles.Contains(defaultRole.Value))
{ {
@ -90,12 +99,6 @@ public sealed partial class MemberUpdateService : BackgroundService
failedResults.AddIfFailed(addResult); 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)) if (!guildMember.User.IsDefined(out var user))
{ {
failedResults.AddIfFailed(new ArgumentNullError(nameof(guildMember.User))); failedResults.AddIfFailed(new ArgumentNullError(nameof(guildMember.User)));
@ -117,11 +120,14 @@ public sealed partial class MemberUpdateService : BackgroundService
return failedResults.AggregateErrors(); return failedResults.AggregateErrors();
} }
private async Task<Result> CheckMemberPunishmentsAsync( private async Task<Result> TryAutoUnbanAsync(
Snowflake guildId, Snowflake id, MemberData data, CancellationToken ct) Snowflake guildId, Snowflake id, MemberData data, CancellationToken ct)
{ {
if (DateTimeOffset.UtcNow > data.BannedUntil) if (DateTimeOffset.UtcNow <= data.BannedUntil)
{ {
return Result.FromSuccess();
}
var unbanResult = await _guildApi.RemoveGuildBanAsync( var unbanResult = await _guildApi.RemoveGuildBanAsync(
guildId, id, Messages.PunishmentExpired.EncodeHeader(), ct); guildId, id, Messages.PunishmentExpired.EncodeHeader(), ct);
if (unbanResult.IsSuccess) if (unbanResult.IsSuccess)
@ -132,8 +138,14 @@ public sealed partial class MemberUpdateService : BackgroundService
return unbanResult; return unbanResult;
} }
if (DateTimeOffset.UtcNow > data.MutedUntil) private async Task<Result> TryAutoUnmuteAsync(
Snowflake guildId, Snowflake id, MemberData data, CancellationToken ct)
{ {
if (DateTimeOffset.UtcNow <= data.MutedUntil)
{
return Result.FromSuccess();
}
var unmuteResult = await _guildApi.ModifyGuildMemberAsync( var unmuteResult = await _guildApi.ModifyGuildMemberAsync(
guildId, id, roles: data.Roles.ConvertAll(r => r.ToSnowflake()), guildId, id, roles: data.Roles.ConvertAll(r => r.ToSnowflake()),
reason: Messages.PunishmentExpired.EncodeHeader(), ct: ct); reason: Messages.PunishmentExpired.EncodeHeader(), ct: ct);
@ -145,9 +157,6 @@ public sealed partial class MemberUpdateService : BackgroundService
return unmuteResult; return unmuteResult;
} }
return Result.FromSuccess();
}
private async Task<Result> FilterNicknameAsync(Snowflake guildId, IUser user, IGuildMember member, private async Task<Result> FilterNicknameAsync(Snowflake guildId, IUser user, IGuildMember member,
CancellationToken ct) CancellationToken ct)
{ {