2025-04-26 22:47:57 +00:00
|
|
|
using SharpChat.SockChat.S2CPackets;
|
2025-04-28 12:29:11 +00:00
|
|
|
using ZLogger;
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
namespace SharpChat.ClientCommands;
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-05-03 02:49:51 +00:00
|
|
|
public class ShutdownRestartClientCommand(SockChatServer server, CancellationTokenSource cancellationTokenSource) : ClientCommand {
|
2025-04-26 23:15:54 +00:00
|
|
|
public bool IsMatch(ClientCommandContext ctx) {
|
|
|
|
return ctx.NameEquals("shutdown")
|
|
|
|
|| ctx.NameEquals("restart");
|
|
|
|
}
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
public async Task Dispatch(ClientCommandContext ctx) {
|
|
|
|
if(!ctx.User.UserId.Equals("1")) {
|
2025-05-03 02:49:51 +00:00
|
|
|
ctx.Logger.ZLogInformation($"{ctx.User.UserId}/{ctx.User.UserName} tried to issue /shutdown or /restart");
|
2025-04-26 23:15:54 +00:00
|
|
|
long msgId = ctx.Chat.RandomSnowflake.Next();
|
|
|
|
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
|
|
|
|
return;
|
|
|
|
}
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-28 10:46:26 +00:00
|
|
|
if(cancellationTokenSource.IsCancellationRequested)
|
2025-04-26 23:15:54 +00:00
|
|
|
return;
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-05-03 02:49:51 +00:00
|
|
|
server.IsRestarting = ctx.NameEquals("restart");
|
|
|
|
ctx.Logger.ZLogInformation($"{(server.IsRestarting ? "Restart" : "Shutdown")} requested through Sock Chat command...");
|
2025-04-26 23:15:54 +00:00
|
|
|
|
|
|
|
await ctx.Chat.Update();
|
2025-04-28 10:46:26 +00:00
|
|
|
await cancellationTokenSource.CancelAsync();
|
2023-02-16 21:34:59 +01:00
|
|
|
}
|
|
|
|
}
|