This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
OctobotStealth/Boyfriend/Commands/BanCommand.cs

76 lines
2.6 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
2022-05-14 16:12:24 +03:00
using Discord.WebSocket;
namespace Boyfriend.Commands;
public class BanCommand : Command {
public override string[] Aliases { get; } = { "ban", "бан" };
2022-05-14 16:12:24 +03:00
public override int ArgsLengthRequired => 2;
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-05-14 16:12:24 +03:00
var guild = context.Guild;
var author = (SocketGuildUser)context.User;
2022-05-14 16:12:24 +03:00
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.BanMembers);
if (permissionCheckResponse is not "") {
2022-05-14 16:12:24 +03:00
Error(permissionCheckResponse, true);
return;
}
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);
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);
if (reason is "") {
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
await Utils.SendDirectMessage(toBan,
string.Format(Messages.YouWereBanned, author.Mention, guild.Name, Utils.Wrap(reason)));
2022-02-02 16:14:26 +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,
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
if (duration.TotalSeconds > 0) {
2022-05-14 16:12:24 +03:00
async void DelayUnban() {
await Task.Delay(duration);
2022-05-14 16:12:24 +03:00
await UnbanCommand.UnbanUser(guild, guild.CurrentUser, toBan, Messages.PunishmentExpired);
}
new Task(DelayUnban).Start();
2022-05-14 16:12:24 +03:00
}
}
}