2024-10-14 17:09:12 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
|
|
using Catalogger.Backend.Cache.InMemoryCache;
|
2024-10-28 14:04:55 +01:00
|
|
|
using Catalogger.Backend.Database.Repositories;
|
2024-10-14 17:09:12 +02:00
|
|
|
using Catalogger.Backend.Extensions;
|
|
|
|
|
using Catalogger.Backend.Services;
|
|
|
|
|
using Remora.Discord.API;
|
|
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
|
using Remora.Discord.Gateway.Responders;
|
|
|
|
|
using Remora.Results;
|
|
|
|
|
|
|
|
|
|
namespace Catalogger.Backend.Bot.Responders.Guilds;
|
|
|
|
|
|
|
|
|
|
public class GuildEmojisUpdateResponder(
|
|
|
|
|
ILogger logger,
|
2024-10-27 23:02:42 +01:00
|
|
|
GuildRepository guildRepository,
|
2024-10-14 17:09:12 +02:00
|
|
|
EmojiCache emojiCache,
|
|
|
|
|
WebhookExecutorService webhookExecutor
|
|
|
|
|
) : IResponder<IGuildEmojisUpdate>
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger = logger.ForContext<GuildEmojisUpdateResponder>();
|
|
|
|
|
|
|
|
|
|
public async Task<Result> RespondAsync(IGuildEmojisUpdate evt, CancellationToken ct = default)
|
|
|
|
|
{
|
2024-11-27 15:48:30 +01:00
|
|
|
using var _ = LogUtils.Enrich(evt);
|
|
|
|
|
|
2024-10-14 17:09:12 +02:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!emojiCache.TryGet(evt.GuildID, out var oldEmoji))
|
|
|
|
|
{
|
|
|
|
|
_logger.Information(
|
|
|
|
|
"Previous emoji for {GuildId} were not in cache, ignoring event",
|
|
|
|
|
evt.GuildID
|
|
|
|
|
);
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEmbed embed;
|
|
|
|
|
|
|
|
|
|
// As far as I know, only one emoji can be added or removed at once.
|
|
|
|
|
var added = evt.Emojis.FirstOrDefault(e => oldEmoji.All(o => o.ID != e.ID));
|
|
|
|
|
var removed = oldEmoji.FirstOrDefault(o => evt.Emojis.All(e => o.ID != e.ID));
|
|
|
|
|
var updated = evt.Emojis.FirstOrDefault(e =>
|
|
|
|
|
oldEmoji.Any(o => o.ID == e.ID && o.Name != e.Name)
|
|
|
|
|
);
|
|
|
|
|
if (added != null)
|
|
|
|
|
{
|
|
|
|
|
var url = CDN.GetEmojiUrl(added).GetOrThrow().ToString();
|
|
|
|
|
embed = new EmbedBuilder()
|
|
|
|
|
.WithTitle("Emoji created")
|
|
|
|
|
.WithDescription($"{FormatEmoji(added)} [{added.Name}]({url})")
|
|
|
|
|
.WithThumbnailUrl(url)
|
|
|
|
|
.WithFooter($"ID: {added.ID}")
|
|
|
|
|
.WithColour(DiscordUtils.Green)
|
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
|
.Build()
|
|
|
|
|
.GetOrThrow();
|
|
|
|
|
}
|
|
|
|
|
else if (removed != null)
|
|
|
|
|
{
|
|
|
|
|
var url = CDN.GetEmojiUrl(removed).GetOrThrow().ToString();
|
|
|
|
|
embed = new EmbedBuilder()
|
|
|
|
|
.WithTitle("Emoji removed")
|
|
|
|
|
.WithDescription($"[{removed.Name}]({url})")
|
|
|
|
|
.WithThumbnailUrl(url)
|
|
|
|
|
.WithFooter($"ID: {removed.ID}")
|
|
|
|
|
.WithColour(DiscordUtils.Red)
|
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
|
.Build()
|
|
|
|
|
.GetOrThrow();
|
|
|
|
|
}
|
|
|
|
|
else if (updated != null)
|
|
|
|
|
{
|
|
|
|
|
var url = CDN.GetEmojiUrl(updated).GetOrThrow().ToString();
|
|
|
|
|
var previous = oldEmoji.First(o => o.ID == updated.ID);
|
|
|
|
|
embed = new EmbedBuilder()
|
|
|
|
|
.WithTitle("Emoji renamed")
|
|
|
|
|
.WithDescription(
|
|
|
|
|
$"""
|
|
|
|
|
{FormatEmoji(updated)} [{updated.Name}]({url})
|
|
|
|
|
{previous.Name} → {updated.Name}
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
.WithThumbnailUrl(url)
|
|
|
|
|
.WithFooter($"ID: {updated.ID}")
|
|
|
|
|
.WithColour(DiscordUtils.Green)
|
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
|
.Build()
|
|
|
|
|
.GetOrThrow();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.Warning(
|
|
|
|
|
"Received emoji update event for {GuildId} but all emoji were identical, not logging",
|
|
|
|
|
evt.GuildID
|
|
|
|
|
);
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-27 23:02:42 +01:00
|
|
|
var guildConfig = await guildRepository.GetAsync(evt.GuildID);
|
2024-10-14 17:09:12 +02:00
|
|
|
webhookExecutor.QueueLog(guildConfig, LogChannelType.GuildEmojisUpdate, embed);
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
emojiCache.Set(evt.GuildID, evt.Emojis);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string FormatEmoji(IEmoji emoji) =>
|
|
|
|
|
emoji.IsAnimated.OrDefault(false)
|
|
|
|
|
? $"<a:{emoji.Name}:{emoji.ID}>"
|
|
|
|
|
: $"<:{emoji.Name}:{emoji.ID}>";
|
|
|
|
|
}
|