From 7cf200d8de607b4dbf1932f22a6f613e5f8eec95 Mon Sep 17 00:00:00 2001 From: Macintosh II <95250141+mctaylors@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:25:28 +0300 Subject: [PATCH] Fix /unmute not checking if the target is muted (#143) Closes #142 --------- Signed-off-by: Macintosh II --- src/Commands/MuteCommandGroup.cs | 98 +++++++++++++++++--------------- 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/src/Commands/MuteCommandGroup.cs b/src/Commands/MuteCommandGroup.cs index d558ba2..fac9c84 100644 --- a/src/Commands/MuteCommandGroup.cs +++ b/src/Commands/MuteCommandGroup.cs @@ -285,17 +285,11 @@ public class MuteCommandGroup : CommandGroup return await _feedback.SendContextualEmbedResultAsync(embed, CancellationToken); } - if (data.GetOrCreateMemberData(target.ID).MutedUntil is not null) - { - return await RemoveMuteRoleUserAsync( - target, reason, guildId, data, channelId, user, currentUser, CancellationToken); - } - - return await RemoveTimeoutUserAsync( + return await RemoveMuteAsync( target, reason, guildId, data, channelId, user, currentUser, CancellationToken); } - private async Task RemoveMuteRoleUserAsync( + private async Task RemoveMuteAsync( IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId, IUser user, IUser currentUser, CancellationToken ct = default) { @@ -315,14 +309,36 @@ public class MuteCommandGroup : CommandGroup return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct); } - var memberData = data.GetOrCreateMemberData(target.ID); - var unmuteResult = await _guildApi.ModifyGuildMemberAsync( - guildId, target.ID, roles: memberData.Roles.ConvertAll(r => r.ToSnowflake()), - reason: $"({user.GetTag()}) {reason}".EncodeHeader(), ct: ct); - memberData.MutedUntil = null; - if (!unmuteResult.IsSuccess) + var guildMemberResult = await _guildApi.GetGuildMemberAsync(guildId, target.ID, ct); + DateTimeOffset? communicationDisabledUntil = null; + if (guildMemberResult.IsDefined(out var guildMember)) { - return Result.FromError(unmuteResult.Error); + communicationDisabledUntil = guildMember.CommunicationDisabledUntil.OrDefault(null); + } + + var memberData = data.GetOrCreateMemberData(target.ID); + var isMuted = memberData.MutedUntil is not null || communicationDisabledUntil is not null; + + if (!isMuted) + { + var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.UserNotMuted, currentUser) + .WithColour(ColorsList.Red).Build(); + + return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct); + } + + var removeMuteRoleAsync = + await RemoveMuteRoleAsync(target, reason, guildId, memberData, user, CancellationToken); + if (!removeMuteRoleAsync.IsSuccess) + { + return Result.FromError(removeMuteRoleAsync.Error); + } + + var removeTimeoutResult = + await RemoveTimeoutAsync(target, reason, guildId, communicationDisabledUntil, user, CancellationToken); + if (!removeTimeoutResult.IsSuccess) + { + return Result.FromError(removeTimeoutResult.Error); } var title = string.Format(Messages.UserUnmuted, target.GetTag()); @@ -341,47 +357,37 @@ public class MuteCommandGroup : CommandGroup return await _feedback.SendContextualEmbedResultAsync(embed, ct); } - private async Task RemoveTimeoutUserAsync( - IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId, IUser user, - IUser currentUser, CancellationToken ct = default) + private async Task RemoveMuteRoleAsync( + IUser target, string reason, Snowflake guildId, MemberData memberData, IUser user, CancellationToken ct = default) { - var interactionResult - = await _utility.CheckInteractionsAsync( - guildId, user.ID, target.ID, "Unmute", ct); - if (!interactionResult.IsSuccess) + if (memberData.MutedUntil is null) { - return Result.FromError(interactionResult); + return Result.FromSuccess(); } - if (interactionResult.Entity is not null) + var unmuteResult = await _guildApi.ModifyGuildMemberAsync( + guildId, target.ID, roles: memberData.Roles.ConvertAll(r => r.ToSnowflake()), + reason: $"({user.GetTag()}) {reason}".EncodeHeader(), ct: ct); + if (unmuteResult.IsSuccess) { - var failedEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, currentUser) - .WithColour(ColorsList.Red).Build(); + memberData.MutedUntil = null; + } - return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct); + return unmuteResult; + } + + private async Task RemoveTimeoutAsync( + IUser target, string reason, Snowflake guildId, DateTimeOffset? communicationDisabledUntil, + IUser user, CancellationToken ct = default) + { + if (communicationDisabledUntil is null) + { + return Result.FromSuccess(); } var unmuteResult = await _guildApi.ModifyGuildMemberAsync( guildId, target.ID, reason: $"({user.GetTag()}) {reason}".EncodeHeader(), communicationDisabledUntil: null, ct: ct); - if (!unmuteResult.IsSuccess) - { - return Result.FromError(unmuteResult.Error); - } - - var title = string.Format(Messages.UserUnmuted, target.GetTag()); - var description = $"- {string.Format(Messages.DescriptionActionReason, reason)}"; - var logResult = _utility.LogActionAsync( - data.Settings, channelId, user, title, description, target, ColorsList.Green, ct: ct); - if (!logResult.IsSuccess) - { - return Result.FromError(logResult.Error); - } - - var embed = new EmbedBuilder().WithSmallTitle( - string.Format(Messages.UserUnmuted, target.GetTag()), target) - .WithColour(ColorsList.Green).Build(); - - return await _feedback.SendContextualEmbedResultAsync(embed, ct); + return unmuteResult; } }