2023-01-18 17:39:24 +03:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using Discord;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
using Discord.WebSocket;
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
|
2022-09-18 17:41:29 +03:00
|
|
|
|
public sealed class ClearCommand : ICommand {
|
|
|
|
|
public string[] Aliases { get; } = { "clear", "purge", "очистить", "стереть" };
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
2022-09-18 17:41:29 +03:00
|
|
|
|
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
2023-01-18 17:39:24 +03:00
|
|
|
|
if (cmd.Context.Channel is not SocketTextChannel channel) throw new UnreachableException();
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
|
if (!cmd.HasPermission(GuildPermission.ManageMessages)) return;
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
2022-09-18 17:41:29 +03:00
|
|
|
|
var toDelete = cmd.GetNumberRange(cleanArgs, 0, 1, 200, "ClearAmount");
|
2022-11-11 22:59:11 +03:00
|
|
|
|
if (toDelete is null) return;
|
2022-08-30 18:15:01 +03:00
|
|
|
|
var messages = await channel.GetMessagesAsync((int)(toDelete + 1)).FlattenAsync();
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
|
var user = (SocketGuildUser)cmd.Context.User;
|
|
|
|
|
await channel.DeleteMessagesAsync(messages, Utils.GetRequestOptions(user.ToString()!));
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2023-01-18 17:39:24 +03:00
|
|
|
|
cmd.Audit(string.Format(Messages.FeedbackMessagesCleared, (toDelete + 1).ToString(),
|
|
|
|
|
Utils.MentionChannel(channel.Id)));
|
2022-01-18 20:38:15 +03:00
|
|
|
|
}
|
2022-09-18 17:41:29 +03:00
|
|
|
|
}
|