Catalogger.NET/Catalogger.Backend/Bot/Responders/Channels/ChannelDeleteResponder.cs

80 lines
2.9 KiB
C#
Raw Normal View History

// Copyright (C) 2021-present sam (starshines.gay)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2024-09-02 15:05:32 +02:00
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Dapper.Repositories;
2024-09-02 15:05:32 +02:00
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,
GuildRepository guildRepository,
2024-09-02 15:05:32 +02:00
ChannelCache channelCache,
2024-10-09 17:35:11 +02:00
WebhookExecutorService webhookExecutor
) : IResponder<IChannelDelete>
2024-09-02 15:05:32 +02:00
{
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 guildRepository.GetAsync(evt.GuildID.Value);
2024-09-02 15:05:32 +02:00
var embed = new EmbedBuilder()
.WithTitle("Channel deleted")
.WithColour(DiscordUtils.Red)
.WithFooter($"ID: {evt.ID}")
.WithCurrentTimestamp()
.WithDescription($"**Name:** {channel.Name.Value}");
2024-10-09 17:35:11 +02:00
if (
channel.ParentID.IsDefined(out var parentId)
&& channelCache.TryGet(parentId.Value, out var category)
)
2024-09-02 15:05:32 +02:00
embed.Description += $"\n**Category:** {category.Name}";
else
embed.Description += "\n**Category:** (none)";
if (channel.Topic.IsDefined(out var topic))
embed.AddField("Description", topic);
2024-10-09 17:35:11 +02:00
webhookExecutor.QueueLog(
guildConfig,
LogChannelType.ChannelDelete,
embed.Build().GetOrThrow()
);
2024-09-02 15:05:32 +02:00
return Result.Success;
}
2024-10-09 17:35:11 +02:00
}