2023-09-28 14:26:58 +03:00
|
|
|
using System.Text.Json.Nodes;
|
2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Data;
|
2023-10-12 18:37:25 +03:00
|
|
|
using Octobot.Extensions;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Services;
|
2023-07-18 15:25:02 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Gateway.Responders;
|
2023-09-28 14:26:58 +03:00
|
|
|
using Remora.Rest.Core;
|
2023-07-18 15:25:02 +03:00
|
|
|
using Remora.Results;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot.Responders;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
/// <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
|
|
|
|
2023-12-21 18:35:10 +03:00
|
|
|
memberData.Kicked = false;
|
|
|
|
|
2023-09-28 14:26:58 +03:00
|
|
|
var returnRolesResult = await TryReturnRolesAsync(cfg, memberData, gatewayEvent.GuildID, user.ID, ct);
|
|
|
|
if (!returnRolesResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(returnRolesResult);
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
|
|
|
|
2024-03-18 11:42:37 +03:00
|
|
|
if (GuildSettings.WelcomeMessagesChannel.Get(cfg).Empty()
|
2023-08-03 23:34:01 +03:00
|
|
|
|| 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))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(guildResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
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-12-17 19:47:52 +03:00
|
|
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
2024-03-18 11:42:37 +03:00
|
|
|
GuildSettings.WelcomeMessagesChannel.Get(cfg), embedResult: embed,
|
2023-09-30 16:58:32 +03:00
|
|
|
allowedMentions: Octobot.NoMentions, ct: ct);
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
2023-09-28 14:26:58 +03:00
|
|
|
|
|
|
|
private async Task<Result> TryReturnRolesAsync(
|
|
|
|
JsonNode cfg, MemberData memberData, Snowflake guildId, Snowflake userId, CancellationToken ct)
|
|
|
|
{
|
|
|
|
if (!GuildSettings.ReturnRolesOnRejoin.Get(cfg))
|
|
|
|
{
|
|
|
|
return Result.FromSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
var assignRoles = new List<Snowflake>();
|
|
|
|
|
|
|
|
if (memberData.MutedUntil is null || !GuildSettings.RemoveRolesOnMute.Get(cfg))
|
|
|
|
{
|
|
|
|
assignRoles.AddRange(memberData.Roles.ConvertAll(r => r.ToSnowflake()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memberData.MutedUntil is not null)
|
|
|
|
{
|
|
|
|
assignRoles.Add(GuildSettings.MuteRole.Get(cfg));
|
|
|
|
}
|
|
|
|
|
|
|
|
return await _guildApi.ModifyGuildMemberAsync(
|
|
|
|
guildId, userId, roles: assignRoles, ct: ct);
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|