// 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 .
using Catalogger.Backend.Cache;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Humanizer;
using Microsoft.EntityFrameworkCore;
using Remora.Discord.API;
using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.API.Objects;
using Remora.Discord.Extensions.Embeds;
using Remora.Discord.Gateway.Responders;
using Remora.Results;
namespace Catalogger.Backend.Bot.Responders.Members;
public class GuildMemberAddResponder(
ILogger logger,
DatabaseContext db,
IMemberCache memberCache,
IInviteCache inviteCache,
UserCache userCache,
WebhookExecutorService webhookExecutor,
IDiscordRestGuildAPI guildApi,
PluralkitApiService pluralkitApi
) : IResponder
{
private readonly ILogger _logger = logger.ForContext();
private static readonly TimeSpan NewAccountThreshold = 7.Days();
public async Task RespondAsync(IGuildMemberAdd member, CancellationToken ct = default)
{
await memberCache.SetAsync(member.GuildID, member);
var user = member.User.GetOrThrow();
userCache.UpdateUser(user);
var builder = new EmbedBuilder()
.WithTitle("Member joined")
.WithColour(DiscordUtils.Green)
.WithAuthor(user.Tag(), null, user.AvatarUrl())
.WithDescription($"<@{user.ID}>")
.WithCurrentTimestamp()
.WithFooter($"ID: {user.ID}");
var guildConfig = await db.GetGuildAsync(member.GuildID, ct);
var guildRes = await guildApi.GetGuildAsync(member.GuildID, withCounts: true, ct);
if (guildRes.IsSuccess && guildRes.Entity.ApproximateMemberCount.IsDefined())
builder.Description +=
$"\n{guildRes.Entity.ApproximateMemberCount.OrDefault(1).Ordinalize()} to join";
builder.Description +=
$"\ncreated {user.ID.Timestamp.Prettify()} ago\n";
var pkSystem = await pluralkitApi.GetPluralKitSystemAsync(user.ID.Value, ct);
if (pkSystem != null)
{
var createdAt =
pkSystem.Created != null
? $"{pkSystem.Created.Value.Prettify()} ago ()"
: "*(unknown)*";
builder.AddField(
"PluralKit system",
$"""
**ID:** {pkSystem.Id} (`{pkSystem.Uuid}`)
**Name:** {pkSystem.Name ?? "*(none)*"}
**Tag:** {pkSystem.Tag ?? "*(none)*"}
**Created:** {createdAt}
"""
);
}
// Bots don't use invites, so the entire following block is useless
if (user.IsBot.OrDefault())
{
goto afterInvite;
}
var existingInvites = (await inviteCache.TryGetAsync(member.GuildID)).ToList();
var newInvitesRes = await guildApi.GetGuildInvitesAsync(member.GuildID, ct);
if (!newInvitesRes.IsSuccess)
{
_logger.Error(
"Could not fetch invites for guild {GuildId}: {Error}",
member.GuildID,
newInvitesRes.Error
);
goto afterInvite;
}
// Update the invite cache immediately--we've already fetched a copy of the invites, after all
await inviteCache.SetAsync(member.GuildID, newInvitesRes.Entity);
// If we can't find a used invite, and the guild has a vanity link, that invite was used.
// Otherwise, we give up
var usedInvite = FindUsedInvite(existingInvites, newInvitesRes.Entity);
if (usedInvite == null)
{
builder.AddField(
"Invite used",
guildRes is { IsSuccess: true, Entity.VanityUrlCode: not null }
? $"Vanity invite (`{guildRes.Entity.VanityUrlCode}`)"
: "*Could not determine invite, sorry.*"
);
goto afterInvite;
}
var inviteName =
await db
.Invites.Where(i => i.Code == usedInvite.Code && i.GuildId == member.GuildID.Value)
.Select(i => i.Name)
.FirstOrDefaultAsync(ct) ?? "*(unnamed)*";
var inviteDescription = $"""
**Code:** {usedInvite.Code}
**Name:** {inviteName}
**Uses:** {usedInvite.Uses}
**Created at:**
""";
if (usedInvite.Inviter.IsDefined(out var inviter))
inviteDescription += $"\n**Created by:** {inviter.Tag()} <@{inviter.ID}>";
builder.AddField("Invite used", inviteDescription);
afterInvite:
List