-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
139 lines (119 loc) · 4.63 KB
/
Program.cs
File metadata and controls
139 lines (119 loc) · 4.63 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Discord;
using Discord.WebSocket;
using GeneralPurposeLib;
using SerbleBot.Commands;
using SerbleBot.Data;
namespace SerbleBot;
internal static class Program {
public const string Version = "0.0.1";
public static Dictionary<string, string>? Config;
public static Random Random { get; } = new ();
private static readonly Dictionary<string, string> DefaultConfig = new() {
{ "token", "discord bot token" },
{ "testing_server_id", "911109182842602044" },
{ "youtube-api-key", "nice little api key" },
{ "urlscan-api-key", "cute little api key" }
};
public static int Main(string[] args) {
// Init Logger
try {
Logger.Init(LogLevel.Debug);
}
catch (Exception e) {
Console.WriteLine("Failed to initialize logger: " + e);
return 1;
}
Logger.Info("SerbleBot starting...");
// Config
try {
ConfigManager configManager = new ("config.json", DefaultConfig);
Config = configManager.LoadConfig();
}
catch (Exception e) {
Logger.Error("Failed to load config: " + e);
Logger.WaitFlush();
return 1;
}
if (args.Length != 0) {
switch (args[0].ToLower()) {
default:
Console.WriteLine("Unknown command");
return 1;
case "updatecommands":
DiscordSocketClient client = new ();
bool finished = false;
client.Ready += () => {
CommandManager.UpdateCommands(client);
Logger.Info("Commands updated");
finished = true;
return Task.CompletedTask;
};
client.LoginAsync(TokenType.Bot, Config["token"]).Wait();
client.StartAsync().Wait();
while (!finished) {
Thread.Sleep(100);
}
Logger.Debug("Command execution finished");
Logger.WaitFlush();
return 0;
case "updatecommand":
if (args.Length != 2) {
Logger.Info("Usage: updatecommand <command>");
Logger.WaitFlush();
return 1;
}
DiscordSocketClient updateClient = new ();
bool updateFinished = false;
updateClient.Ready += () => {
CommandManager.UpdateCommand(updateClient, args[1]);
Logger.Info("Command updated");
updateFinished = true;
return Task.CompletedTask;
};
updateClient.LoginAsync(TokenType.Bot, Config["token"]).Wait();
updateClient.StartAsync().Wait();
while (!updateFinished) {
Thread.Sleep(100);
}
Logger.Debug("Command execution finished");
Logger.WaitFlush();
return 0;
}
}
// Init Services
ServiceManager.Init();
// Run bot
List<DateTime> errors = new();
Exception? lastError = null;
while (true) {
try {
Logger.Info("Starting bot...");
Bot bot = new ();
bot.Run().Wait();
Logger.Warn("Bot task exited unexpectedly");
break;
}
catch (Exception e) {
Logger.Error(e);
// Stop if there are more than 5 errors in 1 minute
// Remove all errors older than 5 minutes
errors.Add(DateTime.Now);
errors.RemoveAll(x => x < DateTime.Now - TimeSpan.FromMinutes(5));
if (errors.Count > 5) {
Logger.Error("Too many errors (Possible error loop), stopping...");
break;
}
// Stop if the same error happened twice in a row
if (lastError == e) {
Logger.Error("Same error twice in a row, stopping...");
break;
}
lastError = e;
Logger.Info("Restating bot in 5 seconds...");
Thread.Sleep(5000);
}
}
Logger.WaitFlush();
return 0;
}
}