2022-01-18 20:38:15 +03:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
|
|
|
|
|
// ReSharper disable UnusedType.Global
|
|
|
|
|
// ReSharper disable UnusedMember.Global
|
|
|
|
|
// ReSharper disable ClassNeverInstantiated.Global
|
|
|
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
|
|
|
|
|
public class BanCommand : Command {
|
|
|
|
|
public override async Task Run(SocketCommandContext context, string[] args) {
|
|
|
|
|
var reason = Utils.JoinString(args, 1);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
TimeSpan duration;
|
|
|
|
|
try {
|
|
|
|
|
duration = Utils.GetTimeSpan(args[1]);
|
|
|
|
|
reason = Utils.JoinString(args, 2);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
} catch (Exception e) when (e is ArgumentNullException or FormatException or OverflowException) {
|
2022-01-18 20:38:15 +03:00
|
|
|
|
duration = TimeSpan.FromMilliseconds(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await BanUser(context.Guild, context.Channel as ITextChannel, context.Guild.GetUser(context.User.Id),
|
2022-02-02 16:14:26 +03:00
|
|
|
|
await Utils.ParseUser(args[0]), duration, reason);
|
2022-01-18 20:38:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task BanUser(IGuild guild, ITextChannel? channel, IGuildUser author, IUser toBan,
|
|
|
|
|
TimeSpan duration, string reason) {
|
|
|
|
|
var authorMention = author.Mention;
|
2022-02-02 16:14:26 +03:00
|
|
|
|
var guildBanMessage = $"({Utils.GetNameAndDiscrim(author)}) {reason}";
|
|
|
|
|
var memberToBan = await guild.GetUserAsync(toBan.Id);
|
|
|
|
|
var notification = string.Format(Messages.UserBanned, authorMention, toBan.Mention, Utils.WrapInline(reason));
|
|
|
|
|
|
|
|
|
|
await CommandHandler.CheckPermissions(author, GuildPermission.BanMembers);
|
|
|
|
|
if (memberToBan != null)
|
|
|
|
|
await CommandHandler.CheckInteractions(author, memberToBan);
|
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
await Utils.SendDirectMessage(toBan, string.Format(Messages.YouWereBanned, author.Mention, guild.Name,
|
|
|
|
|
Utils.WrapInline(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-01-18 20:38:15 +03:00
|
|
|
|
await Utils.SilentSendAsync(channel, string.Format(Messages.BanResponse, toBan.Mention,
|
|
|
|
|
Utils.WrapInline(reason)));
|
|
|
|
|
await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification);
|
|
|
|
|
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(guild), notification);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
|
|
|
|
async void UnbanWhenExpires() {
|
|
|
|
|
try {
|
|
|
|
|
await UnbanCommand.UnbanUser(guild, null, await guild.GetCurrentUserAsync(), toBan,
|
|
|
|
|
Messages.PunishmentExpired);
|
|
|
|
|
} catch (ApplicationException) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Utils.StartDelayed(new Task(UnbanWhenExpires), duration);
|
2022-01-18 20:38:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override List<string> GetAliases() {
|
|
|
|
|
return new List<string> {"ban", "бан"};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetArgumentsAmountRequired() {
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string GetSummary() {
|
|
|
|
|
return "Банит пользователя";
|
|
|
|
|
}
|
2022-02-02 16:14:26 +03:00
|
|
|
|
}
|