2021-11-08 05:46:26 +00:00
|
|
|
|
namespace WorldTime;
|
|
|
|
|
|
|
|
|
|
class Program {
|
2023-06-12 02:09:04 +00:00
|
|
|
|
private static WorldTime _bot = null!;
|
2021-11-08 05:46:26 +00:00
|
|
|
|
private static readonly DateTimeOffset _botStartTime = DateTimeOffset.UtcNow;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the amount of time the program has been running in a human-readable format.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string BotUptime => (DateTimeOffset.UtcNow - _botStartTime).ToString("d' days, 'hh':'mm':'ss");
|
|
|
|
|
|
2022-08-10 01:09:08 +00:00
|
|
|
|
static async Task Main() {
|
2021-11-08 05:46:26 +00:00
|
|
|
|
Configuration? cfg = null;
|
|
|
|
|
try {
|
2022-08-10 01:09:08 +00:00
|
|
|
|
cfg = new Configuration();
|
2021-11-08 05:46:26 +00:00
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Console.WriteLine(ex);
|
2023-06-12 02:09:04 +00:00
|
|
|
|
Environment.Exit(2);
|
2021-11-08 05:46:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 04:46:58 +00:00
|
|
|
|
_bot = new WorldTime(cfg);
|
2023-06-12 02:09:04 +00:00
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += OnCancelEvent;
|
|
|
|
|
Console.CancelKeyPress += OnCancelEvent;
|
2021-11-08 05:46:26 +00:00
|
|
|
|
|
2023-06-12 02:09:04 +00:00
|
|
|
|
await _bot.StartAsync();
|
|
|
|
|
await Task.Delay(-1);
|
2021-11-08 05:46:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a formatted message to console.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Log(string source, string message) {
|
2022-08-10 00:48:14 +00:00
|
|
|
|
var ts = DateTime.Now;
|
2021-11-08 05:46:26 +00:00
|
|
|
|
var ls = new string[] { "\r\n", "\n" };
|
|
|
|
|
foreach (var item in message.Split(ls, StringSplitOptions.None))
|
2022-08-10 00:48:14 +00:00
|
|
|
|
Console.WriteLine($"{ts:s} [{source}] {item}");
|
2021-11-08 05:46:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-12 02:09:04 +00:00
|
|
|
|
private static bool _shutdownRequested = false;
|
|
|
|
|
private static void OnCancelEvent(object? sender, EventArgs e) {
|
|
|
|
|
if (e is ConsoleCancelEventArgs ce) ce.Cancel = true;
|
|
|
|
|
|
|
|
|
|
if (_shutdownRequested) return;
|
|
|
|
|
_shutdownRequested = true;
|
|
|
|
|
Log("Shutdown", "Shutting down...");
|
|
|
|
|
|
|
|
|
|
var dispose = Task.Run(_bot.Dispose);
|
|
|
|
|
if (!dispose.Wait(15000)) {
|
|
|
|
|
Log("Shutdown", "Normal shutdown is taking too long. Will force quit.");
|
|
|
|
|
Environment.ExitCode = 1;
|
2021-11-08 05:46:26 +00:00
|
|
|
|
}
|
|
|
|
|
Environment.Exit(Environment.ExitCode);
|
|
|
|
|
}
|
|
|
|
|
}
|