forked from kwsch/SysBot.AnimalCrossing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBotRunner.cs
62 lines (55 loc) · 2.08 KB
/
BotRunner.cs
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
54
55
56
57
58
59
60
61
62
using System;
using System.Threading;
using System.Threading.Tasks;
using SysBot.Base;
namespace SysBot.AnimalCrossing
{
public static class BotRunner
{
public static async Task RunFrom(CrossBotConfig config, CancellationToken cancel)
{
// Set up logging for Console Window
LogUtil.Forwarders.Add(Logger);
static void Logger(string msg, string identity) => Console.WriteLine(GetMessage(msg, identity));
static string GetMessage(string msg, string identity) => $"> [{DateTime.Now:hh:mm:ss}] - {identity}: {msg}";
var bot = new CrossBot(config);
var sys = new SysCord(bot);
Globals.Self = sys;
Globals.Bot = bot;
LogUtil.LogInfo("Starting Discord.", bot.Connection.IP);
#pragma warning disable 4014
Task.Run(() => sys.MainAsync(config.Token, cancel), cancel);
#pragma warning restore 4014
if (config.SkipConsoleBotCreation)
{
await Task.Delay(-1, cancel).ConfigureAwait(false);
return;
}
LogUtil.LogInfo("Starting bot loop.", bot.Connection.IP);
var task = bot.RunAsync(cancel);
await task;
if (task.IsFaulted)
{
if (task.Exception == null)
{
LogUtil.LogError("Bot has terminated due to an unknown error.", bot.Connection.IP);
}
else
{
LogUtil.LogError("Bot has terminated due to an error:", bot.Connection.IP);
foreach (var ex in task.Exception.InnerExceptions)
{
LogUtil.LogError(ex.Message, bot.Connection.IP);
var st = ex.StackTrace;
if (st != null)
LogUtil.LogError(st, bot.Connection.IP);
}
}
}
else
{
LogUtil.LogInfo("Bot has terminated.", bot.Connection.IP);
}
}
}
}