fixed main to die on ctrl-c, added logs

This commit is contained in:
jetsparrow 2019-08-17 23:09:40 +03:00
parent a4e7b836b8
commit 453bbdf9e4
3 changed files with 25 additions and 3 deletions

View File

@ -27,7 +27,7 @@ namespace AntiAntiSwearingBot
TelegramBotClient Client { get; set; }
ChatCommandRouter Router { get; set; }
User Me { get; set; }
public User Me { get; private set; }
public async Task Init()
{

View File

@ -1,4 +1,5 @@
using System;
using System.Threading;
namespace AntiAntiSwearingBot
{
@ -13,17 +14,36 @@ namespace AntiAntiSwearingBot
ErrorInvalidCommandLine = 0x100
};
static void Log(string m) => Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff}|{m}");
public static int Main(string[] args)
{
try
{
Log("AntiAntiSwearBot starting....");
var cfg = Config.Load<Config>("aasb.cfg.json", "aasb.cfg.secret.json");
var dict = new SearchDictionary(cfg);
Log($"{dict.Count} words loaded.");
var bot = new AntiAntiSwearingBot(cfg, dict);
bot.Init().Wait();
Console.WriteLine("AntiAntiSwear started. Press any key to exit...");
Log($"Connected to Telegram as @{bot.Me.Username}");
Log("AntiAntiSwearBot started! Press Ctrl-C to exit.");
Environment.ExitCode = (int)ExitCode.ErrorRunning;
Console.ReadKey();
ManualResetEvent quitEvent = new ManualResetEvent(false);
try
{
Console.CancelKeyPress += (sender, eArgs) => // ctrl-c
{
eArgs.Cancel = true;
quitEvent.Set();
};
}
catch { }
quitEvent.WaitOne(Timeout.Infinite);
Console.WriteLine("Waiting for exit...");
bot.Stop().Wait();
dict.Save();

View File

@ -21,6 +21,8 @@ namespace AntiAntiSwearingBot
words = File.ReadAllLines(path).ToList();
}
public int Count => words.Count;
public void Save()
{
if (File.Exists(tmppath))