2023-07-09 16:32:14 +03:00
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Text;
|
2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Data;
|
|
|
|
using Octobot.Services;
|
|
|
|
using Octobot.Services.Update;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Commands.Attributes;
|
|
|
|
using Remora.Commands.Groups;
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
2023-07-18 15:25:02 +03:00
|
|
|
using Remora.Discord.Commands.Attributes;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.Commands.Conditions;
|
|
|
|
using Remora.Discord.Commands.Contexts;
|
|
|
|
using Remora.Discord.Commands.Feedback.Services;
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Extensions.Formatting;
|
2023-07-20 00:08:44 +03:00
|
|
|
using Remora.Rest.Core;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Results;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot.Commands;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles commands related to ban management: /ban and /unban.
|
|
|
|
/// </summary>
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-08-02 23:51:16 +03:00
|
|
|
public class BanCommandGroup : CommandGroup
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
private readonly IDiscordRestChannelAPI _channelApi;
|
2023-08-02 23:51:16 +03:00
|
|
|
private readonly ICommandContext _context;
|
|
|
|
private readonly FeedbackService _feedback;
|
|
|
|
private readonly IDiscordRestGuildAPI _guildApi;
|
|
|
|
private readonly GuildDataService _guildData;
|
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
|
|
|
private readonly UtilityService _utility;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
public BanCommandGroup(
|
2023-08-02 23:51:16 +03:00
|
|
|
ICommandContext context, IDiscordRestChannelAPI channelApi, GuildDataService guildData,
|
|
|
|
FeedbackService feedback, IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi,
|
|
|
|
UtilityService utility)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
_context = context;
|
|
|
|
_channelApi = channelApi;
|
2023-08-02 23:51:16 +03:00
|
|
|
_guildData = guildData;
|
|
|
|
_feedback = feedback;
|
2023-07-09 16:32:14 +03:00
|
|
|
_guildApi = guildApi;
|
|
|
|
_userApi = userApi;
|
|
|
|
_utility = utility;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A slash command that bans a Discord user with the specified reason.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="target">The user to ban.</param>
|
|
|
|
/// <param name="duration">The duration for this ban. The user will be automatically unbanned after this duration.</param>
|
|
|
|
/// <param name="reason">
|
|
|
|
/// The reason for this ban. Must be encoded with <see cref="Extensions.EncodeHeader" /> when passed to
|
|
|
|
/// <see cref="IDiscordRestGuildAPI.CreateGuildBanAsync" />.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// A feedback sending result which may or may not have succeeded. A successful result does not mean that the user
|
|
|
|
/// was banned and vice-versa.
|
|
|
|
/// </returns>
|
2023-07-20 00:08:44 +03:00
|
|
|
/// <seealso cref="ExecuteUnban" />
|
2023-07-09 16:32:14 +03:00
|
|
|
[Command("ban", "бан")]
|
2023-07-18 15:25:02 +03:00
|
|
|
[DiscordDefaultMemberPermissions(DiscordPermission.BanMembers)]
|
|
|
|
[DiscordDefaultDMPermission(false)]
|
2023-07-09 16:32:14 +03:00
|
|
|
[RequireContext(ChannelContext.Guild)]
|
|
|
|
[RequireDiscordPermission(DiscordPermission.BanMembers)]
|
|
|
|
[RequireBotDiscordPermissions(DiscordPermission.BanMembers)]
|
|
|
|
[Description("Ban user")]
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-07-20 23:41:02 +03:00
|
|
|
public async Task<Result> ExecuteBanAsync(
|
2023-08-02 23:51:16 +03:00
|
|
|
[Description("User to ban")] IUser target,
|
|
|
|
[Description("Ban reason")] string reason,
|
|
|
|
[Description("Ban duration")] TimeSpan? duration = null)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-09 16:32:14 +03:00
|
|
|
// The current user's avatar is used when sending error messages
|
|
|
|
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
if (!currentUserResult.IsDefined(out var currentUser))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(currentUserResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-24 14:57:41 +03:00
|
|
|
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
2023-07-20 00:08:44 +03:00
|
|
|
if (!userResult.IsDefined(out var user))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
return Result.FromError(userResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-24 14:57:41 +03:00
|
|
|
var guildResult = await _guildApi.GetGuildAsync(guildId, ct: CancellationToken);
|
2023-07-20 00:08:44 +03:00
|
|
|
if (!guildResult.IsDefined(out var guild))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
return Result.FromError(guildResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 00:08:44 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
var data = await _guildData.GetData(guild.ID, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
|
|
|
|
|
|
|
return await BanUserAsync(
|
2023-07-24 14:57:41 +03:00
|
|
|
target, reason, duration, guild, data, channelId, user, currentUser, CancellationToken);
|
2023-07-20 00:08:44 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-20 00:08:44 +03:00
|
|
|
private async Task<Result> BanUserAsync(
|
2023-08-02 23:51:16 +03:00
|
|
|
IUser target, string reason, TimeSpan? duration, IGuild guild, GuildData data, Snowflake channelId,
|
|
|
|
IUser user, IUser currentUser, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-20 23:41:02 +03:00
|
|
|
var existingBanResult = await _guildApi.GetGuildBanAsync(guild.ID, target.ID, ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (existingBanResult.IsDefined())
|
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.UserAlreadyBanned, currentUser)
|
2023-07-09 16:32:14 +03:00
|
|
|
.WithColour(ColorsList.Red).Build();
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var interactionResult
|
2023-07-20 23:41:02 +03:00
|
|
|
= await _utility.CheckInteractionsAsync(guild.ID, user.ID, target.ID, "Ban", ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!interactionResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(interactionResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
if (interactionResult.Entity is not null)
|
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
var errorEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, currentUser)
|
2023-07-09 16:32:14 +03:00
|
|
|
.WithColour(ColorsList.Red).Build();
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(errorEmbed, ct);
|
2023-07-20 00:08:44 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-09-29 18:36:16 +03:00
|
|
|
var builder = new StringBuilder().Append("- ")
|
|
|
|
.AppendLine(string.Format(Messages.DescriptionActionReason, reason));
|
2023-07-20 00:08:44 +03:00
|
|
|
if (duration is not null)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-09-29 18:36:16 +03:00
|
|
|
builder.Append("- ").Append(
|
2023-07-20 00:08:44 +03:00
|
|
|
string.Format(
|
|
|
|
Messages.DescriptionActionExpiresAt,
|
|
|
|
Markdown.Timestamp(DateTimeOffset.UtcNow.Add(duration.Value))));
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 00:08:44 +03:00
|
|
|
var title = string.Format(Messages.UserBanned, target.GetTag());
|
|
|
|
var description = builder.ToString();
|
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
var dmChannelResult = await _userApi.CreateDMAsync(target.ID, ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (dmChannelResult.IsDefined(out var dmChannel))
|
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
var dmEmbed = new EmbedBuilder().WithGuildTitle(guild)
|
|
|
|
.WithTitle(Messages.YouWereBanned)
|
|
|
|
.WithDescription(description)
|
|
|
|
.WithActionFooter(user)
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
.Build();
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-20 00:08:44 +03:00
|
|
|
if (!dmEmbed.IsDefined(out var dmBuilt))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
return Result.FromError(dmEmbed);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
await _channelApi.CreateMessageAsync(dmChannel.ID, embeds: new[] { dmBuilt }, ct: ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 00:08:44 +03:00
|
|
|
var banResult = await _guildApi.CreateGuildBanAsync(
|
|
|
|
guild.ID, target.ID, reason: $"({user.GetTag()}) {reason}".EncodeHeader(),
|
2023-07-20 23:41:02 +03:00
|
|
|
ct: ct);
|
2023-07-20 00:08:44 +03:00
|
|
|
if (!banResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
return Result.FromError(banResult.Error);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-08-04 16:52:54 +03:00
|
|
|
var memberData = data.GetOrCreateMemberData(target.ID);
|
2023-07-20 00:08:44 +03:00
|
|
|
memberData.BannedUntil
|
|
|
|
= duration is not null ? DateTimeOffset.UtcNow.Add(duration.Value) : DateTimeOffset.MaxValue;
|
|
|
|
memberData.Roles.Clear();
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(
|
|
|
|
title, target)
|
|
|
|
.WithColour(ColorsList.Green).Build();
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
var logResult = _utility.LogActionAsync(
|
2023-07-23 23:07:36 +03:00
|
|
|
data.Settings, channelId, user, title, description, target, ColorsList.Red, ct: ct);
|
2023-07-20 10:36:21 +03:00
|
|
|
if (!logResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 10:36:21 +03:00
|
|
|
return Result.FromError(logResult.Error);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 00:08:44 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A slash command that unbans a Discord user with the specified reason.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="target">The user to unban.</param>
|
|
|
|
/// <param name="reason">
|
|
|
|
/// The reason for this unban. Must be encoded with <see cref="Extensions.EncodeHeader" /> when passed to
|
|
|
|
/// <see cref="IDiscordRestGuildAPI.RemoveGuildBanAsync" />.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// A feedback sending result which may or may not have succeeded. A successful result does not mean that the user
|
|
|
|
/// was unbanned and vice-versa.
|
|
|
|
/// </returns>
|
2023-07-20 23:41:02 +03:00
|
|
|
/// <seealso cref="ExecuteBanAsync" />
|
2023-08-05 21:02:40 +03:00
|
|
|
/// <seealso cref="MemberUpdateService.TickMemberDataAsync" />
|
2023-07-09 16:32:14 +03:00
|
|
|
[Command("unban")]
|
2023-07-18 15:25:02 +03:00
|
|
|
[DiscordDefaultMemberPermissions(DiscordPermission.BanMembers)]
|
|
|
|
[DiscordDefaultDMPermission(false)]
|
2023-07-09 16:32:14 +03:00
|
|
|
[RequireContext(ChannelContext.Guild)]
|
|
|
|
[RequireDiscordPermission(DiscordPermission.BanMembers)]
|
|
|
|
[RequireBotDiscordPermissions(DiscordPermission.BanMembers)]
|
|
|
|
[Description("Unban user")]
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-07-20 00:08:44 +03:00
|
|
|
public async Task<Result> ExecuteUnban(
|
2023-08-02 23:51:16 +03:00
|
|
|
[Description("User to unban")] IUser target,
|
|
|
|
[Description("Unban reason")] string reason)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-09 16:32:14 +03:00
|
|
|
// The current user's avatar is used when sending error messages
|
|
|
|
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
if (!currentUserResult.IsDefined(out var currentUser))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(currentUserResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 00:08:44 +03:00
|
|
|
// Needed to get the tag and avatar
|
2023-07-24 14:57:41 +03:00
|
|
|
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
2023-07-20 00:08:44 +03:00
|
|
|
if (!userResult.IsDefined(out var user))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
return Result.FromError(userResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 00:08:44 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
var data = await _guildData.GetData(guildId, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
|
|
|
|
|
|
|
return await UnbanUserAsync(
|
2023-07-24 14:57:41 +03:00
|
|
|
target, reason, guildId, data, channelId, user, currentUser, CancellationToken);
|
2023-07-20 00:08:44 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-20 00:08:44 +03:00
|
|
|
private async Task<Result> UnbanUserAsync(
|
2023-08-02 23:51:16 +03:00
|
|
|
IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId, IUser user,
|
|
|
|
IUser currentUser, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-20 23:41:02 +03:00
|
|
|
var existingBanResult = await _guildApi.GetGuildBanAsync(guildId, target.ID, ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!existingBanResult.IsDefined())
|
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
var errorEmbed = new EmbedBuilder().WithSmallTitle(Messages.UserNotBanned, currentUser)
|
2023-07-09 16:32:14 +03:00
|
|
|
.WithColour(ColorsList.Red).Build();
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(errorEmbed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var unbanResult = await _guildApi.RemoveGuildBanAsync(
|
2023-07-20 00:08:44 +03:00
|
|
|
guildId, target.ID, $"({user.GetTag()}) {reason}".EncodeHeader(),
|
2023-07-20 23:41:02 +03:00
|
|
|
ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!unbanResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(unbanResult.Error);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-09-27 12:47:43 +03:00
|
|
|
data.GetOrCreateMemberData(target.ID).BannedUntil = null;
|
|
|
|
|
2023-07-20 00:08:44 +03:00
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(
|
2023-07-09 16:32:14 +03:00
|
|
|
string.Format(Messages.UserUnbanned, target.GetTag()), target)
|
|
|
|
.WithColour(ColorsList.Green).Build();
|
|
|
|
|
2023-07-20 00:08:44 +03:00
|
|
|
var title = string.Format(Messages.UserUnbanned, target.GetTag());
|
2023-09-29 18:36:16 +03:00
|
|
|
var description = new StringBuilder().Append("- ")
|
|
|
|
.Append(string.Format(Messages.DescriptionActionReason, reason));
|
2023-07-23 23:07:36 +03:00
|
|
|
var logResult = _utility.LogActionAsync(
|
2023-09-29 18:36:16 +03:00
|
|
|
data.Settings, channelId, user, title, description.ToString(), target, ColorsList.Green, ct: ct);
|
2023-07-20 00:08:44 +03:00
|
|
|
if (!logResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
return Result.FromError(logResult.Error);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
}
|