From 4e4e60f845b79f6cfa66a4e8727a2856cfa7594b Mon Sep 17 00:00:00 2001 From: Macintosh II <95250141+mctaylors@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:26:58 +0300 Subject: [PATCH] Fix mute role returning when rejoining server (#121) Closes #117 --------- Signed-off-by: Macintosh II Co-authored-by: Octol1ttle --- src/Responders/GuildMemberJoinedResponder.cs | 37 +++++++++++++++----- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Responders/GuildMemberJoinedResponder.cs b/src/Responders/GuildMemberJoinedResponder.cs index 5699008..849ff5e 100644 --- a/src/Responders/GuildMemberJoinedResponder.cs +++ b/src/Responders/GuildMemberJoinedResponder.cs @@ -1,3 +1,4 @@ +using System.Text.Json.Nodes; using Boyfriend.Data; using Boyfriend.Services; using JetBrains.Annotations; @@ -5,6 +6,7 @@ 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.Rest.Core; using Remora.Results; namespace Boyfriend.Responders; @@ -40,15 +42,10 @@ public class GuildMemberJoinedResponder : IResponder var cfg = data.Settings; var memberData = data.GetOrCreateMemberData(user.ID); - if (GuildSettings.ReturnRolesOnRejoin.Get(cfg)) + var returnRolesResult = await TryReturnRolesAsync(cfg, memberData, gatewayEvent.GuildID, user.ID, ct); + if (!returnRolesResult.IsSuccess) { - var result = await _guildApi.ModifyGuildMemberAsync( - gatewayEvent.GuildID, user.ID, - roles: memberData.Roles.ConvertAll(r => r.ToSnowflake()), ct: ct); - if (!result.IsSuccess) - { - return Result.FromError(result.Error); - } + return Result.FromError(returnRolesResult.Error); } if (GuildSettings.PublicFeedbackChannel.Get(cfg).Empty() @@ -83,4 +80,28 @@ public class GuildMemberJoinedResponder : IResponder GuildSettings.PublicFeedbackChannel.Get(cfg), embeds: new[] { built }, allowedMentions: Boyfriend.NoMentions, ct: ct); } + + private async Task TryReturnRolesAsync( + JsonNode cfg, MemberData memberData, Snowflake guildId, Snowflake userId, CancellationToken ct) + { + if (!GuildSettings.ReturnRolesOnRejoin.Get(cfg)) + { + return Result.FromSuccess(); + } + + var assignRoles = new List(); + + 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); + } }