2022-08-29 19:22:42 +00:00
|
|
|
|
namespace BirthdayBot;
|
2021-10-18 07:26:25 +00:00
|
|
|
|
class Program {
|
2023-06-12 01:10:46 +00:00
|
|
|
|
private static ShardManager _bot = null!;
|
2021-10-18 23:58:22 +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");
|
2021-10-18 07:26:25 +00:00
|
|
|
|
|
2022-08-29 19:22:42 +00:00
|
|
|
|
static async Task Main() {
|
2021-10-23 00:44:53 +00:00
|
|
|
|
Configuration? cfg = null;
|
|
|
|
|
try {
|
2022-08-10 00:37:29 +00:00
|
|
|
|
cfg = new Configuration();
|
2021-10-23 00:44:53 +00:00
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Console.WriteLine(ex);
|
2023-06-12 01:10:46 +00:00
|
|
|
|
Environment.Exit(2);
|
2021-10-23 00:44:53 +00:00
|
|
|
|
}
|
2022-03-19 21:51:41 +00:00
|
|
|
|
|
2021-10-18 07:26:25 +00:00
|
|
|
|
_bot = new ShardManager(cfg);
|
2023-06-12 01:10:46 +00:00
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += OnCancelEvent;
|
|
|
|
|
Console.CancelKeyPress += OnCancelEvent;
|
2020-10-29 07:53:18 +00:00
|
|
|
|
|
2021-10-18 07:26:25 +00:00
|
|
|
|
await Task.Delay(-1);
|
|
|
|
|
}
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2021-10-18 07:26:25 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a formatted message to console.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Log(string source, string message) {
|
2022-08-10 00:46:39 +00:00
|
|
|
|
var ts = DateTime.Now;
|
2021-10-18 07:26:25 +00:00
|
|
|
|
var ls = new string[] { "\r\n", "\n" };
|
|
|
|
|
foreach (var item in message.Split(ls, StringSplitOptions.None))
|
2022-08-10 00:46:39 +00:00
|
|
|
|
Console.WriteLine($"{ts:s} [{source}] {item}");
|
2021-10-18 07:26:25 +00:00
|
|
|
|
}
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2023-06-12 01:10:46 +00:00
|
|
|
|
private static bool _shutdownRequested = false;
|
|
|
|
|
private static void OnCancelEvent(object? sender, EventArgs e) {
|
|
|
|
|
if (e is ConsoleCancelEventArgs ce) ce.Cancel = true;
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2023-06-12 01:10:46 +00:00
|
|
|
|
if (_shutdownRequested) return;
|
|
|
|
|
_shutdownRequested = true;
|
2022-09-04 01:04:55 +00:00
|
|
|
|
Log(nameof(Program), "Shutting down...");
|
2021-05-30 17:05:33 +00:00
|
|
|
|
|
2023-06-12 01:10:46 +00:00
|
|
|
|
var dispose = Task.Run(_bot.Dispose);
|
|
|
|
|
if (!dispose.Wait(15000)) {
|
2022-09-04 01:04:55 +00:00
|
|
|
|
Log(nameof(Program), "Disconnection is taking too long. Will force exit.");
|
2023-06-12 01:10:46 +00:00
|
|
|
|
Environment.ExitCode = 1;
|
2020-04-02 18:27:55 +00:00
|
|
|
|
}
|
2022-09-04 01:04:55 +00:00
|
|
|
|
Log(nameof(Program), $"Uptime: {BotUptime}");
|
2021-10-18 07:26:25 +00:00
|
|
|
|
Environment.Exit(Environment.ExitCode);
|
2020-04-02 18:27:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|