2022-01-18 20:38:15 +03:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
using Discord.WebSocket;
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
|
|
|
|
|
public class BanCommand : Command {
|
2022-06-06 18:39:47 +03:00
|
|
|
|
public override string[] Aliases { get; } = { "ban", "бан" };
|
2022-05-14 16:12:24 +03:00
|
|
|
|
public override int ArgsLengthRequired => 2;
|
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
public override async Task Run(SocketCommandContext context, string[] args) {
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var toBan = Utils.ParseUser(args[0]);
|
|
|
|
|
|
|
|
|
|
if (toBan == null) {
|
|
|
|
|
Error(Messages.UserDoesntExist, false);
|
|
|
|
|
return;
|
2022-01-18 20:38:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var guild = context.Guild;
|
2022-06-06 18:39:47 +03:00
|
|
|
|
var author = (SocketGuildUser)context.User;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
|
|
|
|
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.BanMembers);
|
2022-07-15 19:23:45 +03:00
|
|
|
|
if (permissionCheckResponse is not "") {
|
2022-05-14 16:12:24 +03:00
|
|
|
|
Error(permissionCheckResponse, true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var reason = Utils.JoinString(ref args, 2);
|
|
|
|
|
var memberToBan = Utils.ParseMember(guild, args[0]);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
|
if (memberToBan != null) {
|
|
|
|
|
var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref memberToBan);
|
2022-07-15 19:23:45 +03:00
|
|
|
|
if (interactionCheckResponse is not "") {
|
2022-05-14 16:12:24 +03:00
|
|
|
|
Error(interactionCheckResponse, true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var duration = Utils.GetTimeSpan(ref args[1]) ?? TimeSpan.FromMilliseconds(-1);
|
|
|
|
|
if (duration.TotalSeconds < 0) {
|
|
|
|
|
Warn(Messages.DurationParseFailed);
|
|
|
|
|
reason = Utils.JoinString(ref args, 1);
|
2022-06-06 18:39:47 +03:00
|
|
|
|
|
2022-07-15 19:23:45 +03:00
|
|
|
|
if (reason is "") {
|
2022-06-06 18:39:47 +03:00
|
|
|
|
Error(Messages.ReasonRequired, false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-05-14 16:12:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await BanUser(guild, author, toBan, duration, reason);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task BanUser(SocketGuild guild, SocketGuildUser author, SocketUser toBan, TimeSpan duration,
|
|
|
|
|
string reason) {
|
|
|
|
|
var guildBanMessage = $"({author}) {reason}";
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-02-21 20:08:55 +03:00
|
|
|
|
await Utils.SendDirectMessage(toBan,
|
2022-06-06 18:39:47 +03:00
|
|
|
|
string.Format(Messages.YouWereBanned, author.Mention, guild.Name, Utils.Wrap(reason)));
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
await guild.AddBanAsync(toBan, 0, guildBanMessage);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var feedback = string.Format(Messages.FeedbackUserBanned, toBan.Mention,
|
2022-06-06 18:39:47 +03:00
|
|
|
|
Utils.GetHumanizedTimeOffset(ref duration), Utils.Wrap(reason));
|
2022-05-14 16:12:24 +03:00
|
|
|
|
Success(feedback, author.Mention, false, false);
|
|
|
|
|
await Utils.SendFeedback(feedback, guild.Id, author.Mention, true);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-02-12 21:54:45 +03:00
|
|
|
|
if (duration.TotalSeconds > 0) {
|
2022-05-14 16:12:24 +03:00
|
|
|
|
async void DelayUnban() {
|
2022-02-12 21:54:45 +03:00
|
|
|
|
await Task.Delay(duration);
|
2022-05-14 16:12:24 +03:00
|
|
|
|
await UnbanCommand.UnbanUser(guild, guild.CurrentUser, toBan, Messages.PunishmentExpired);
|
|
|
|
|
}
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
2022-06-06 18:39:47 +03:00
|
|
|
|
new Task(DelayUnban).Start();
|
2022-05-14 16:12:24 +03:00
|
|
|
|
}
|
2022-01-18 20:38:15 +03:00
|
|
|
|
}
|
2022-07-15 19:23:45 +03:00
|
|
|
|
}
|