1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-19 16:33:36 +03:00

feat: profile /unmute

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-12-31 13:40:15 +05:00
parent b34903aead
commit aa5fb836f6
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF

View file

@ -97,6 +97,7 @@ public class MuteCommandGroup : CommandGroup
_profiler.Pop();
_profiler.Push("executor_get");
// Needed to get the tag and avatar
var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken);
if (!executorResult.IsDefined(out var executor))
{
@ -281,61 +282,83 @@ public class MuteCommandGroup : CommandGroup
[Description("Unmute reason")] [MaxLength(256)]
string reason)
{
_profiler.Push("unmute_command");
_profiler.Push("preparation");
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var executorId))
{
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
return _profiler.ReportWithResult(new ArgumentInvalidError(nameof(_context),
"Unable to retrieve necessary IDs from command context"));
}
_profiler.Push("current_user_get");
// The bot's avatar is used when sending error messages
var botResult = await _userApi.GetCurrentUserAsync(CancellationToken);
if (!botResult.IsDefined(out var bot))
{
return Result.FromError(botResult);
return _profiler.ReportWithResult(Result.FromError(botResult));
}
_profiler.Pop();
_profiler.Push("executor_get");
// Needed to get the tag and avatar
var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken);
if (!executorResult.IsDefined(out var executor))
{
return Result.FromError(executorResult);
return _profiler.ReportWithResult(Result.FromError(executorResult));
}
_profiler.Pop();
_profiler.Push("guild_data_get");
var data = await _guildData.GetData(guildId, CancellationToken);
Messages.Culture = GuildSettings.Language.Get(data.Settings);
_profiler.Pop();
_profiler.Push("target_get");
var memberResult = await _guildApi.GetGuildMemberAsync(guildId, target.ID, CancellationToken);
if (!memberResult.IsSuccess)
{
_profiler.Push("not_found_send");
var embed = new EmbedBuilder().WithSmallTitle(Messages.UserNotFoundShort, bot)
.WithColour(ColorsList.Red).Build();
return await _feedback.SendContextualEmbedResultAsync(embed, ct: CancellationToken);
return _profiler.ReportWithResult(
await _feedback.SendContextualEmbedResultAsync(embed, ct: CancellationToken));
}
return await RemoveMuteAsync(executor, target, reason, guildId, data, channelId, bot, CancellationToken);
_profiler.Pop();
_profiler.Pop();
return _profiler.ReportWithResult(await RemoveMuteAsync(executor, target, reason, guildId, data, channelId, bot,
CancellationToken));
}
private async Task<Result> RemoveMuteAsync(
IUser executor, IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId,
IUser bot, CancellationToken ct = default)
{
_profiler.Push("main");
_profiler.Push("interactions_check");
var interactionResult
= await _utility.CheckInteractionsAsync(
guildId, executor.ID, target.ID, "Unmute", ct);
if (!interactionResult.IsSuccess)
{
return Result.FromError(interactionResult);
return _profiler.PopWithResult(Result.FromError(interactionResult));
}
_profiler.Pop();
if (interactionResult.Entity is not null)
{
_profiler.Push("interactions_failed_send");
var failedEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, bot)
.WithColour(ColorsList.Red).Build();
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct);
return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct));
}
_profiler.Push("guild_member_get");
var guildMemberResult = await _guildApi.GetGuildMemberAsync(guildId, target.ID, ct);
_profiler.Pop();
DateTimeOffset? communicationDisabledUntil = null;
if (guildMemberResult.IsDefined(out var guildMember))
{
@ -347,51 +370,58 @@ public class MuteCommandGroup : CommandGroup
if (!wasMuted)
{
_profiler.Push("not_muted_send");
var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.UserNotMuted, bot)
.WithColour(ColorsList.Red).Build();
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct);
return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct));
}
var removeMuteRoleAsync =
await RemoveMuteRoleAsync(executor, target, reason, guildId, memberData, CancellationToken);
if (!removeMuteRoleAsync.IsSuccess)
{
return Result.FromError(removeMuteRoleAsync.Error);
return _profiler.PopWithResult(Result.FromError(removeMuteRoleAsync.Error));
}
var removeTimeoutResult =
await RemoveTimeoutAsync(executor, target, reason, guildId, communicationDisabledUntil, CancellationToken);
if (!removeTimeoutResult.IsSuccess)
{
return Result.FromError(removeTimeoutResult.Error);
return _profiler.PopWithResult(Result.FromError(removeTimeoutResult.Error));
}
_profiler.Push("embed_send");
var title = string.Format(Messages.UserUnmuted, target.GetTag());
var description = MarkdownExtensions.BulletPoint(string.Format(Messages.DescriptionActionReason, reason));
_profiler.Push("action_log");
var logResult = _utility.LogActionAsync(
data.Settings, channelId, executor, title, description, target, ColorsList.Green, ct: ct);
if (!logResult.IsSuccess)
{
return Result.FromError(logResult.Error);
return _profiler.PopWithResult(Result.FromError(logResult.Error));
}
_profiler.Pop();
var embed = new EmbedBuilder().WithSmallTitle(
string.Format(Messages.UserUnmuted, target.GetTag()), target)
.WithColour(ColorsList.Green).Build();
return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct);
return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(embed, ct: ct));
}
private async Task<Result> RemoveMuteRoleAsync(
IUser executor, IUser target, string reason, Snowflake guildId, MemberData memberData,
CancellationToken ct = default)
{
_profiler.Push("mute_role_remove");
if (memberData.MutedUntil is null)
{
return Result.FromSuccess();
return _profiler.PopWithResult(Result.FromSuccess());
}
_profiler.Push("guild_member_modify");
var unmuteResult = await _guildApi.ModifyGuildMemberAsync(
guildId, target.ID, roles: memberData.Roles.ConvertAll(r => r.ToSnowflake()),
reason: $"({executor.GetTag()}) {reason}".EncodeHeader(), ct: ct);
@ -400,21 +430,25 @@ public class MuteCommandGroup : CommandGroup
memberData.MutedUntil = null;
}
return unmuteResult;
_profiler.Pop();
return _profiler.PopWithResult(unmuteResult);
}
private async Task<Result> RemoveTimeoutAsync(
IUser executor, IUser target, string reason, Snowflake guildId, DateTimeOffset? communicationDisabledUntil,
CancellationToken ct = default)
{
_profiler.Push("timeout_remove");
if (communicationDisabledUntil is null)
{
return Result.FromSuccess();
return _profiler.PopWithResult(Result.FromSuccess());
}
_profiler.Push("guild_member_modify");
var unmuteResult = await _guildApi.ModifyGuildMemberAsync(
guildId, target.ID, reason: $"({executor.GetTag()}) {reason}".EncodeHeader(),
communicationDisabledUntil: null, ct: ct);
return unmuteResult;
_profiler.Pop();
return _profiler.PopWithResult(unmuteResult);
}
}