mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-11 08:23:15 +03:00
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:
parent
5f0d806213
commit
f12d6d13c5
3 changed files with 80 additions and 39 deletions
|
@ -4,11 +4,11 @@ using Octobot.Data;
|
|||
using Octobot.Extensions;
|
||||
using Octobot.Services;
|
||||
using Remora.Discord.API.Abstractions.Gateway.Events;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
using Remora.Discord.API.Abstractions.Rest;
|
||||
using Remora.Discord.API.Gateway.Events;
|
||||
using Remora.Discord.Extensions.Embeds;
|
||||
using Remora.Discord.Gateway.Responders;
|
||||
using Remora.Rest.Core;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Octobot.Responders;
|
||||
|
@ -24,15 +24,17 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
|
|||
private readonly GuildDataService _guildData;
|
||||
private readonly ILogger<GuildLoadedResponder> _logger;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
private readonly UtilityService _utility;
|
||||
|
||||
public GuildLoadedResponder(
|
||||
IDiscordRestChannelAPI channelApi, GuildDataService guildData, ILogger<GuildLoadedResponder> logger,
|
||||
IDiscordRestUserAPI userApi)
|
||||
IDiscordRestUserAPI userApi, UtilityService utility)
|
||||
{
|
||||
_channelApi = channelApi;
|
||||
_guildData = guildData;
|
||||
_logger = logger;
|
||||
_userApi = userApi;
|
||||
_utility = utility;
|
||||
}
|
||||
|
||||
public async Task<Result> RespondAsync(IGuildCreate gatewayEvent, CancellationToken ct = default)
|
||||
|
@ -59,20 +61,7 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
|
|||
|
||||
if (data.DataLoadFailed)
|
||||
{
|
||||
var errorEmbed = new EmbedBuilder()
|
||||
.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);
|
||||
return await SendDataLoadFailed(guild, data, bot, ct);
|
||||
}
|
||||
|
||||
_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);
|
||||
}
|
||||
|
||||
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);
|
||||
if (!privateFeedback.Empty())
|
||||
var errorEmbed = new EmbedBuilder()
|
||||
.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);
|
||||
if (!publicFeedback.Empty())
|
||||
var channelResult = await _utility.GetEmergencyFeedbackChannel(guild, data, ct);
|
||||
if (!channelResult.IsDefined(out var channel))
|
||||
{
|
||||
return publicFeedback;
|
||||
return Result.FromError(channelResult);
|
||||
}
|
||||
|
||||
return guild.SystemChannelID.AsOptional().IsDefined(out var systemChannel)
|
||||
? systemChannel
|
||||
: guild.Channels[0].ID;
|
||||
return (Result)await _channelApi.CreateMessageAsync(
|
||||
channel, embeds: new[] { errorBuilt }, ct: ct);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue