Check interactions in MemberUpdateService before operating on members (#177)

This PR fixes an issue that caused REST errors to occur in
MemberUpdateService if the bot tries to interact with a member it can't
interact with. The issue is fixed by returning from TickMemberDataAsync
early if the member cannot be interacted with. An error message was
planned, but it requires adding a lot of services and severely
increasing the complexity. Contributors may feel free to add one if they
deem so necessary.
This commit is contained in:
Octol1ttle 2023-11-04 23:33:37 +05:00 committed by GitHub
parent 5f0d806213
commit f12d6d13c5
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 39 deletions

View file

@ -4,11 +4,11 @@ using Octobot.Data;
using Octobot.Extensions; using Octobot.Extensions;
using Octobot.Services; using Octobot.Services;
using Remora.Discord.API.Abstractions.Gateway.Events; using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest; using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.API.Gateway.Events; using Remora.Discord.API.Gateway.Events;
using Remora.Discord.Extensions.Embeds; using Remora.Discord.Extensions.Embeds;
using Remora.Discord.Gateway.Responders; using Remora.Discord.Gateway.Responders;
using Remora.Rest.Core;
using Remora.Results; using Remora.Results;
namespace Octobot.Responders; namespace Octobot.Responders;
@ -24,15 +24,17 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
private readonly GuildDataService _guildData; private readonly GuildDataService _guildData;
private readonly ILogger<GuildLoadedResponder> _logger; private readonly ILogger<GuildLoadedResponder> _logger;
private readonly IDiscordRestUserAPI _userApi; private readonly IDiscordRestUserAPI _userApi;
private readonly UtilityService _utility;
public GuildLoadedResponder( public GuildLoadedResponder(
IDiscordRestChannelAPI channelApi, GuildDataService guildData, ILogger<GuildLoadedResponder> logger, IDiscordRestChannelAPI channelApi, GuildDataService guildData, ILogger<GuildLoadedResponder> logger,
IDiscordRestUserAPI userApi) IDiscordRestUserAPI userApi, UtilityService utility)
{ {
_channelApi = channelApi; _channelApi = channelApi;
_guildData = guildData; _guildData = guildData;
_logger = logger; _logger = logger;
_userApi = userApi; _userApi = userApi;
_utility = utility;
} }
public async Task<Result> RespondAsync(IGuildCreate gatewayEvent, CancellationToken ct = default) public async Task<Result> RespondAsync(IGuildCreate gatewayEvent, CancellationToken ct = default)
@ -59,20 +61,7 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
if (data.DataLoadFailed) if (data.DataLoadFailed)
{ {
var errorEmbed = new EmbedBuilder() return await SendDataLoadFailed(guild, data, bot, ct);
.WithSmallTitle(Messages.DataLoadFailedTitle, bot)
.WithDescription(Messages.DataLoadFailedDescription)
.WithFooter(Messages.ContactDevelopers)
.WithColour(ColorsList.Red)
.Build();
if (!errorEmbed.IsDefined(out var errorBuilt))
{
return Result.FromError(errorEmbed);
}
return (Result)await _channelApi.CreateMessageAsync(
GetEmergencyFeedbackChannel(guild, data), embeds: new[] { errorBuilt }, ct: ct);
} }
_logger.LogInformation("Loaded guild {ID} (\"{Name}\")", guild.ID, guild.Name); _logger.LogInformation("Loaded guild {ID} (\"{Name}\")", guild.ID, guild.Name);
@ -105,22 +94,27 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
GuildSettings.PrivateFeedbackChannel.Get(cfg), embeds: new[] { built }, ct: ct); GuildSettings.PrivateFeedbackChannel.Get(cfg), embeds: new[] { built }, ct: ct);
} }
private static Snowflake GetEmergencyFeedbackChannel(IGuildCreate.IAvailableGuild guild, GuildData data) private async Task<Result> SendDataLoadFailed(IGuild guild, GuildData data, IUser bot, CancellationToken ct)
{ {
var privateFeedback = GuildSettings.PrivateFeedbackChannel.Get(data.Settings); var errorEmbed = new EmbedBuilder()
if (!privateFeedback.Empty()) .WithSmallTitle(Messages.DataLoadFailedTitle, bot)
.WithDescription(Messages.DataLoadFailedDescription)
.WithFooter(Messages.ContactDevelopers)
.WithColour(ColorsList.Red)
.Build();
if (!errorEmbed.IsDefined(out var errorBuilt))
{ {
return privateFeedback; return Result.FromError(errorEmbed);
} }
var publicFeedback = GuildSettings.PublicFeedbackChannel.Get(data.Settings); var channelResult = await _utility.GetEmergencyFeedbackChannel(guild, data, ct);
if (!publicFeedback.Empty()) if (!channelResult.IsDefined(out var channel))
{ {
return publicFeedback; return Result.FromError(channelResult);
} }
return guild.SystemChannelID.AsOptional().IsDefined(out var systemChannel) return (Result)await _channelApi.CreateMessageAsync(
? systemChannel channel, embeds: new[] { errorBuilt }, ct: ct);
: guild.Channels[0].ID;
} }
} }

View file

@ -29,14 +29,16 @@ public sealed partial class MemberUpdateService : BackgroundService
private readonly IDiscordRestGuildAPI _guildApi; private readonly IDiscordRestGuildAPI _guildApi;
private readonly GuildDataService _guildData; private readonly GuildDataService _guildData;
private readonly ILogger<MemberUpdateService> _logger; private readonly ILogger<MemberUpdateService> _logger;
private readonly UtilityService _utility;
public MemberUpdateService(IDiscordRestChannelAPI channelApi, IDiscordRestGuildAPI guildApi, public MemberUpdateService(IDiscordRestChannelAPI channelApi, IDiscordRestGuildAPI guildApi,
GuildDataService guildData, ILogger<MemberUpdateService> logger) GuildDataService guildData, ILogger<MemberUpdateService> logger, UtilityService utility)
{ {
_channelApi = channelApi; _channelApi = channelApi;
_guildApi = guildApi; _guildApi = guildApi;
_guildData = guildData; _guildData = guildData;
_logger = logger; _logger = logger;
_utility = utility;
} }
protected override async Task ExecuteAsync(CancellationToken ct) protected override async Task ExecuteAsync(CancellationToken ct)
@ -90,21 +92,20 @@ public sealed partial class MemberUpdateService : BackgroundService
return failedResults.AggregateErrors(); return failedResults.AggregateErrors();
} }
var interactionResult
= await _utility.CheckInteractionsAsync(guildId, null, id, "Update", ct);
if (!interactionResult.IsSuccess)
{
return Result.FromError(interactionResult);
}
var canInteract = interactionResult.Entity is null;
if (data.MutedUntil is null) if (data.MutedUntil is null)
{ {
data.Roles = guildMember.Roles.ToList().ConvertAll(r => r.Value); data.Roles = guildMember.Roles.ToList().ConvertAll(r => r.Value);
} }
var autoUnmuteResult = await TryAutoUnmuteAsync(guildId, id, data, ct);
failedResults.AddIfFailed(autoUnmuteResult);
if (!defaultRole.Empty() && !data.Roles.Contains(defaultRole.Value))
{
var addResult = await _guildApi.AddGuildMemberRoleAsync(
guildId, id, defaultRole, ct: ct);
failedResults.AddIfFailed(addResult);
}
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,6 +118,21 @@ public sealed partial class MemberUpdateService : BackgroundService
failedResults.AddIfFailed(reminderTickResult); failedResults.AddIfFailed(reminderTickResult);
} }
if (!canInteract)
{
return Result.FromSuccess();
}
var autoUnmuteResult = await TryAutoUnmuteAsync(guildId, id, data, ct);
failedResults.AddIfFailed(autoUnmuteResult);
if (!defaultRole.Empty() && !data.Roles.Contains(defaultRole.Value))
{
var addResult = await _guildApi.AddGuildMemberRoleAsync(
guildId, id, defaultRole, ct: ct);
failedResults.AddIfFailed(addResult);
}
if (GuildSettings.RenameHoistedUsers.Get(guildData.Settings)) if (GuildSettings.RenameHoistedUsers.Get(guildData.Settings))
{ {
var filterResult = await FilterNicknameAsync(guildId, user, guildMember, ct); var filterResult = await FilterNicknameAsync(guildId, user, guildMember, ct);

View file

@ -63,7 +63,7 @@ public sealed class UtilityService : IHostedService
/// </list> /// </list>
/// </returns> /// </returns>
public async Task<Result<string?>> CheckInteractionsAsync( public async Task<Result<string?>> CheckInteractionsAsync(
Snowflake guildId, Snowflake interacterId, Snowflake targetId, string action, CancellationToken ct = default) Snowflake guildId, Snowflake? interacterId, Snowflake targetId, string action, CancellationToken ct = default)
{ {
if (interacterId == targetId) if (interacterId == targetId)
{ {
@ -100,7 +100,12 @@ public sealed class UtilityService : IHostedService
return Result<string?>.FromError(rolesResult); return Result<string?>.FromError(rolesResult);
} }
var interacterResult = await _guildApi.GetGuildMemberAsync(guildId, interacterId, ct); if (interacterId is null)
{
return CheckInteractions(action, guild, roles, targetMember, currentMember, currentMember);
}
var interacterResult = await _guildApi.GetGuildMemberAsync(guildId, interacterId.Value, ct);
return interacterResult.IsDefined(out var interacter) return interacterResult.IsDefined(out var interacter)
? CheckInteractions(action, guild, roles, targetMember, currentMember, interacter) ? CheckInteractions(action, guild, roles, targetMember, currentMember, interacter)
: Result<string?>.FromError(interacterResult); : Result<string?>.FromError(interacterResult);
@ -246,4 +251,30 @@ public sealed class UtilityService : IHostedService
return Result.FromSuccess(); return Result.FromSuccess();
} }
public async Task<Result<Snowflake>> GetEmergencyFeedbackChannel(IGuild guild, GuildData data, CancellationToken ct)
{
var privateFeedback = GuildSettings.PrivateFeedbackChannel.Get(data.Settings);
if (!privateFeedback.Empty())
{
return privateFeedback;
}
var publicFeedback = GuildSettings.PublicFeedbackChannel.Get(data.Settings);
if (!publicFeedback.Empty())
{
return publicFeedback;
}
if (guild.SystemChannelID.AsOptional().IsDefined(out var systemChannel))
{
return systemChannel;
}
var channelsResult = await _guildApi.GetGuildChannelsAsync(guild.ID, ct);
return channelsResult.IsDefined(out var channels)
? channels[0].ID
: Result<Snowflake>.FromError(channelsResult);
}
} }