2023-02-16 20:34:59 +00:00
|
|
|
|
using SharpChat.Misuzu;
|
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.Commands {
|
|
|
|
|
public class PardonUserCommand : IChatCommand {
|
|
|
|
|
private readonly MisuzuClient Misuzu;
|
|
|
|
|
|
|
|
|
|
public PardonUserCommand(MisuzuClient msz) {
|
|
|
|
|
Misuzu = msz ?? throw new ArgumentNullException(nameof(msz));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsMatch(ChatCommandContext ctx) {
|
|
|
|
|
return ctx.NameEquals("pardon")
|
|
|
|
|
|| ctx.NameEquals("unban");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispatch(ChatCommandContext ctx) {
|
|
|
|
|
if(!ctx.User.Can(ChatUserPermissions.BanUser | ChatUserPermissions.KickUser)) {
|
2024-05-14 19:11:09 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool unbanUserTargetIsName = true;
|
2024-05-10 19:18:55 +00:00
|
|
|
|
string? unbanUserTarget = ctx.Args.FirstOrDefault();
|
2023-02-16 20:34:59 +00:00
|
|
|
|
if(string.IsNullOrWhiteSpace(unbanUserTarget)) {
|
2024-05-14 19:11:09 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
ChatUser? unbanUser = ctx.Chat.Users.FirstOrDefault(u => u.NameEquals(unbanUserTarget));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
if(unbanUser == null && long.TryParse(unbanUserTarget, out long unbanUserId)) {
|
|
|
|
|
unbanUserTargetIsName = false;
|
2023-02-19 22:27:08 +00:00
|
|
|
|
unbanUser = ctx.Chat.Users.FirstOrDefault(u => u.UserId == unbanUserId);
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(unbanUser != null)
|
|
|
|
|
unbanUserTarget = unbanUser.UserId.ToString();
|
|
|
|
|
|
|
|
|
|
Task.Run(async () => {
|
2024-05-10 19:18:55 +00:00
|
|
|
|
MisuzuBanInfo? banInfo = await Misuzu.CheckBanAsync(unbanUserTarget, userIdIsName: unbanUserTargetIsName);
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
if(banInfo?.IsBanned != true || banInfo.HasExpired) {
|
2024-05-14 22:17:25 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new KickBanNoRecordErrorPacket(unbanUserTarget));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool wasBanned = await Misuzu.RevokeBanAsync(banInfo, MisuzuClient.BanRevokeKind.UserId);
|
|
|
|
|
if(wasBanned)
|
2024-05-14 22:17:25 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new PardonResponsePacket(unbanUserTarget));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
else
|
2024-05-14 22:17:25 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new KickBanNoRecordErrorPacket(unbanUserTarget));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}).Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|