-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
BotRunner.cs
97 lines (83 loc) · 3.2 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Threading;
using System.Threading.Tasks;
using SysBot.Base;
using SysBot.ACNHOrders.Twitch;
using SysBot.ACNHOrders.Signalr;
namespace SysBot.ACNHOrders
{
public static class BotRunner
{
public static async Task RunFrom(CrossBotConfig config, CancellationToken cancel, TwitchConfig? tConfig = null)
{
// 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;
Globals.Hub = QueueHub.CurrentInstance;
GlobalBan.UpdateConfiguration(config);
bot.Log("Starting Discord.");
#pragma warning disable 4014
Task.Run(() => sys.MainAsync(config.Token, cancel), cancel);
#pragma warning restore 4014
if (tConfig != null && !string.IsNullOrWhiteSpace(tConfig.Token))
{
bot.Log("Starting Twitch.");
var _ = new TwitchCrossBot(tConfig, bot);
}
if (!string.IsNullOrWhiteSpace(config.SignalrConfig.URIEndpoint))
{
bot.Log("Starting Web.");
var _ = new SignalrCrossBot(config.SignalrConfig, bot);
}
if (config.SkipConsoleBotCreation)
{
await Task.Delay(-1, cancel).ConfigureAwait(false);
return;
}
while (!cancel.IsCancellationRequested)
{
bot.Log("Starting bot loop.");
var task = bot.RunAsync(cancel);
await task.ConfigureAwait(false);
bool attemptReconnect = false;
if (task.IsFaulted)
{
if (task.Exception == null)
{
bot.Log("Bot has terminated due to an unknown error.");
}
else
{
bot.Log("Bot has terminated due to an error:");
foreach (var ex in task.Exception.InnerExceptions)
{
bot.Log(ex.Message);
var st = ex.StackTrace;
if (st != null)
bot.Log(st);
}
}
attemptReconnect = false;
}
else
{
bot.Log("Bot has terminated.");
if (config.DodoModeConfig.LimitedDodoRestoreOnlyMode) // don't restore ordermode crashes
attemptReconnect = true;
}
if (attemptReconnect)
{
await Task.Delay(10_000, cancel).ConfigureAwait(false);
bot.Log("Bot is attempting a restart...");
}
else
break;
}
}
}
}