2023-02-16 21:34:59 +01:00
|
|
|
|
using SharpChat.Misuzu;
|
2025-04-25 20:12:42 +00:00
|
|
|
|
using SharpChat.S2CPackets;
|
2023-02-16 21:34:59 +01:00
|
|
|
|
|
2025-04-26 12:51:08 +00:00
|
|
|
|
namespace SharpChat.ClientCommands {
|
|
|
|
|
public class PardonUserClientCommand(MisuzuClient msz) : ClientCommand {
|
2025-04-25 15:49:46 +00:00
|
|
|
|
private readonly MisuzuClient Misuzu = msz ?? throw new ArgumentNullException(nameof(msz));
|
2023-02-16 21:34:59 +01:00
|
|
|
|
|
2025-04-26 12:51:08 +00:00
|
|
|
|
public bool IsMatch(ClientCommandContext ctx) {
|
2023-02-16 21:34:59 +01:00
|
|
|
|
return ctx.NameEquals("pardon")
|
|
|
|
|
|| ctx.NameEquals("unban");
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-26 12:51:08 +00:00
|
|
|
|
public void Dispatch(ClientCommandContext ctx) {
|
2025-04-25 20:05:55 +00:00
|
|
|
|
long msgId = ctx.Chat.RandomSnowflake.Next();
|
|
|
|
|
|
2025-04-26 12:51:08 +00:00
|
|
|
|
if(!ctx.User.Can(UserPermissions.BanUser | UserPermissions.KickUser)) {
|
2025-04-25 20:12:42 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
|
2023-02-16 21:34:59 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool unbanUserTargetIsName = true;
|
2025-04-25 18:18:13 +00:00
|
|
|
|
string? unbanUserTarget = ctx.Args.FirstOrDefault();
|
2023-02-16 21:34:59 +01:00
|
|
|
|
if(string.IsNullOrWhiteSpace(unbanUserTarget)) {
|
2025-04-25 20:12:42 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_FORMAT_ERROR));
|
2023-02-16 21:34:59 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-26 12:51:08 +00:00
|
|
|
|
User? unbanUser = ctx.Chat.Users.FirstOrDefault(u => u.NameEquals(unbanUserTarget));
|
2023-02-16 21:34:59 +01:00
|
|
|
|
if(unbanUser == null && long.TryParse(unbanUserTarget, out long unbanUserId)) {
|
|
|
|
|
unbanUserTargetIsName = false;
|
2023-02-19 23:27:08 +01:00
|
|
|
|
unbanUser = ctx.Chat.Users.FirstOrDefault(u => u.UserId == unbanUserId);
|
2023-02-16 21:34:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(unbanUser != null)
|
|
|
|
|
unbanUserTarget = unbanUser.UserId.ToString();
|
|
|
|
|
|
|
|
|
|
Task.Run(async () => {
|
2025-04-25 18:18:13 +00:00
|
|
|
|
MisuzuBanInfo? banInfo = await Misuzu.CheckBanAsync(unbanUserTarget, userIdIsName: unbanUserTargetIsName);
|
|
|
|
|
if(banInfo is null) {
|
2025-04-25 20:12:42 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.GENERIC_ERROR, true));
|
2025-04-25 18:18:13 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-16 21:34:59 +01:00
|
|
|
|
|
|
|
|
|
if(!banInfo.IsBanned || banInfo.HasExpired) {
|
2025-04-25 20:12:42 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.USER_NOT_BANNED, true, unbanUserTarget));
|
2023-02-16 21:34:59 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool wasBanned = await Misuzu.RevokeBanAsync(banInfo, MisuzuClient.BanRevokeKind.UserId);
|
|
|
|
|
if(wasBanned)
|
2025-04-25 20:12:42 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.USER_UNBANNED, false, unbanUserTarget));
|
2023-02-16 21:34:59 +01:00
|
|
|
|
else
|
2025-04-25 20:12:42 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.USER_NOT_BANNED, true, unbanUserTarget));
|
2023-02-16 21:34:59 +01:00
|
|
|
|
}).Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|