2021-12-07 21:27:27 +03:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2021-12-10 14:25:29 +03:00
|
|
|
|
|
2021-12-07 21:27:27 +03:00
|
|
|
|
// ReSharper disable UnusedType.Global
|
|
|
|
|
// ReSharper disable UnusedMember.Global
|
2021-12-10 14:25:29 +03:00
|
|
|
|
// ReSharper disable ClassNeverInstantiated.Global
|
2021-12-07 21:27:27 +03:00
|
|
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
|
|
|
|
|
public class BanModule : ModuleBase<SocketCommandContext> {
|
|
|
|
|
|
|
|
|
|
[Command("ban")]
|
|
|
|
|
[Summary("Банит пользователя")]
|
|
|
|
|
[Alias("бан")]
|
2021-12-15 09:19:14 +03:00
|
|
|
|
public async Task Run(string user, string durationString, [Remainder]string reason) {
|
|
|
|
|
TimeSpan duration;
|
|
|
|
|
try {
|
|
|
|
|
duration = TimeSpan.Parse(durationString);
|
|
|
|
|
} catch (Exception e) when (e is ArgumentNullException or FormatException or OverflowException) {
|
|
|
|
|
duration = TimeSpan.FromMilliseconds(-1);
|
|
|
|
|
reason = durationString + reason;
|
|
|
|
|
}
|
|
|
|
|
var author = Context.Guild.GetUser(Context.User.Id);
|
|
|
|
|
var toBan = await Utils.ParseUser(user);
|
|
|
|
|
await CommandHandler.CheckPermissions(author, GuildPermission.BanMembers);
|
|
|
|
|
var memberToBan = Context.Guild.GetUser(toBan.Id);
|
|
|
|
|
if (memberToBan != null)
|
|
|
|
|
await CommandHandler.CheckInteractions(author, memberToBan);
|
|
|
|
|
BanUser(Context.Guild, Context.Guild.GetUser(Context.User.Id), toBan, duration, reason);
|
2021-12-10 14:25:29 +03:00
|
|
|
|
}
|
2021-12-07 21:27:27 +03:00
|
|
|
|
|
2021-12-15 09:19:14 +03:00
|
|
|
|
public static async void BanUser(IGuild guild, IGuildUser author, IUser toBan, TimeSpan duration, string reason) {
|
2021-12-07 21:27:27 +03:00
|
|
|
|
var authorMention = author.Mention;
|
2021-12-10 14:25:29 +03:00
|
|
|
|
await Utils.SendDirectMessage(toBan, $"Тебя забанил {author.Mention} на сервере {guild.Name} за `{reason}`");
|
|
|
|
|
var guildBanMessage = $"({author.Username}#{author.Discriminator}) {reason}";
|
|
|
|
|
await guild.AddBanAsync(toBan, 0, guildBanMessage);
|
|
|
|
|
var notification = $"{authorMention} банит {toBan.Mention} за {Utils.WrapInline(reason)}";
|
|
|
|
|
await Utils.SilentSendAsync(guild.GetSystemChannelAsync().Result, notification);
|
|
|
|
|
await Utils.SilentSendAsync(Utils.GetAdminLogChannel(), notification);
|
|
|
|
|
var task = new Task(() => UnbanModule.UnbanUser(guild, guild.GetCurrentUserAsync().Result, toBan,
|
|
|
|
|
"Время наказания истекло"));
|
|
|
|
|
await Utils.StartDelayed(task, duration, () => guild.GetBanAsync(toBan).Result != null);
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
|
|
|
|
}
|