2024-08-13 13:08:50 +02:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
using Catalogger.Backend.Cache;
|
|
|
|
|
using Catalogger.Backend.Database;
|
|
|
|
|
using Catalogger.Backend.Database.Models;
|
|
|
|
|
using Catalogger.Backend.Extensions;
|
2024-08-13 16:48:54 +02:00
|
|
|
using Catalogger.Backend.Services;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
2024-08-13 16:48:54 +02:00
|
|
|
using Remora.Discord.API.Gateway.Events;
|
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Remora.Discord.Gateway.Responders;
|
|
|
|
|
using Remora.Results;
|
|
|
|
|
|
|
|
|
|
namespace Catalogger.Backend.Bot.Responders;
|
|
|
|
|
|
2024-08-13 16:48:54 +02:00
|
|
|
public class GuildCreateResponder(
|
|
|
|
|
Config config,
|
|
|
|
|
ILogger logger,
|
|
|
|
|
DatabaseContext db,
|
|
|
|
|
GuildCacheService guildCache,
|
2024-08-16 22:28:05 +02:00
|
|
|
RoleCacheService roleCache,
|
2024-08-13 16:48:54 +02:00
|
|
|
ChannelCacheService channelCache,
|
|
|
|
|
WebhookExecutorService webhookExecutor)
|
|
|
|
|
: IResponder<IGuildCreate>, IResponder<GuildDelete>
|
2024-08-13 13:08:50 +02:00
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger = logger.ForContext<GuildCreateResponder>();
|
|
|
|
|
|
|
|
|
|
public async Task<Result> RespondAsync(IGuildCreate evt, CancellationToken ct = default)
|
|
|
|
|
{
|
|
|
|
|
ulong guildId;
|
|
|
|
|
string? guildName = null;
|
2024-08-16 22:28:05 +02:00
|
|
|
if (evt.Guild.TryPickT0(out var guild, out var unavailableGuild))
|
2024-08-13 13:08:50 +02:00
|
|
|
{
|
2024-08-13 16:48:54 +02:00
|
|
|
_logger.Verbose("Received guild create for available guild {GuildName} / {GuildId})", guild.Name, guild.ID);
|
2024-08-13 13:08:50 +02:00
|
|
|
guildId = guild.ID.ToUlong();
|
|
|
|
|
guildName = guild.Name;
|
|
|
|
|
|
2024-08-13 16:48:54 +02:00
|
|
|
guildCache.Set(guild);
|
|
|
|
|
foreach (var c in guild.Channels) channelCache.Set(c, guild.ID);
|
2024-08-16 22:28:05 +02:00
|
|
|
foreach (var r in guild.Roles) roleCache.Set(r, guild.ID);
|
2024-08-13 13:08:50 +02:00
|
|
|
}
|
2024-08-16 22:28:05 +02:00
|
|
|
else
|
2024-08-13 13:08:50 +02:00
|
|
|
{
|
|
|
|
|
_logger.Verbose("Received guild create for unavailable guild {GuildId}", unavailableGuild.ID);
|
|
|
|
|
guildId = unavailableGuild.ID.ToUlong();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tx = await db.Database.BeginTransactionAsync(ct);
|
|
|
|
|
if (await db.Guilds.FindAsync([guildId], ct) != null) return Result.Success;
|
|
|
|
|
|
|
|
|
|
db.Add(new Guild
|
|
|
|
|
{
|
|
|
|
|
Id = guildId
|
|
|
|
|
});
|
|
|
|
|
await db.SaveChangesAsync(ct);
|
|
|
|
|
await tx.CommitAsync(ct);
|
|
|
|
|
|
2024-08-13 16:48:54 +02:00
|
|
|
_logger.Information("Joined new guild {GuildName} / {GuildId}", guildName, guildId);
|
2024-08-16 17:04:24 +02:00
|
|
|
|
2024-08-13 16:48:54 +02:00
|
|
|
if (config.Discord.GuildLogId != null && evt.Guild.IsT0)
|
|
|
|
|
await webhookExecutor.QueueLogAsync(config.Discord.GuildLogId.Value, new EmbedBuilder()
|
|
|
|
|
.WithTitle("Joined new guild")
|
|
|
|
|
.WithDescription($"Joined new guild **{guild.Name}**")
|
|
|
|
|
.WithFooter($"ID: {guild.ID}")
|
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
|
#pragma warning disable CS8604 // Possible null reference argument.
|
|
|
|
|
.WithThumbnailUrl(guild.IconUrl())
|
|
|
|
|
#pragma warning restore CS8604 // Possible null reference argument.
|
|
|
|
|
.Build()
|
|
|
|
|
.GetOrThrow());
|
|
|
|
|
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Result> RespondAsync(GuildDelete evt, CancellationToken ct = default)
|
|
|
|
|
{
|
|
|
|
|
if (evt.IsUnavailable.OrDefault(false))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Guild {GuildId} became unavailable", evt.ID);
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!guildCache.TryGet(evt.ID, out var guild))
|
|
|
|
|
{
|
|
|
|
|
_logger.Information("Left uncached guild {GuildId}", evt.ID);
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
2024-08-16 17:04:24 +02:00
|
|
|
|
2024-08-13 16:48:54 +02:00
|
|
|
_logger.Information("Left guild {GuildName} / {GuildId}", guild.Name, guild.ID);
|
2024-08-16 17:04:24 +02:00
|
|
|
|
2024-08-13 16:48:54 +02:00
|
|
|
if (config.Discord.GuildLogId != null)
|
|
|
|
|
await webhookExecutor.QueueLogAsync(config.Discord.GuildLogId.Value, new EmbedBuilder()
|
|
|
|
|
.WithTitle("Left guild")
|
|
|
|
|
.WithDescription($"Left guild **{guild.Name}**")
|
|
|
|
|
.WithFooter($"ID: {guild.ID}")
|
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
|
#pragma warning disable CS8604 // Possible null reference argument.
|
|
|
|
|
.WithThumbnailUrl(guild.IconUrl())
|
|
|
|
|
#pragma warning restore CS8604 // Possible null reference argument.
|
|
|
|
|
.Build()
|
|
|
|
|
.GetOrThrow());
|
|
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|