1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

Fix /unmute not checking if the target is muted (#143)

Closes #142

---------

Signed-off-by: Macintosh II <mctaylxrs@outlook.com>
This commit is contained in:
Macintxsh 2023-10-03 17:25:28 +03:00 committed by GitHub
parent bae92fc84b
commit 7cf200d8de
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23

View file

@ -285,17 +285,11 @@ public class MuteCommandGroup : CommandGroup
return await _feedback.SendContextualEmbedResultAsync(embed, CancellationToken); return await _feedback.SendContextualEmbedResultAsync(embed, CancellationToken);
} }
if (data.GetOrCreateMemberData(target.ID).MutedUntil is not null) return await RemoveMuteAsync(
{
return await RemoveMuteRoleUserAsync(
target, reason, guildId, data, channelId, user, currentUser, CancellationToken); target, reason, guildId, data, channelId, user, currentUser, CancellationToken);
} }
return await RemoveTimeoutUserAsync( private async Task<Result> RemoveMuteAsync(
target, reason, guildId, data, channelId, user, currentUser, CancellationToken);
}
private async Task<Result> RemoveMuteRoleUserAsync(
IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId, IUser user, IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId, IUser user,
IUser currentUser, CancellationToken ct = default) IUser currentUser, CancellationToken ct = default)
{ {
@ -315,14 +309,36 @@ public class MuteCommandGroup : CommandGroup
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct); return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct);
} }
var memberData = data.GetOrCreateMemberData(target.ID); var guildMemberResult = await _guildApi.GetGuildMemberAsync(guildId, target.ID, ct);
var unmuteResult = await _guildApi.ModifyGuildMemberAsync( DateTimeOffset? communicationDisabledUntil = null;
guildId, target.ID, roles: memberData.Roles.ConvertAll(r => r.ToSnowflake()), if (guildMemberResult.IsDefined(out var guildMember))
reason: $"({user.GetTag()}) {reason}".EncodeHeader(), ct: ct);
memberData.MutedUntil = null;
if (!unmuteResult.IsSuccess)
{ {
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()); var title = string.Format(Messages.UserUnmuted, target.GetTag());
@ -341,47 +357,37 @@ public class MuteCommandGroup : CommandGroup
return await _feedback.SendContextualEmbedResultAsync(embed, ct); return await _feedback.SendContextualEmbedResultAsync(embed, ct);
} }
private async Task<Result> RemoveTimeoutUserAsync( private async Task<Result> RemoveMuteRoleAsync(
IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId, IUser user, IUser target, string reason, Snowflake guildId, MemberData memberData, IUser user, CancellationToken ct = default)
IUser currentUser, CancellationToken ct = default)
{ {
var interactionResult if (memberData.MutedUntil is null)
= await _utility.CheckInteractionsAsync(
guildId, user.ID, target.ID, "Unmute", ct);
if (!interactionResult.IsSuccess)
{ {
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) memberData.MutedUntil = null;
.WithColour(ColorsList.Red).Build(); }
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct); return unmuteResult;
}
private async Task<Result> 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( var unmuteResult = await _guildApi.ModifyGuildMemberAsync(
guildId, target.ID, reason: $"({user.GetTag()}) {reason}".EncodeHeader(), guildId, target.ID, reason: $"({user.GetTag()}) {reason}".EncodeHeader(),
communicationDisabledUntil: null, ct: ct); communicationDisabledUntil: null, ct: ct);
if (!unmuteResult.IsSuccess) return unmuteResult;
{
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);
} }
} }