feat: add channel delete event
This commit is contained in:
parent
c42ca3f888
commit
9e0e53a428
1 changed files with 55 additions and 0 deletions
|
|
@ -0,0 +1,55 @@
|
|||
using Catalogger.Backend.Cache.InMemoryCache;
|
||||
using Catalogger.Backend.Database;
|
||||
using Catalogger.Backend.Database.Queries;
|
||||
using Catalogger.Backend.Extensions;
|
||||
using Catalogger.Backend.Services;
|
||||
using Remora.Discord.API.Abstractions.Gateway.Events;
|
||||
using Remora.Discord.Extensions.Embeds;
|
||||
using Remora.Discord.Gateway.Responders;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Catalogger.Backend.Bot.Responders.Channels;
|
||||
|
||||
public class ChannelDeleteResponder(
|
||||
ILogger logger,
|
||||
DatabaseContext db,
|
||||
ChannelCache channelCache,
|
||||
WebhookExecutorService webhookExecutor) : IResponder<IChannelDelete>
|
||||
{
|
||||
private readonly ILogger _logger = logger.ForContext<ChannelDeleteResponder>();
|
||||
|
||||
public async Task<Result> RespondAsync(IChannelDelete evt, CancellationToken ct = default)
|
||||
{
|
||||
if (!evt.GuildID.IsDefined())
|
||||
{
|
||||
_logger.Debug("Deleted channel {ChannelId} is not in a guild", evt.ID);
|
||||
return Result.Success;
|
||||
}
|
||||
|
||||
if (!channelCache.TryGet(evt.ID, out var channel))
|
||||
{
|
||||
_logger.Information("Deleted channel {ChannelId} not found in the cache", evt.ID);
|
||||
channelCache.Remove(evt.GuildID.OrDefault(), evt.ID, out _);
|
||||
return Result.Success;
|
||||
}
|
||||
|
||||
var guildConfig = await db.GetGuildAsync(evt.GuildID.Value, ct);
|
||||
var embed = new EmbedBuilder()
|
||||
.WithTitle("Channel deleted")
|
||||
.WithColour(DiscordUtils.Red)
|
||||
.WithFooter($"ID: {evt.ID}")
|
||||
.WithCurrentTimestamp()
|
||||
.WithDescription($"**Name:** {channel.Name.Value}");
|
||||
|
||||
if (channel.ParentID.IsDefined(out var parentId) && channelCache.TryGet(parentId.Value, out var category))
|
||||
embed.Description += $"\n**Category:** {category.Name}";
|
||||
else
|
||||
embed.Description += "\n**Category:** (none)";
|
||||
|
||||
if (channel.Topic.IsDefined(out var topic))
|
||||
embed.AddField("Description", topic);
|
||||
|
||||
await webhookExecutor.QueueLogAsync(guildConfig, LogChannelType.ChannelDelete, embed.Build().GetOrThrow());
|
||||
return Result.Success;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue