2024-10-14 14:56:40 +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/>.
|
|
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
using Catalogger.Backend.Cache;
|
2024-08-19 16:12:28 +02:00
|
|
|
using Catalogger.Backend.Cache.InMemoryCache;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Catalogger.Backend.Database;
|
2024-10-27 23:02:42 +01:00
|
|
|
using Catalogger.Backend.Database.Dapper.Repositories;
|
2024-08-13 13:08:50 +02:00
|
|
|
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.Extensions.Embeds;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Remora.Discord.Gateway.Responders;
|
|
|
|
|
using Remora.Results;
|
2024-10-14 17:09:12 +02:00
|
|
|
using Guild = Catalogger.Backend.Database.Models.Guild;
|
2024-08-13 13:08:50 +02:00
|
|
|
|
2024-08-19 16:12:28 +02:00
|
|
|
namespace Catalogger.Backend.Bot.Responders.Guilds;
|
2024-08-13 13:08:50 +02:00
|
|
|
|
2024-08-13 16:48:54 +02:00
|
|
|
public class GuildCreateResponder(
|
|
|
|
|
Config config,
|
|
|
|
|
ILogger logger,
|
2024-10-27 23:02:42 +01:00
|
|
|
GuildRepository guildRepository,
|
2024-08-19 16:12:28 +02:00
|
|
|
GuildCache guildCache,
|
2024-10-14 17:09:12 +02:00
|
|
|
EmojiCache emojiCache,
|
2024-10-24 21:16:37 +02:00
|
|
|
ChannelCache channelCache,
|
|
|
|
|
RoleCache roleCache,
|
2024-08-19 16:12:28 +02:00
|
|
|
IMemberCache memberCache,
|
2024-10-24 21:16:37 +02:00
|
|
|
IInviteCache inviteCache,
|
|
|
|
|
WebhookExecutorService webhookExecutor,
|
2024-10-09 17:35:11 +02:00
|
|
|
GuildFetchService guildFetchService
|
|
|
|
|
) : IResponder<IGuildCreate>, IResponder<IGuildDelete>
|
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-10-09 17:35:11 +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);
|
2024-10-14 17:09:12 +02:00
|
|
|
emojiCache.Set(guild.ID, guild.Emojis);
|
2024-10-09 17:35:11 +02:00
|
|
|
foreach (var c in guild.Channels)
|
|
|
|
|
channelCache.Set(c, guild.ID);
|
|
|
|
|
foreach (var r in guild.Roles)
|
|
|
|
|
roleCache.Set(r, guild.ID);
|
2024-08-19 16:12:28 +02:00
|
|
|
|
|
|
|
|
if (!await memberCache.IsGuildCachedAsync(guild.ID))
|
|
|
|
|
guildFetchService.EnqueueGuild(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
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
_logger.Verbose(
|
|
|
|
|
"Received guild create for unavailable guild {GuildId}",
|
|
|
|
|
unavailableGuild.ID
|
|
|
|
|
);
|
2024-08-13 13:08:50 +02:00
|
|
|
guildId = unavailableGuild.ID.ToUlong();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-27 23:02:42 +01:00
|
|
|
if (await guildRepository.IsGuildKnown(guildId))
|
2024-10-09 17:35:11 +02:00
|
|
|
return Result.Success;
|
2024-10-27 23:02:42 +01:00
|
|
|
await guildRepository.AddGuildAsync(guildId);
|
2024-08-13 13:08:50 +02:00
|
|
|
|
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)
|
2024-10-09 17:35:11 +02:00
|
|
|
webhookExecutor.QueueLog(
|
|
|
|
|
config.Discord.GuildLogId.Value,
|
|
|
|
|
new EmbedBuilder()
|
|
|
|
|
.WithTitle("Joined new guild")
|
|
|
|
|
.WithDescription($"Joined new guild **{guild.Name}**")
|
|
|
|
|
.WithFooter($"ID: {guild.ID}")
|
|
|
|
|
.WithCurrentTimestamp()
|
2024-08-13 16:48:54 +02:00
|
|
|
#pragma warning disable CS8604 // Possible null reference argument.
|
2024-10-09 17:35:11 +02:00
|
|
|
.WithThumbnailUrl(guild.IconUrl())
|
2024-08-13 16:48:54 +02:00
|
|
|
#pragma warning restore CS8604 // Possible null reference argument.
|
2024-10-09 17:35:11 +02:00
|
|
|
.Build()
|
|
|
|
|
.GetOrThrow()
|
|
|
|
|
);
|
2024-08-13 16:48:54 +02:00
|
|
|
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-24 21:16:37 +02:00
|
|
|
public async Task<Result> RespondAsync(IGuildDelete evt, CancellationToken ct = default)
|
2024-08-13 16:48:54 +02:00
|
|
|
{
|
|
|
|
|
if (evt.IsUnavailable.OrDefault(false))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Guild {GuildId} became unavailable", evt.ID);
|
2024-10-24 21:16:37 +02:00
|
|
|
return Result.Success;
|
2024-08-13 16:48:54 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-24 21:16:37 +02:00
|
|
|
// Clear the cache for this guild
|
2024-10-13 17:08:32 +02:00
|
|
|
guildCache.Remove(evt.ID, out _);
|
2024-10-24 21:16:37 +02:00
|
|
|
emojiCache.Remove(evt.ID);
|
|
|
|
|
channelCache.RemoveGuild(evt.ID);
|
|
|
|
|
roleCache.RemoveGuild(evt.ID);
|
|
|
|
|
await memberCache.RemoveAllMembersAsync(evt.ID);
|
|
|
|
|
await inviteCache.RemoveAsync(evt.ID);
|
2024-10-13 17:08:32 +02:00
|
|
|
|
2024-08-13 16:48:54 +02:00
|
|
|
if (!guildCache.TryGet(evt.ID, out var guild))
|
|
|
|
|
{
|
|
|
|
|
_logger.Information("Left uncached guild {GuildId}", evt.ID);
|
2024-10-24 21:16:37 +02:00
|
|
|
return Result.Success;
|
2024-08-13 16:48:54 +02:00
|
|
|
}
|
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)
|
2024-10-09 17:35:11 +02:00
|
|
|
webhookExecutor.QueueLog(
|
|
|
|
|
config.Discord.GuildLogId.Value,
|
|
|
|
|
new EmbedBuilder()
|
|
|
|
|
.WithTitle("Left guild")
|
|
|
|
|
.WithDescription($"Left guild **{guild.Name}**")
|
|
|
|
|
.WithFooter($"ID: {guild.ID}")
|
|
|
|
|
.WithCurrentTimestamp()
|
2024-08-13 16:48:54 +02:00
|
|
|
#pragma warning disable CS8604 // Possible null reference argument.
|
2024-10-09 17:35:11 +02:00
|
|
|
.WithThumbnailUrl(guild.IconUrl())
|
2024-08-13 16:48:54 +02:00
|
|
|
#pragma warning restore CS8604 // Possible null reference argument.
|
2024-10-09 17:35:11 +02:00
|
|
|
.Build()
|
|
|
|
|
.GetOrThrow()
|
|
|
|
|
);
|
2024-08-13 16:48:54 +02:00
|
|
|
|
2024-10-24 21:16:37 +02:00
|
|
|
return Result.Success;
|
2024-08-13 13:08:50 +02:00
|
|
|
}
|
2024-10-09 17:35:11 +02:00
|
|
|
}
|