2023-07-18 15:25:02 +03:00
|
|
|
using Boyfriend.Data;
|
|
|
|
using Boyfriend.Services;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Gateway.Responders;
|
|
|
|
using Remora.Results;
|
|
|
|
|
|
|
|
namespace Boyfriend.Responders;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles sending a guild's <see cref="GuildSettings.WelcomeMessage" /> if one is set.
|
|
|
|
/// If <see cref="GuildSettings.ReturnRolesOnRejoin" /> is enabled, roles will be returned.
|
|
|
|
/// </summary>
|
|
|
|
/// <seealso cref="GuildSettings.WelcomeMessage" />
|
|
|
|
[UsedImplicitly]
|
2023-08-02 23:51:16 +03:00
|
|
|
public class GuildMemberJoinedResponder : IResponder<IGuildMemberAdd>
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
private readonly IDiscordRestChannelAPI _channelApi;
|
2023-08-02 23:51:16 +03:00
|
|
|
private readonly IDiscordRestGuildAPI _guildApi;
|
|
|
|
private readonly GuildDataService _guildData;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
public GuildMemberJoinedResponder(
|
2023-08-02 23:51:16 +03:00
|
|
|
IDiscordRestChannelAPI channelApi, GuildDataService guildData, IDiscordRestGuildAPI guildApi)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
_channelApi = channelApi;
|
2023-08-02 23:51:16 +03:00
|
|
|
_guildData = guildData;
|
2023-07-18 15:25:02 +03:00
|
|
|
_guildApi = guildApi;
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public async Task<Result> RespondAsync(IGuildMemberAdd gatewayEvent, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
if (!gatewayEvent.User.IsDefined(out var user))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentNullError(nameof(gatewayEvent.User));
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var data = await _guildData.GetData(gatewayEvent.GuildID, ct);
|
2023-07-18 15:25:02 +03:00
|
|
|
var cfg = data.Settings;
|
2023-08-04 16:52:54 +03:00
|
|
|
var memberData = data.GetOrCreateMemberData(user.ID);
|
2023-08-02 23:51:16 +03:00
|
|
|
|
|
|
|
if (GuildSettings.ReturnRolesOnRejoin.Get(cfg))
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
var result = await _guildApi.ModifyGuildMemberAsync(
|
|
|
|
gatewayEvent.GuildID, user.ID,
|
2023-08-04 16:52:54 +03:00
|
|
|
roles: memberData.Roles.ConvertAll(r => r.ToSnowflake()), ct: ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!result.IsSuccess)
|
|
|
|
{
|
|
|
|
return Result.FromError(result.Error);
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
|
|
|
|
2023-08-03 23:34:01 +03:00
|
|
|
if (GuildSettings.PublicFeedbackChannel.Get(cfg).Empty()
|
|
|
|
|| GuildSettings.WelcomeMessage.Get(cfg) is "off" or "disable" or "disabled")
|
|
|
|
{
|
|
|
|
return Result.FromSuccess();
|
|
|
|
}
|
|
|
|
|
2023-07-18 15:25:02 +03:00
|
|
|
Messages.Culture = GuildSettings.Language.Get(cfg);
|
|
|
|
var welcomeMessage = GuildSettings.WelcomeMessage.Get(cfg) is "default" or "reset"
|
|
|
|
? Messages.DefaultWelcomeMessage
|
|
|
|
: GuildSettings.WelcomeMessage.Get(cfg);
|
|
|
|
|
|
|
|
var guildResult = await _guildApi.GetGuildAsync(gatewayEvent.GuildID, ct: ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!guildResult.IsDefined(out var guild))
|
|
|
|
{
|
|
|
|
return Result.FromError(guildResult);
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
var embed = new EmbedBuilder()
|
|
|
|
.WithSmallTitle(string.Format(welcomeMessage, user.GetTag(), guild.Name), user)
|
|
|
|
.WithGuildFooter(guild)
|
|
|
|
.WithTimestamp(gatewayEvent.JoinedAt)
|
|
|
|
.WithColour(ColorsList.Green)
|
|
|
|
.Build();
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!embed.IsDefined(out var built))
|
|
|
|
{
|
|
|
|
return Result.FromError(embed);
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
return (Result)await _channelApi.CreateMessageAsync(
|
|
|
|
GuildSettings.PublicFeedbackChannel.Get(cfg), embeds: new[] { built },
|
|
|
|
allowedMentions: Boyfriend.NoMentions, ct: ct);
|
|
|
|
}
|
|
|
|
}
|