From d713b977f04751d3a6982cebfafc517463ae2b2d Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Sat, 30 Sep 2023 20:36:55 +0500 Subject: [PATCH] Synchronize roles only on member data updates (#130) This PR makes it so that roles in MemberData are updated only in MemberUpdateService. This reduces possible points of failures, maintenance burden and reliance on gateway events --- .../GuildMemberRolesUpdatedResponder.cs | 33 ------------------- src/Services/GuildDataService.cs | 19 +---------- src/Services/Update/MemberUpdateService.cs | 5 +++ 3 files changed, 6 insertions(+), 51 deletions(-) delete mode 100644 src/Responders/GuildMemberRolesUpdatedResponder.cs diff --git a/src/Responders/GuildMemberRolesUpdatedResponder.cs b/src/Responders/GuildMemberRolesUpdatedResponder.cs deleted file mode 100644 index b883c89..0000000 --- a/src/Responders/GuildMemberRolesUpdatedResponder.cs +++ /dev/null @@ -1,33 +0,0 @@ -using JetBrains.Annotations; -using Octobot.Data; -using Octobot.Services; -using Remora.Discord.API.Abstractions.Gateway.Events; -using Remora.Discord.Gateway.Responders; -using Remora.Results; - -namespace Octobot.Responders; - -/// -/// Handles updating when a guild member is updated. -/// -[UsedImplicitly] -public class GuildMemberUpdateResponder : IResponder -{ - private readonly GuildDataService _guildData; - - public GuildMemberUpdateResponder(GuildDataService guildData) - { - _guildData = guildData; - } - - public async Task RespondAsync(IGuildMemberUpdate gatewayEvent, CancellationToken ct = default) - { - var memberData = await _guildData.GetMemberData(gatewayEvent.GuildID, gatewayEvent.User.ID, ct); - if (memberData.MutedUntil is null) - { - memberData.Roles = gatewayEvent.Roles.ToList().ConvertAll(r => r.Value); - } - - return Result.FromSuccess(); - } -} diff --git a/src/Services/GuildDataService.cs b/src/Services/GuildDataService.cs index 5d2b1b1..f5c7faa 100644 --- a/src/Services/GuildDataService.cs +++ b/src/Services/GuildDataService.cs @@ -4,7 +4,6 @@ using System.Text.Json.Nodes; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Octobot.Data; -using Remora.Discord.API.Abstractions.Rest; using Remora.Rest.Core; namespace Octobot.Services; @@ -15,14 +14,12 @@ namespace Octobot.Services; public sealed class GuildDataService : IHostedService { private readonly ConcurrentDictionary _datas = new(); - private readonly IDiscordRestGuildAPI _guildApi; private readonly ILogger _logger; // https://github.com/dotnet/aspnetcore/issues/39139 public GuildDataService( - IHostApplicationLifetime lifetime, IDiscordRestGuildAPI guildApi, ILogger logger) + IHostApplicationLifetime lifetime, ILogger logger) { - _guildApi = guildApi; _logger = logger; lifetime.ApplicationStopping.Register(ApplicationStopping); } @@ -110,15 +107,6 @@ public sealed class GuildDataService : IHostedService continue; } - if (data.MutedUntil is null) - { - var memberResult = await _guildApi.GetGuildMemberAsync(guildId, data.Id.ToSnowflake(), ct); - if (memberResult.IsSuccess) - { - data.Roles = memberResult.Entity.Roles.ToList().ConvertAll(r => r.Value); - } - } - memberData.Add(data.Id, data); } @@ -150,11 +138,6 @@ public sealed class GuildDataService : IHostedService return (await GetData(guildId, ct)).Settings; } - public async Task GetMemberData(Snowflake guildId, Snowflake userId, CancellationToken ct = default) - { - return (await GetData(guildId, ct)).GetOrCreateMemberData(userId); - } - public ICollection GetGuildIds() { return _datas.Keys; diff --git a/src/Services/Update/MemberUpdateService.cs b/src/Services/Update/MemberUpdateService.cs index 7cd13fe..1d2b7f6 100644 --- a/src/Services/Update/MemberUpdateService.cs +++ b/src/Services/Update/MemberUpdateService.cs @@ -89,6 +89,11 @@ public sealed partial class MemberUpdateService : BackgroundService return failedResults.AggregateErrors(); } + if (data.MutedUntil is null) + { + data.Roles = guildMember.Roles.ToList().ConvertAll(r => r.Value); + } + var autoUnmuteResult = await TryAutoUnmuteAsync(guildId, id, data, ct); failedResults.AddIfFailed(autoUnmuteResult);