From 453bbdf9e4bb5cdf35d7d64b5b04ea47c37ee05c Mon Sep 17 00:00:00 2001 From: jetsparrow Date: Sat, 17 Aug 2019 23:09:40 +0300 Subject: [PATCH] fixed main to die on ctrl-c, added logs --- AntiAntiSwearingBot/AntiAntiSwearingBot.cs | 2 +- AntiAntiSwearingBot/Program.cs | 24 ++++++++++++++++++++-- AntiAntiSwearingBot/SearchDictionary.cs | 2 ++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/AntiAntiSwearingBot/AntiAntiSwearingBot.cs b/AntiAntiSwearingBot/AntiAntiSwearingBot.cs index 89d98d6..f052156 100644 --- a/AntiAntiSwearingBot/AntiAntiSwearingBot.cs +++ b/AntiAntiSwearingBot/AntiAntiSwearingBot.cs @@ -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() { diff --git a/AntiAntiSwearingBot/Program.cs b/AntiAntiSwearingBot/Program.cs index dc86087..a981e6b 100644 --- a/AntiAntiSwearingBot/Program.cs +++ b/AntiAntiSwearingBot/Program.cs @@ -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("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(); diff --git a/AntiAntiSwearingBot/SearchDictionary.cs b/AntiAntiSwearingBot/SearchDictionary.cs index b4be9a1..553c595 100644 --- a/AntiAntiSwearingBot/SearchDictionary.cs +++ b/AntiAntiSwearingBot/SearchDictionary.cs @@ -21,6 +21,8 @@ namespace AntiAntiSwearingBot words = File.ReadAllLines(path).ToList(); } + public int Count => words.Count; + public void Save() { if (File.Exists(tmppath))