From 68d1534b265d1208c12c6bc591f123fd58018ead Mon Sep 17 00:00:00 2001 From: Macintosh II <95250141+mctaylors@users.noreply.github.com> Date: Wed, 4 Oct 2023 15:58:56 +0300 Subject: [PATCH 1/2] Unload guild datas when they become unavailable (#146) Closes #120 --------- Signed-off-by: Macintosh II Signed-off-by: Macintosh II <95250141+mctaylors@users.noreply.github.com> --- src/Responders/GuildUnloadedResponder.cs | 38 ++++++++++++++++++++++++ src/Services/GuildDataService.cs | 5 ++++ 2 files changed, 43 insertions(+) create mode 100644 src/Responders/GuildUnloadedResponder.cs diff --git a/src/Responders/GuildUnloadedResponder.cs b/src/Responders/GuildUnloadedResponder.cs new file mode 100644 index 0000000..0cffe25 --- /dev/null +++ b/src/Responders/GuildUnloadedResponder.cs @@ -0,0 +1,38 @@ +using JetBrains.Annotations; +using Microsoft.Extensions.Logging; +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 removing guild ID from if the guild becomes unavailable. +/// +[UsedImplicitly] +public class GuildUnloadedResponder : IResponder +{ + private readonly GuildDataService _guildData; + private readonly ILogger _logger; + + public GuildUnloadedResponder( + GuildDataService guildData, ILogger logger) + { + _guildData = guildData; + _logger = logger; + } + + public Task RespondAsync(IGuildDelete gatewayEvent, CancellationToken ct = default) + { + var guildId = gatewayEvent.ID; + var isDataRemoved = _guildData.UnloadGuildData(guildId); + if (isDataRemoved) + { + _logger.LogInformation("Left guild {GuildId}", guildId); + } + + return Task.FromResult(Result.FromSuccess()); + } +} diff --git a/src/Services/GuildDataService.cs b/src/Services/GuildDataService.cs index f5c7faa..73d4a25 100644 --- a/src/Services/GuildDataService.cs +++ b/src/Services/GuildDataService.cs @@ -142,4 +142,9 @@ public sealed class GuildDataService : IHostedService { return _datas.Keys; } + + public bool UnloadGuildData(Snowflake id) + { + return _datas.TryRemove(id, out _); + } } From 395a656fc2492d38c2521f87bc8683c132b24b04 Mon Sep 17 00:00:00 2001 From: Macintosh II <95250141+mctaylors@users.noreply.github.com> Date: Wed, 4 Oct 2023 16:01:26 +0300 Subject: [PATCH 2/2] Edit log output in GuildLoadedResponder (#147) Depends on #146 Signed-off-by: Macintosh II <95250141+mctaylors@users.noreply.github.com> --- src/Responders/GuildLoadedResponder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Responders/GuildLoadedResponder.cs b/src/Responders/GuildLoadedResponder.cs index 92fc009..b91b209 100644 --- a/src/Responders/GuildLoadedResponder.cs +++ b/src/Responders/GuildLoadedResponder.cs @@ -41,7 +41,7 @@ public class GuildLoadedResponder : IResponder } var guild = gatewayEvent.Guild.AsT0; - _logger.LogInformation("Joined guild \"{Name}\"", guild.Name); + _logger.LogInformation("Joined guild {ID} (\"{Name}\")", guild.ID, guild.Name); var data = await _guildData.GetData(guild.ID, ct); var cfg = data.Settings;