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 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<Result> CheckMemberPunishmentsAsync(
private async Task<Result> 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<Result> 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<Result> FilterNicknameAsync(Snowflake guildId, IUser user, IGuildMember member,