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