1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-29 10:39:53 +03:00

another two or three refactors

This commit is contained in:
l1ttleO 2022-05-14 18:12:24 +05:00
parent 868b6bcaa7
commit 790f77aa49
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
21 changed files with 1447 additions and 944 deletions

View file

@ -1,54 +1,46 @@
using Discord;
using Discord.Commands;
// ReSharper disable UnusedType.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable ClassNeverInstantiated.Global
using Discord.WebSocket;
namespace Boyfriend.Commands;
public class ClearCommand : Command {
public override async Task Run(SocketCommandContext context, string[] args) {
var user = context.User;
public override string[] Aliases { get; } = {"clear", "purge", "очистить", "стереть"};
public override int ArgsLengthRequired => 1;
int toDelete;
try {
toDelete = Convert.ToInt32(args[0]);
} catch (Exception e) when (e is FormatException or OverflowException) {
throw new ApplicationException(Messages.ClearInvalidAmountSpecified);
public override async Task Run(SocketCommandContext context, string[] args) {
var user = (SocketGuildUser) context.User;
if (context.Channel is not SocketTextChannel channel) throw new Exception();
var permissionCheckResponse = CommandHandler.HasPermission(ref user, GuildPermission.ManageMessages);
if (permissionCheckResponse != "") {
Error(permissionCheckResponse, true);
return;
}
if (context.Channel is not ITextChannel channel) return;
await CommandHandler.CheckPermissions(context.Guild.GetUser(user.Id), GuildPermission.ManageMessages);
if (!int.TryParse(args[0], out var toDelete)) {
Error(Messages.ClearInvalidAmountSpecified, false);
return;
}
switch (toDelete) {
case < 1:
throw new ApplicationException(Messages.ClearNegativeAmount);
Error(Messages.ClearNegativeAmount, false);
break;
case > 200:
throw new ApplicationException(Messages.ClearAmountTooLarge);
default: {
Error(Messages.ClearAmountTooLarge, false);
break;
default:
var messages = await channel.GetMessagesAsync(toDelete + 1).FlattenAsync();
await channel.DeleteMessagesAsync(messages, Utils.GetRequestOptions(Utils.GetNameAndDiscrim(user)));
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);
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(context.Guild),
string.Format(Messages.MessagesDeleted, user.Mention, toDelete + 1,
Utils.MentionChannel(context.Channel.Id)));
break;
}
}
}
public override List<string> GetAliases() {
return new List<string> {"clear", "purge", "очистить", "стереть"};
}
public override int GetArgumentsAmountRequired() {
return 1;
}
public override string GetSummary() {
return "Очищает сообщения";
}
}
}