This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
OctobotStealth/Commands/ClearCommand.cs

31 lines
1.2 KiB
C#
Raw Normal View History

using System.Diagnostics;
using Discord;
2022-05-14 16:12:24 +03:00
using Discord.WebSocket;
namespace Boyfriend.Commands;
public sealed class ClearCommand : ICommand {
public string[] Aliases { get; } = { "clear", "purge", "очистить", "стереть" };
2022-05-14 16:12:24 +03:00
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
if (cmd.Context.Channel is not SocketTextChannel channel) throw new UnreachableException();
2022-02-02 16:14:26 +03:00
if (!cmd.HasPermission(GuildPermission.ManageMessages)) return;
var toDelete = cmd.GetNumberRange(cleanArgs, 0, 1, 200, "ClearAmount");
if (toDelete is null) return;
var messages = await channel.GetMessagesAsync((int)(toDelete + 1)).FlattenAsync();
2022-02-02 16:14:26 +03:00
var user = (SocketGuildUser)cmd.Context.User;
var msgArray = messages.Reverse().ToArray();
await channel.DeleteMessagesAsync(msgArray, Utils.GetRequestOptions(user.ToString()!));
2022-02-02 16:14:26 +03:00
foreach (var msg in msgArray.Where(m => !m.Author.IsBot))
cmd.Audit(
string.Format(
Messages.CachedMessageDeleted, msg.Author.Mention,
Utils.MentionChannel(channel.Id),
Utils.Wrap(msg.CleanContent)), false);
}
}