using SharpChat.Misuzu; using SharpChat.Packet; using System; using System.Linq; using System.Threading.Tasks; namespace SharpChat.Commands { public class PardonUserCommand : IUserCommand { private readonly MisuzuClient Misuzu; public PardonUserCommand(MisuzuClient msz) { Misuzu = msz; } public bool IsMatch(UserCommandContext ctx) { return ctx.NameEquals("pardon") || ctx.NameEquals("unban"); } public void Dispatch(UserCommandContext ctx) { if(!ctx.User.Permissions.HasFlag(UserPermissions.KickUser) && !ctx.User.Permissions.HasFlag(UserPermissions.BanUser)) { ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name)); return; } bool unbanUserTargetIsName = true; string? unbanUserTarget = ctx.Args.FirstOrDefault(); if(string.IsNullOrWhiteSpace(unbanUserTarget)) { ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket()); return; } UserInfo? unbanUser = ctx.Chat.Users.Values.FirstOrDefault(u => u.NameEquals(unbanUserTarget)); if(unbanUser == null && long.TryParse(unbanUserTarget, out long unbanUserId)) { unbanUserTargetIsName = false; unbanUser = ctx.Chat.Users.Values.FirstOrDefault(u => u.UserId == unbanUserId); } if(unbanUser != null) unbanUserTarget = unbanUser.UserId.ToString(); Task.Run(async () => { MisuzuBanInfo? banInfo = await Misuzu.CheckBanAsync(unbanUserTarget, userIdIsName: unbanUserTargetIsName); if(banInfo?.IsBanned != true || banInfo.HasExpired) { ctx.Chat.SendTo(ctx.User, new KickBanNoRecordErrorPacket(unbanUserTarget)); return; } bool wasBanned = await Misuzu.RevokeBanAsync(banInfo, MisuzuClient.BanRevokeKind.UserId); if(wasBanned) ctx.Chat.SendTo(ctx.User, new PardonResponsePacket(unbanUserTarget)); else ctx.Chat.SendTo(ctx.User, new KickBanNoRecordErrorPacket(unbanUserTarget)); }).Wait(); } } }