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/Boyfriend/Commands/ClearCommand.cs

46 lines
1.6 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
2022-05-14 16:12:24 +03:00
using Discord.WebSocket;
namespace Boyfriend.Commands;
public class ClearCommand : Command {
2022-05-14 16:12:24 +03:00
public override string[] Aliases { get; } = {"clear", "purge", "очистить", "стереть"};
public override int ArgsLengthRequired => 1;
public override async Task Run(SocketCommandContext context, string[] args) {
2022-05-14 16:12:24 +03:00
var user = (SocketGuildUser) context.User;
2022-02-02 16:14:26 +03:00
2022-05-14 16:12:24 +03:00
if (context.Channel is not SocketTextChannel channel) throw new Exception();
2022-05-14 16:12:24 +03:00
var permissionCheckResponse = CommandHandler.HasPermission(ref user, GuildPermission.ManageMessages);
if (permissionCheckResponse != "") {
Error(permissionCheckResponse, true);
return;
}
2022-02-02 16:14:26 +03:00
2022-05-14 16:12:24 +03:00
if (!int.TryParse(args[0], out var toDelete)) {
Error(Messages.ClearInvalidAmountSpecified, false);
return;
}
2022-02-02 16:14:26 +03:00
switch (toDelete) {
case < 1:
2022-05-14 16:12:24 +03:00
Error(Messages.ClearNegativeAmount, false);
break;
case > 200:
2022-05-14 16:12:24 +03:00
Error(Messages.ClearAmountTooLarge, false);
break;
default:
var messages = await channel.GetMessagesAsync(toDelete + 1).FlattenAsync();
2022-02-02 16:14:26 +03:00
2022-05-14 16:12:24 +03:00
await channel.DeleteMessagesAsync(messages, Utils.GetRequestOptions(user.ToString()!));
await Utils.SendFeedback(
string.Format(Messages.FeedbackMessagesCleared, (toDelete + 1).ToString(), channel.Mention),
context.Guild.Id, user.Mention);
2022-02-02 16:14:26 +03:00
break;
}
}
2022-05-14 16:12:24 +03:00
}