sharp-chat/SharpChat/Commands/PardonUserCommand.cs

60 lines
2.3 KiB
C#
Raw Normal View History

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;
}
public bool IsMatch(ChatCommandContext ctx) {
return ctx.NameEquals("pardon")
|| ctx.NameEquals("unban");
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Permissions.HasFlag(ChatUserPermissions.KickUser)
&& !ctx.User.Permissions.HasFlag(ChatUserPermissions.BanUser)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
bool unbanUserTargetIsName = true;
2024-05-10 19:18:55 +00:00
string? unbanUserTarget = ctx.Args.FirstOrDefault();
if(string.IsNullOrWhiteSpace(unbanUserTarget)) {
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}
ChatUser? 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 () => {
2024-05-10 19:18:55 +00:00
MisuzuBanInfo? banInfo = await Misuzu.CheckBanAsync(unbanUserTarget, userIdIsName: unbanUserTargetIsName);
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));
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));
else
2024-05-14 22:17:25 +00:00
ctx.Chat.SendTo(ctx.User, new KickBanNoRecordErrorPacket(unbanUserTarget));
}).Wait();
}
}
}