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)) {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool unbanUserTargetIsName = true;
|
|
|
|
|
string unbanUserTarget = ctx.Args.FirstOrDefault();
|
|
|
|
|
if(string.IsNullOrWhiteSpace(unbanUserTarget)) {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChatUser unbanUser;
|
|
|
|
|
lock(ctx.Chat.UsersAccess)
|
|
|
|
|
unbanUser = ctx.Chat.Users.FirstOrDefault(u => u.NameEquals(unbanUserTarget));
|
|
|
|
|
if(unbanUser == null && long.TryParse(unbanUserTarget, out long unbanUserId)) {
|
|
|
|
|
unbanUserTargetIsName = false;
|
|
|
|
|
lock(ctx.Chat.UsersAccess)
|
|
|
|
|
unbanUser = ctx.Chat.Users.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 || banInfo.HasExpired) {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.USER_NOT_BANNED, true, unbanUserTarget));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool wasBanned = await Misuzu.RevokeBanAsync(banInfo, MisuzuClient.BanRevokeKind.UserId);
|
|
|
|
|
if(wasBanned)
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.USER_UNBANNED, false, unbanUserTarget));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
else
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.USER_NOT_BANNED, true, unbanUserTarget));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}).Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|