Minecraft-Console-Client/MinecraftClient/ChatBots/PlayerListLogger.cs

58 lines
1.5 KiB
C#
Raw Normal View History

using System;
using System.Text;
2022-10-05 15:02:30 +08:00
using Tomlet.Attributes;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// This bot sends a /list command every X seconds and save the result.
/// </summary>
public class PlayerListLogger : ChatBot
{
2022-10-05 15:02:30 +08:00
public static Configs Config = new();
2022-10-05 15:02:30 +08:00
[TomlDoNotInlineObject]
public class Configs
{
2022-10-05 15:02:30 +08:00
[NonSerialized]
private const string BotName = "PlayerListLogger";
public bool Enabled = false;
public string File = "playerlog.txt";
[TomlInlineComment("$ChatBot.PlayerListLogger.Delay$")]
2022-10-06 18:12:32 +08:00
public double Delay = 60;
2022-10-05 15:02:30 +08:00
public void OnSettingUpdate()
{
2022-10-05 15:39:42 +08:00
File ??= string.Empty;
2022-10-06 18:12:32 +08:00
if (Delay < 1.0)
Delay = 1.0;
2022-10-05 15:02:30 +08:00
}
}
2022-10-05 15:02:30 +08:00
private int count = 0;
public override void Update()
{
count++;
2022-10-08 17:56:32 +08:00
if (count >= Settings.DoubleToTick(Config.Delay))
{
DateTime now = DateTime.Now;
LogDebugToConsole("Saving Player List");
StringBuilder sb = new();
sb.AppendLine(string.Format("[{0}/{1}/{2} {3}:{4}]", now.Year, now.Month, now.Day, now.Hour, now.Minute));
sb.AppendLine(string.Join(", ", GetOnlinePlayers())).AppendLine();
2022-10-05 15:02:30 +08:00
System.IO.File.AppendAllText(Settings.Config.AppVar.ExpandVars(Config.File), sb.ToString());
count = 0;
}
}
}
}