mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-04-19 16:33:36 +03:00
feat: profile /clear
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
995710bddd
commit
0dc23400b9
1 changed files with 42 additions and 17 deletions
|
@ -4,6 +4,7 @@ using JetBrains.Annotations;
|
||||||
using Octobot.Data;
|
using Octobot.Data;
|
||||||
using Octobot.Extensions;
|
using Octobot.Extensions;
|
||||||
using Octobot.Services;
|
using Octobot.Services;
|
||||||
|
using Octobot.Services.Profiler;
|
||||||
using Remora.Commands.Attributes;
|
using Remora.Commands.Attributes;
|
||||||
using Remora.Commands.Groups;
|
using Remora.Commands.Groups;
|
||||||
using Remora.Discord.API.Abstractions.Objects;
|
using Remora.Discord.API.Abstractions.Objects;
|
||||||
|
@ -29,12 +30,13 @@ public class ClearCommandGroup : CommandGroup
|
||||||
private readonly ICommandContext _context;
|
private readonly ICommandContext _context;
|
||||||
private readonly IFeedbackService _feedback;
|
private readonly IFeedbackService _feedback;
|
||||||
private readonly GuildDataService _guildData;
|
private readonly GuildDataService _guildData;
|
||||||
|
private readonly Profiler _profiler;
|
||||||
private readonly IDiscordRestUserAPI _userApi;
|
private readonly IDiscordRestUserAPI _userApi;
|
||||||
private readonly Utility _utility;
|
private readonly Utility _utility;
|
||||||
|
|
||||||
public ClearCommandGroup(
|
public ClearCommandGroup(
|
||||||
IDiscordRestChannelAPI channelApi, ICommandContext context, GuildDataService guildData,
|
IDiscordRestChannelAPI channelApi, ICommandContext context, GuildDataService guildData,
|
||||||
IFeedbackService feedback, IDiscordRestUserAPI userApi, Utility utility)
|
IFeedbackService feedback, IDiscordRestUserAPI userApi, Utility utility, Profiler profiler)
|
||||||
{
|
{
|
||||||
_channelApi = channelApi;
|
_channelApi = channelApi;
|
||||||
_context = context;
|
_context = context;
|
||||||
|
@ -42,6 +44,7 @@ public class ClearCommandGroup : CommandGroup
|
||||||
_feedback = feedback;
|
_feedback = feedback;
|
||||||
_userApi = userApi;
|
_userApi = userApi;
|
||||||
_utility = utility;
|
_utility = utility;
|
||||||
|
_profiler = profiler;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -66,41 +69,56 @@ public class ClearCommandGroup : CommandGroup
|
||||||
int amount,
|
int amount,
|
||||||
IUser? author = null)
|
IUser? author = null)
|
||||||
{
|
{
|
||||||
|
_profiler.Push("clear_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 messages
|
// The bot's avatar is used when sending 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");
|
||||||
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("channel_messages_get");
|
||||||
var messagesResult = await _channelApi.GetChannelMessagesAsync(
|
var messagesResult = await _channelApi.GetChannelMessagesAsync(
|
||||||
channelId, limit: amount + 1, ct: CancellationToken);
|
channelId, limit: amount + 1, ct: CancellationToken);
|
||||||
if (!messagesResult.IsDefined(out var messages))
|
if (!messagesResult.IsDefined(out var messages))
|
||||||
{
|
{
|
||||||
return Result.FromError(messagesResult);
|
return _profiler.ReportWithResult(Result.FromError(messagesResult));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_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();
|
||||||
|
|
||||||
return await ClearMessagesAsync(executor, author, data, channelId, messages, bot, CancellationToken);
|
_profiler.Pop();
|
||||||
|
return _profiler.ReportWithResult(await ClearMessagesAsync(executor, author, data, channelId, messages, bot,
|
||||||
|
CancellationToken));
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<Result> ClearMessagesAsync(
|
private async Task<Result> ClearMessagesAsync(
|
||||||
IUser executor, IUser? author, GuildData data, Snowflake channelId, IReadOnlyList<IMessage> messages, IUser bot,
|
IUser executor, IUser? author, GuildData data, Snowflake channelId, IReadOnlyList<IMessage> messages, IUser bot,
|
||||||
CancellationToken ct = default)
|
CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
|
_profiler.Push("main");
|
||||||
|
_profiler.Push("builder_construction");
|
||||||
var idList = new List<Snowflake>(messages.Count);
|
var idList = new List<Snowflake>(messages.Count);
|
||||||
var builder = new StringBuilder().AppendLine(Mention.Channel(channelId)).AppendLine();
|
var builder = new StringBuilder().AppendLine(Mention.Channel(channelId)).AppendLine();
|
||||||
for (var i = messages.Count - 1; i >= 1; i--) // '>= 1' to skip last message ('Octobot is thinking...')
|
for (var i = messages.Count - 1; i >= 1; i--) // '>= 1' to skip last message ('Octobot is thinking...')
|
||||||
|
@ -116,26 +134,32 @@ public class ClearCommandGroup : CommandGroup
|
||||||
builder.Append(message.Content.InBlockCode());
|
builder.Append(message.Content.InBlockCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_profiler.Pop();
|
||||||
if (idList.Count == 0)
|
if (idList.Count == 0)
|
||||||
{
|
{
|
||||||
var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.NoMessagesToClear, bot)
|
_profiler.Push("no_messages_send");
|
||||||
|
var noMessagesEmbed = new EmbedBuilder().WithSmallTitle(Messages.NoMessagesToClear, bot)
|
||||||
.WithColour(ColorsList.Red).Build();
|
.WithColour(ColorsList.Red).Build();
|
||||||
|
|
||||||
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct);
|
return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(noMessagesEmbed, ct: ct));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_profiler.Push("messages_bulk_delete");
|
||||||
|
var deleteResult = await _channelApi.BulkDeleteMessagesAsync(
|
||||||
|
channelId, idList, executor.GetTag().EncodeHeader(), ct);
|
||||||
|
if (!deleteResult.IsSuccess)
|
||||||
|
{
|
||||||
|
return _profiler.PopWithResult(Result.FromError(deleteResult.Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
_profiler.Pop();
|
||||||
|
_profiler.Push("embed_send");
|
||||||
var title = author is not null
|
var title = author is not null
|
||||||
? string.Format(Messages.MessagesClearedFiltered, idList.Count.ToString(), author.GetTag())
|
? string.Format(Messages.MessagesClearedFiltered, idList.Count.ToString(), author.GetTag())
|
||||||
: string.Format(Messages.MessagesCleared, idList.Count.ToString());
|
: string.Format(Messages.MessagesCleared, idList.Count.ToString());
|
||||||
var description = builder.ToString();
|
var description = builder.ToString();
|
||||||
|
|
||||||
var deleteResult = await _channelApi.BulkDeleteMessagesAsync(
|
_profiler.Push("action_log");
|
||||||
channelId, idList, executor.GetTag().EncodeHeader(), ct);
|
|
||||||
if (!deleteResult.IsSuccess)
|
|
||||||
{
|
|
||||||
return Result.FromError(deleteResult.Error);
|
|
||||||
}
|
|
||||||
|
|
||||||
var logResult = _utility.LogActionAsync(
|
var logResult = _utility.LogActionAsync(
|
||||||
data.Settings, channelId, executor, title, description, bot, ColorsList.Red, false, ct);
|
data.Settings, channelId, executor, title, description, bot, ColorsList.Red, false, ct);
|
||||||
if (!logResult.IsSuccess)
|
if (!logResult.IsSuccess)
|
||||||
|
@ -143,9 +167,10 @@ public class ClearCommandGroup : CommandGroup
|
||||||
return Result.FromError(logResult.Error);
|
return Result.FromError(logResult.Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_profiler.Pop();
|
||||||
|
|
||||||
var embed = new EmbedBuilder().WithSmallTitle(title, bot)
|
var embed = new EmbedBuilder().WithSmallTitle(title, bot)
|
||||||
.WithColour(ColorsList.Green).Build();
|
.WithColour(ColorsList.Green).Build();
|
||||||
|
return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(embed, ct: ct));
|
||||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue