2023-01-18 17:39:24 +03:00
|
|
|
using Boyfriend.Data;
|
2022-12-06 18:33:46 +03:00
|
|
|
using Discord;
|
2022-05-14 16:12:24 +03:00
|
|
|
using Discord.WebSocket;
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
2022-09-18 17:41:29 +03:00
|
|
|
public sealed class BanCommand : ICommand {
|
|
|
|
public string[] Aliases { get; } = { "ban", "бан" };
|
2022-05-14 16:12:24 +03:00
|
|
|
|
2022-09-18 17:41:29 +03:00
|
|
|
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
2023-01-18 17:39:24 +03:00
|
|
|
var toBan = cmd.GetUser(args, cleanArgs, 0);
|
2022-11-11 22:59:11 +03:00
|
|
|
if (toBan is null || !cmd.HasPermission(GuildPermission.BanMembers)) return;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
2023-02-14 20:32:29 +03:00
|
|
|
var memberToBan = cmd.GetMember(toBan.Value.Id);
|
2022-11-11 22:59:11 +03:00
|
|
|
if (memberToBan is not null && !cmd.CanInteractWith(memberToBan, "Ban")) return;
|
2022-06-06 18:39:47 +03:00
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
var duration = CommandProcessor.GetTimeSpan(args, 1);
|
|
|
|
var reason = cmd.GetRemaining(args, duration.TotalSeconds < 1 ? 1 : 2, "BanReason");
|
2023-02-14 20:32:29 +03:00
|
|
|
if (reason is not null) await BanUserAsync(cmd, toBan.Value, duration, reason);
|
2022-05-14 16:12:24 +03:00
|
|
|
}
|
|
|
|
|
2023-02-14 20:32:29 +03:00
|
|
|
private static async Task BanUserAsync(
|
|
|
|
CommandProcessor cmd, (ulong Id, SocketUser? User) toBan, TimeSpan duration,
|
|
|
|
string reason) {
|
2022-08-30 18:15:01 +03:00
|
|
|
var author = cmd.Context.User;
|
|
|
|
var guild = cmd.Context.Guild;
|
2023-02-14 20:32:29 +03:00
|
|
|
if (toBan.User is not null)
|
|
|
|
await Utils.SendDirectMessage(
|
|
|
|
toBan.User,
|
2023-01-18 17:39:24 +03:00
|
|
|
string.Format(Messages.YouWereBanned, author.Mention, guild.Name, Utils.Wrap(reason)));
|
2022-02-02 16:14:26 +03:00
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
var guildBanMessage = $"({author}) {reason}";
|
2023-02-14 20:32:29 +03:00
|
|
|
await guild.AddBanAsync(toBan.Id, 0, guildBanMessage);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
2023-02-14 20:32:29 +03:00
|
|
|
var memberData = GuildData.Get(guild).MemberData[toBan.Id];
|
2023-01-18 17:39:24 +03:00
|
|
|
memberData.BannedUntil
|
2023-04-27 20:14:30 +03:00
|
|
|
= duration.TotalSeconds < 1 ? DateTimeOffset.MaxValue : DateTimeOffset.UtcNow.Add(duration);
|
2023-01-18 17:39:24 +03:00
|
|
|
memberData.Roles.Clear();
|
|
|
|
|
|
|
|
cmd.ConfigWriteScheduled = true;
|
|
|
|
|
2023-02-14 20:32:29 +03:00
|
|
|
var feedback = string.Format(
|
|
|
|
Messages.FeedbackUserBanned, $"<@{toBan.Id.ToString()}>",
|
2023-01-18 17:39:24 +03:00
|
|
|
Utils.GetHumanizedTimeSpan(duration), Utils.Wrap(reason));
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(feedback, ReplyEmojis.Banned);
|
2022-08-30 18:15:01 +03:00
|
|
|
cmd.Audit(feedback);
|
2022-01-18 20:38:15 +03:00
|
|
|
}
|
2022-09-18 17:41:29 +03:00
|
|
|
}
|