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;
|
2023-10-12 18:37:25 +03:00
|
|
|
using Octobot.Extensions;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Services;
|
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;
|
|
|
|
using Remora.Discord.Commands.Attributes;
|
|
|
|
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;
|
|
|
|
using Remora.Rest.Core;
|
|
|
|
using Remora.Results;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot.Commands;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles the command to clear messages in a channel: /clear.
|
|
|
|
/// </summary>
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-08-02 23:51:16 +03:00
|
|
|
public class ClearCommandGroup : 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 GuildDataService _guildData;
|
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
|
|
|
private readonly UtilityService _utility;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
public ClearCommandGroup(
|
2023-08-02 23:51:16 +03:00
|
|
|
IDiscordRestChannelAPI channelApi, ICommandContext context, GuildDataService guildData,
|
|
|
|
FeedbackService feedback, IDiscordRestUserAPI userApi, UtilityService utility)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
_channelApi = channelApi;
|
|
|
|
_context = context;
|
2023-08-02 23:51:16 +03:00
|
|
|
_guildData = guildData;
|
|
|
|
_feedback = feedback;
|
2023-07-09 16:32:14 +03:00
|
|
|
_userApi = userApi;
|
2023-07-20 10:44:07 +03:00
|
|
|
_utility = utility;
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A slash command that clears messages in the channel it was executed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="amount">The amount of messages to clear.</param>
|
|
|
|
/// <returns>
|
|
|
|
/// A feedback sending result which may or may not have succeeded. A successful result does not mean that any messages
|
|
|
|
/// were cleared and vice-versa.
|
|
|
|
/// </returns>
|
|
|
|
[Command("clear", "очистить")]
|
2023-07-18 15:25:02 +03:00
|
|
|
[DiscordDefaultMemberPermissions(DiscordPermission.ManageMessages)]
|
|
|
|
[DiscordDefaultDMPermission(false)]
|
2023-07-09 16:32:14 +03:00
|
|
|
[RequireContext(ChannelContext.Guild)]
|
|
|
|
[RequireDiscordPermission(DiscordPermission.ManageMessages)]
|
|
|
|
[RequireBotDiscordPermissions(DiscordPermission.ManageMessages)]
|
|
|
|
[Description("Remove multiple messages")]
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-07-20 10:44:07 +03:00
|
|
|
public async Task<Result> ExecuteClear(
|
2023-07-09 16:32:14 +03:00
|
|
|
[Description("Number of messages to remove (2-100)")] [MinValue(2)] [MaxValue(100)]
|
2023-08-02 23:51:16 +03:00
|
|
|
int amount)
|
|
|
|
{
|
2023-10-04 18:21:10 +03:00
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var executorId))
|
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
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
// The bot's avatar is used when sending messages
|
|
|
|
var botResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
if (!botResult.IsDefined(out var bot))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-10-04 18:21:10 +03:00
|
|
|
return Result.FromError(botResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken);
|
|
|
|
if (!executorResult.IsDefined(out var executor))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-10-04 18:21:10 +03:00
|
|
|
return Result.FromError(executorResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
var messagesResult = await _channelApi.GetChannelMessagesAsync(
|
|
|
|
channelId, limit: amount + 1, ct: CancellationToken);
|
|
|
|
if (!messagesResult.IsDefined(out var messages))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-10-04 18:21:10 +03:00
|
|
|
return Result.FromError(messagesResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 10:44:07 +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);
|
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
return await ClearMessagesAsync(executor, amount, data, channelId, messages, bot, CancellationToken);
|
2023-07-20 10:44:07 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-20 10:44:07 +03:00
|
|
|
private async Task<Result> ClearMessagesAsync(
|
2023-10-04 18:21:10 +03:00
|
|
|
IUser executor, int amount, GuildData data, Snowflake channelId, IReadOnlyList<IMessage> messages, IUser bot,
|
|
|
|
CancellationToken ct = default)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var idList = new List<Snowflake>(messages.Count);
|
2023-07-20 10:44:07 +03:00
|
|
|
var builder = new StringBuilder().AppendLine(Mention.Channel(channelId)).AppendLine();
|
2023-09-30 16:58:32 +03:00
|
|
|
for (var i = messages.Count - 1; i >= 1; i--) // '>= 1' to skip last message ('Octobot is thinking...')
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var message = messages[i];
|
|
|
|
idList.Add(message.ID);
|
|
|
|
builder.AppendLine(string.Format(Messages.MessageFrom, Mention.User(message.Author)));
|
|
|
|
builder.Append(message.Content.InBlockCode());
|
|
|
|
}
|
|
|
|
|
2023-07-20 10:44:07 +03:00
|
|
|
var title = string.Format(Messages.MessagesCleared, amount.ToString());
|
2023-07-09 16:32:14 +03:00
|
|
|
var description = builder.ToString();
|
|
|
|
|
|
|
|
var deleteResult = await _channelApi.BulkDeleteMessagesAsync(
|
2023-10-04 18:21:10 +03:00
|
|
|
channelId, idList, executor.GetTag().EncodeHeader(), ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!deleteResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(deleteResult.Error);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-20 10:44:07 +03:00
|
|
|
var logResult = _utility.LogActionAsync(
|
2023-10-04 18:21:10 +03:00
|
|
|
data.Settings, channelId, executor, title, description, bot, ColorsList.Red, false, ct);
|
2023-07-20 10:44:07 +03:00
|
|
|
if (!logResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 10:44:07 +03:00
|
|
|
return Result.FromError(logResult.Error);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(title, bot)
|
2023-07-09 16:32:14 +03:00
|
|
|
.WithColour(ColorsList.Green).Build();
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
}
|