forked from NoiTheCat/BirthdayBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
53 lines (45 loc) · 1.79 KB
/
Program.cs
File metadata and controls
53 lines (45 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
namespace BirthdayBot;
class Program {
private static ShardManager _bot = null!;
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");
static async Task Main() {
Configuration? cfg = null;
try {
cfg = new Configuration();
} catch (Exception ex) {
Console.WriteLine(ex);
Environment.Exit(2);
}
_bot = new ShardManager(cfg);
AppDomain.CurrentDomain.ProcessExit += OnCancelEvent;
Console.CancelKeyPress += OnCancelEvent;
await Task.Delay(-1);
}
/// <summary>
/// Sends a formatted message to console.
/// </summary>
public static void Log(string source, string message) {
var ts = DateTime.Now;
var ls = new string[] { "\r\n", "\n" };
foreach (var item in message.Split(ls, StringSplitOptions.None))
Console.WriteLine($"{ts:s} [{source}] {item}");
}
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(nameof(Program), "Shutting down...");
var dispose = Task.Run(_bot.Dispose);
if (!dispose.Wait(15000)) {
Log(nameof(Program), "Disconnection is taking too long. Will force exit.");
Environment.ExitCode = 1;
}
Log(nameof(Program), $"Uptime: {BotUptime}");
Environment.Exit(Environment.ExitCode);
}
}