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

feat: profile /unban, fix up /ban

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-12-24 12:46:40 +05:00
parent f2ed0a1f8d
commit 995710bddd
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF

View file

@ -151,10 +151,11 @@ public class BanCommandGroup : CommandGroup
var existingBanResult = await _guildApi.GetGuildBanAsync(guild.ID, target.ID, ct);
if (existingBanResult.IsDefined())
{
var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.UserAlreadyBanned, bot)
_profiler.Push("already_banned_send");
var alreadyBannedEmbed = new EmbedBuilder().WithSmallTitle(Messages.UserAlreadyBanned, bot)
.WithColour(ColorsList.Red).Build();
return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct));
return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(alreadyBannedEmbed, ct: ct));
}
_profiler.Pop();
@ -163,17 +164,18 @@ public class BanCommandGroup : CommandGroup
= await _utility.CheckInteractionsAsync(guild.ID, executor.ID, target.ID, "Ban", ct);
if (!interactionResult.IsSuccess)
{
return Result.FromError(interactionResult);
return _profiler.PopWithResult(Result.FromError(interactionResult));
}
_profiler.Pop();
if (interactionResult.Entity is not null)
{
_profiler.Push("error_embed_send");
var errorEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, bot)
_profiler.Push("interaction_failed_send");
var interactionFailedEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, bot)
.WithColour(ColorsList.Red).Build();
return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(errorEmbed, ct: ct));
return _profiler.PopWithResult(
await _feedback.SendContextualEmbedResultAsync(interactionFailedEmbed, ct: ct));
}
_profiler.Push("builder_construction");
@ -215,7 +217,7 @@ public class BanCommandGroup : CommandGroup
ct: ct);
if (!banResult.IsSuccess)
{
return Result.FromError(banResult.Error);
return _profiler.PopWithResult(Result.FromError(banResult.Error));
}
var memberData = data.GetOrCreateMemberData(target.ID);
@ -234,7 +236,7 @@ public class BanCommandGroup : CommandGroup
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();
@ -268,54 +270,72 @@ public class BanCommandGroup : CommandGroup
[Description("Unban reason")] [MaxLength(256)]
string reason)
{
_profiler.Push("unban_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();
return await UnbanUserAsync(executor, target, reason, guildId, data, channelId, bot, CancellationToken);
_profiler.Pop();
return _profiler.ReportWithResult(await UnbanUserAsync(executor, target, reason, guildId, data, channelId, bot,
CancellationToken));
}
private async Task<Result> UnbanUserAsync(
IUser executor, IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId,
IUser bot, CancellationToken ct = default)
{
_profiler.Push("main");
_profiler.Push("guild_ban_get");
var existingBanResult = await _guildApi.GetGuildBanAsync(guildId, target.ID, ct);
if (!existingBanResult.IsDefined())
{
var errorEmbed = new EmbedBuilder().WithSmallTitle(Messages.UserNotBanned, bot)
_profiler.Push("not_banned_send");
var notBannedEmbed = new EmbedBuilder().WithSmallTitle(Messages.UserNotBanned, bot)
.WithColour(ColorsList.Red).Build();
return await _feedback.SendContextualEmbedResultAsync(errorEmbed, ct: ct);
return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(notBannedEmbed, ct: ct));
}
_profiler.Pop();
_profiler.Push("guild_ban_remove");
var unbanResult = await _guildApi.RemoveGuildBanAsync(
guildId, target.ID, $"({executor.GetTag()}) {reason}".EncodeHeader(),
ct);
if (!unbanResult.IsSuccess)
{
return Result.FromError(unbanResult.Error);
return _profiler.PopWithResult(Result.FromError(unbanResult.Error));
}
data.GetOrCreateMemberData(target.ID).BannedUntil = null;
_profiler.Pop();
_profiler.Push("embed_send");
var embed = new EmbedBuilder().WithSmallTitle(
string.Format(Messages.UserUnbanned, target.GetTag()), target)
.WithColour(ColorsList.Green).Build();
@ -323,13 +343,16 @@ public class BanCommandGroup : CommandGroup
var title = string.Format(Messages.UserUnbanned, target.GetTag());
var description =
new StringBuilder().AppendBulletPoint(string.Format(Messages.DescriptionActionReason, reason));
_profiler.Push("action_log");
var logResult = _utility.LogActionAsync(
data.Settings, channelId, executor, title, description.ToString(), target, ColorsList.Green, ct: ct);
if (!logResult.IsSuccess)
{
return Result.FromError(logResult.Error);
return _profiler.PopWithResult(Result.FromError(logResult.Error));
}
return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct);
_profiler.Pop();
return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(embed, ct: ct));
}
}