mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-04-19 16:33:36 +03:00
feat: profile /kick
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
3883ae08ae
commit
95d6524b91
1 changed files with 47 additions and 13 deletions
|
@ -4,6 +4,7 @@ using JetBrains.Annotations;
|
|||
using Octobot.Data;
|
||||
using Octobot.Extensions;
|
||||
using Octobot.Services;
|
||||
using Octobot.Services.Profiler;
|
||||
using Remora.Commands.Attributes;
|
||||
using Remora.Commands.Groups;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
|
@ -29,13 +30,14 @@ public class KickCommandGroup : CommandGroup
|
|||
private readonly IFeedbackService _feedback;
|
||||
private readonly IDiscordRestGuildAPI _guildApi;
|
||||
private readonly GuildDataService _guildData;
|
||||
private readonly Profiler _profiler;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
private readonly Utility _utility;
|
||||
|
||||
public KickCommandGroup(
|
||||
ICommandContext context, IDiscordRestChannelAPI channelApi, GuildDataService guildData,
|
||||
IFeedbackService feedback, IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi,
|
||||
Utility utility)
|
||||
Utility utility, Profiler profiler)
|
||||
{
|
||||
_context = context;
|
||||
_channelApi = channelApi;
|
||||
|
@ -44,6 +46,7 @@ public class KickCommandGroup : CommandGroup
|
|||
_guildApi = guildApi;
|
||||
_userApi = userApi;
|
||||
_utility = utility;
|
||||
_profiler = profiler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -71,103 +74,134 @@ public class KickCommandGroup : CommandGroup
|
|||
[Description("Kick reason")] [MaxLength(256)]
|
||||
string reason)
|
||||
{
|
||||
_profiler.Push("kick_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.PopWithResult(Result.FromError(botResult));
|
||||
}
|
||||
|
||||
_profiler.Pop();
|
||||
_profiler.Push("executor_get");
|
||||
var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken);
|
||||
if (!executorResult.IsDefined(out var executor))
|
||||
{
|
||||
return Result.FromError(executorResult);
|
||||
return _profiler.PopWithResult(Result.FromError(executorResult));
|
||||
}
|
||||
|
||||
_profiler.Pop();
|
||||
_profiler.Push("guild_get");
|
||||
var guildResult = await _guildApi.GetGuildAsync(guildId, ct: CancellationToken);
|
||||
if (!guildResult.IsDefined(out var guild))
|
||||
{
|
||||
return Result.FromError(guildResult);
|
||||
return _profiler.PopWithResult(Result.FromError(guildResult));
|
||||
}
|
||||
|
||||
_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);
|
||||
|
||||
_profiler.Pop();
|
||||
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 KickUserAsync(executor, target, reason, guild, channelId, data, bot, CancellationToken);
|
||||
_profiler.Pop();
|
||||
return _profiler.ReportWithResult(await KickUserAsync(executor, target, reason, guild, channelId, data, bot,
|
||||
CancellationToken));
|
||||
}
|
||||
|
||||
private async Task<Result> KickUserAsync(
|
||||
IUser executor, IUser target, string reason, IGuild guild, Snowflake channelId, GuildData data, IUser bot,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
_profiler.Push("interactions_check");
|
||||
var interactionResult
|
||||
= await _utility.CheckInteractionsAsync(guild.ID, executor.ID, target.ID, "Kick", ct);
|
||||
if (!interactionResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(interactionResult);
|
||||
return _profiler.PopWithResult(Result.FromError(interactionResult));
|
||||
}
|
||||
|
||||
_profiler.Pop();
|
||||
if (interactionResult.Entity is not null)
|
||||
{
|
||||
_profiler.Push("interaction_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("dm_create");
|
||||
var dmChannelResult = await _userApi.CreateDMAsync(target.ID, ct);
|
||||
if (dmChannelResult.IsDefined(out var dmChannel))
|
||||
{
|
||||
_profiler.Push("dm_embed_send");
|
||||
var dmEmbed = new EmbedBuilder().WithGuildTitle(guild)
|
||||
.WithTitle(Messages.YouWereKicked)
|
||||
.WithDescription(MarkdownExtensions.BulletPoint(string.Format(Messages.DescriptionActionReason, reason)))
|
||||
.WithDescription(
|
||||
MarkdownExtensions.BulletPoint(string.Format(Messages.DescriptionActionReason, reason)))
|
||||
.WithActionFooter(executor)
|
||||
.WithCurrentTimestamp()
|
||||
.WithColour(ColorsList.Red)
|
||||
.Build();
|
||||
|
||||
await _channelApi.CreateMessageWithEmbedResultAsync(dmChannel.ID, embedResult: dmEmbed, ct: ct);
|
||||
_profiler.Pop();
|
||||
}
|
||||
|
||||
_profiler.Pop();
|
||||
_profiler.Push("member_remove");
|
||||
var kickResult = await _guildApi.RemoveGuildMemberAsync(
|
||||
guild.ID, target.ID, $"({executor.GetTag()}) {reason}".EncodeHeader(),
|
||||
ct);
|
||||
if (!kickResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(kickResult.Error);
|
||||
return _profiler.PopWithResult(Result.FromError(kickResult.Error));
|
||||
}
|
||||
|
||||
var memberData = data.GetOrCreateMemberData(target.ID);
|
||||
memberData.Roles.Clear();
|
||||
memberData.Kicked = true;
|
||||
|
||||
_profiler.Pop();
|
||||
_profiler.Push("embed_send");
|
||||
var title = string.Format(Messages.UserKicked, 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.Red, 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.UserKicked, target.GetTag()), target)
|
||||
.WithColour(ColorsList.Green).Build();
|
||||
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct);
|
||||
return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(embed, ct: ct));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue