1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

Unload guild datas when they become unavailable (#146)

Closes #120

---------

Signed-off-by: Macintosh II <mctaylxrs@outlook.com>
Signed-off-by: Macintosh II <95250141+mctaylors@users.noreply.github.com>
This commit is contained in:
Macintxsh 2023-10-04 15:58:56 +03:00 committed by GitHub
parent 777dbc6eec
commit 68d1534b26
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View file

@ -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;
/// <summary>
/// Handles removing guild ID from <see cref="GuildData" /> if the guild becomes unavailable.
/// </summary>
[UsedImplicitly]
public class GuildUnloadedResponder : IResponder<IGuildDelete>
{
private readonly GuildDataService _guildData;
private readonly ILogger<GuildUnloadedResponder> _logger;
public GuildUnloadedResponder(
GuildDataService guildData, ILogger<GuildUnloadedResponder> logger)
{
_guildData = guildData;
_logger = logger;
}
public Task<Result> 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());
}
}

View file

@ -142,4 +142,9 @@ public sealed class GuildDataService : IHostedService
{ {
return _datas.Keys; return _datas.Keys;
} }
public bool UnloadGuildData(Snowflake id)
{
return _datas.TryRemove(id, out _);
}
} }