-
Notifications
You must be signed in to change notification settings - Fork 45
/
Program.cs
417 lines (384 loc) · 18.4 KB
/
Program.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using CompatBot.Commands;
using CompatBot.Commands.Converters;
using CompatBot.Database;
using CompatBot.Database.Providers;
using CompatBot.EventHandlers;
using CompatBot.Utils;
using CompatBot.Utils.Extensions;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Interactivity.Extensions;
using DSharpPlus.SlashCommands;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.DependencyInjection;
using Fortune = CompatBot.Commands.Fortune;
namespace CompatBot;
internal static class Program
{
private static readonly SemaphoreSlim InstanceCheck = new(0, 1);
private static readonly SemaphoreSlim ShutdownCheck = new(0, 1);
// pre-load the assembly so it won't fail after framework update while the process is still running
private static readonly Assembly DiagnosticsAssembly = Assembly.Load(typeof(Process).Assembly.GetName());
internal const ulong InvalidChannelId = 13;
internal static async Task Main(string[] args)
{
Config.TelemetryClient?.TrackEvent("startup");
//AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromMilliseconds(100));
Regex.CacheSize = 200; // default is 15, we need more for content filter
Console.WriteLine("Confinement: " + SandboxDetector.Detect());
if (args.Length > 0 && args[0] == "--dry-run")
{
await OpenSslConfigurator.CheckAndFixSystemConfigAsync().ConfigureAwait(false);
Console.WriteLine("Database path: " + Path.GetDirectoryName(Path.GetFullPath(DbImporter.GetDbPath("fake.db", Environment.SpecialFolder.ApplicationData))));
if (Assembly.GetEntryAssembly()?.GetCustomAttribute<UserSecretsIdAttribute>() != null)
Console.WriteLine("Bot config path: " + Path.GetDirectoryName(Path.GetFullPath(Config.GoogleApiConfigPath)));
return;
}
if (Environment.ProcessId == 0)
Config.Log.Info("Well, this was unexpected");
var singleInstanceCheckThread = new Thread(() =>
{
using var instanceLock = new Mutex(false, @"Global\RPCS3 Compatibility Bot");
if (instanceLock.WaitOne(1000))
try
{
InstanceCheck.Release();
ShutdownCheck.Wait();
}
finally
{
instanceLock.ReleaseMutex();
}
});
try
{
singleInstanceCheckThread.Start();
if (!await InstanceCheck.WaitAsync(1000).ConfigureAwait(false))
{
Config.Log.Fatal("Another instance is already running.");
return;
}
if (string.IsNullOrEmpty(Config.Token) || Config.Token.Length < 16)
{
Config.Log.Fatal("No token was specified.");
return;
}
if (SandboxDetector.Detect() == SandboxType.Docker)
{
Config.Log.Info("Checking OpenSSL system configuration...");
await OpenSslConfigurator.CheckAndFixSystemConfigAsync().ConfigureAwait(false);
Config.Log.Info("Checking for updates...");
try
{
var (updated, stdout) = await Sudo.Bot.GitPullAsync(Config.Cts.Token).ConfigureAwait(false);
if (!string.IsNullOrEmpty(stdout) && updated)
Config.Log.Debug(stdout);
if (updated)
{
Sudo.Bot.Restart(InvalidChannelId, "Restarted due to new bot updates not present in this Docker image");
return;
}
}
catch (Exception e)
{
Config.Log.Error(e, "Failed to check for updates");
}
}
if (!await DbImporter.UpgradeAsync(Config.Cts.Token).ConfigureAwait(false))
return;
await SqlConfiguration.RestoreAsync().ConfigureAwait(false);
Config.Log.Debug("Restored configuration variables from persistent storage");
await StatsStorage.RestoreAsync().ConfigureAwait(false);
Config.Log.Debug("Restored stats from persistent storage");
var backgroundTasks = Task.WhenAll(
AmdDriverVersionProvider.RefreshAsync(),
#if !DEBUG
ThumbScrapper.GameTdbScraper.RunAsync(Config.Cts.Token),
//TitleUpdateInfoProvider.RefreshGameUpdateInfoAsync(Config.Cts.Token),
#endif
StatsStorage.BackgroundSaveAsync(),
CompatList.ImportCompatListAsync(),
Config.GetAzureDevOpsClient().GetPipelineDurationAsync(Config.Cts.Token),
Config.GetCurrentGitRevisionAsync(Config.Cts.Token),
Sudo.Bot.UpdateCheckScheduledAsync(Config.Cts.Token)
);
try
{
if (!Directory.Exists(Config.IrdCachePath))
Directory.CreateDirectory(Config.IrdCachePath);
}
catch (Exception e)
{
Config.Log.Warn(e, $"Failed to create new folder {Config.IrdCachePath}: {e.Message}");
}
var config = new DiscordConfiguration
{
Token = Config.Token,
TokenType = TokenType.Bot,
MessageCacheSize = Config.MessageCacheSize,
LoggerFactory = Config.LoggerFactory,
Intents = DiscordIntents.All,
};
using var client = new DiscordClient(config);
var commands = client.UseCommandsNext(new()
{
StringPrefixes = new[] {Config.CommandPrefix, Config.AutoRemoveCommandPrefix},
Services = new ServiceCollection().BuildServiceProvider(),
});
commands.RegisterConverter(new TextOnlyDiscordChannelConverter());
#if DEBUG
commands.RegisterCommands<DevOnly>();
#endif
commands.RegisterCommands<Misc>();
commands.RegisterCommands<CompatList>();
commands.RegisterCommands<Sudo>();
commands.RegisterCommands<CommandsManagement>();
commands.RegisterCommands<ContentFilters>();
commands.RegisterCommands<Warnings>();
commands.RegisterCommands<Explain>();
commands.RegisterCommands<Psn>();
commands.RegisterCommands<Invites>();
commands.RegisterCommands<Moderation>();
commands.RegisterCommands<Ird>();
commands.RegisterCommands<BotMath>();
commands.RegisterCommands<Pr>();
commands.RegisterCommands<Events>();
commands.RegisterCommands<E3>();
commands.RegisterCommands<BotStats>();
commands.RegisterCommands<Hardware>();
commands.RegisterCommands<Syscall>();
commands.RegisterCommands<ForcedNicknames>();
commands.RegisterCommands<Minesweeper>();
commands.RegisterCommands<Fortune>();
if (!string.IsNullOrEmpty(Config.AzureComputerVisionKey))
commands.RegisterCommands<Vision>();
var slashCommands = client.UseSlashCommands();
// Only register to rpcs3 guild for now.
slashCommands.RegisterCommands<SlashMisc>(Config.BotGuildId);
commands.CommandErrored += UnknownCommandHandler.OnError;
client.UseInteractivity(new());
client.Ready += async (c, _) =>
{
Config.Log.Info("Bot is ready to serve!");
Config.Log.Info("");
Config.Log.Info($"Bot user id : {c.CurrentUser.Id} ({c.CurrentUser.Username})");
var owners = c.CurrentApplication.Owners.ToList();
var msg = new StringBuilder($"Bot admin id{(owners.Count == 1 ? "": "s")}:");
if (owners.Count > 1)
msg.AppendLine();
await using var db = new BotDb();
foreach (var owner in owners)
{
msg.AppendLine($"\t{owner.Id} ({owner.Username ?? "???"}#{owner.Discriminator ?? "????"})");
if (!await db.Moderator.AnyAsync(m => m.DiscordId == owner.Id, Config.Cts.Token).ConfigureAwait(false))
await db.Moderator.AddAsync(new() {DiscordId = owner.Id, Sudoer = true}, Config.Cts.Token).ConfigureAwait(false);
}
await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
Config.Log.Info(msg.ToString().TrimEnd);
Config.Log.Info("");
};
client.GuildAvailable += async (c, gaArgs) =>
{
await BotStatusMonitor.RefreshAsync(c).ConfigureAwait(false);
Watchdog.DisconnectTimestamps.Clear();
Watchdog.TimeSinceLastIncomingMessage.Restart();
if (gaArgs.Guild.Id != Config.BotGuildId)
{
#if DEBUG
Config.Log.Warn($"Unknown discord server {gaArgs.Guild.Id} ({gaArgs.Guild.Name})");
#else
Config.Log.Warn($"Unknown discord server {gaArgs.Guild.Id} ({gaArgs.Guild.Name}), leaving...");
await gaArgs.Guild.LeaveAsync().ConfigureAwait(false);
#endif
return;
}
Config.Log.Info($"Server {gaArgs.Guild.Name} is available now");
Config.Log.Info($"Checking moderation backlogs in {gaArgs.Guild.Name}...");
try
{
await Task.WhenAll(
Starbucks.CheckBacklogAsync(c, gaArgs.Guild).ContinueWith(_ => Config.Log.Info($"Starbucks backlog checked in {gaArgs.Guild.Name}."), TaskScheduler.Default),
DiscordInviteFilter.CheckBacklogAsync(c, gaArgs.Guild).ContinueWith(_ => Config.Log.Info($"Discord invites backlog checked in {gaArgs.Guild.Name}."), TaskScheduler.Default)
).ConfigureAwait(false);
}
catch (Exception e)
{
Config.Log.Warn(e, "Error running backlog tasks");
}
Config.Log.Info($"All moderation backlogs checked in {gaArgs.Guild.Name}.");
};
client.GuildAvailable += (c, _) => UsernameValidationMonitor.MonitorAsync(c, true);
client.GuildUnavailable += (_, guArgs) =>
{
Config.Log.Warn($"{guArgs.Guild.Name} is unavailable");
return Task.CompletedTask;
};
#if !DEBUG
/*
client.GuildDownloadCompleted += async gdcArgs =>
{
foreach (var guild in gdcArgs.Guilds)
await ModProvider.SyncRolesAsync(guild.Value).ConfigureAwait(false);
};
*/
#endif
client.MessageReactionAdded += Starbucks.Handler;
client.MessageReactionAdded += ContentFilterMonitor.OnReaction;
var mediaScreenshotMonitor = new MediaScreenshotMonitor(client);
client.MessageCreated += Watchdog.OnMessageCreated;
client.MessageCreated += new OrderedEventHandlerWrapper<MessageCreateEventArgs>(
new[]
{
ContentFilterMonitor.OnMessageCreated, // should be first
DiscordInviteFilter.OnMessageCreated,
},
new[]
{
GlobalMessageCache.OnMessageCreated,
mediaScreenshotMonitor.OnMessageCreated,
ProductCodeLookup.OnMessageCreated,
LogParsingHandler.OnMessageCreated,
LogAsTextMonitor.OnMessageCreated,
PostLogHelpHandler.OnMessageCreated,
BotReactionsHandler.OnMessageCreated,
GithubLinksHandler.OnMessageCreated,
NewBuildsMonitor.OnMessageCreated,
TableFlipMonitor.OnMessageCreated,
IsTheGamePlayableHandler.OnMessageCreated,
EmpathySimulationHandler.OnMessageCreated,
}).OnEvent;
client.MessageUpdated += new OrderedEventHandlerWrapper<MessageUpdateEventArgs>(new[]
{
ContentFilterMonitor.OnMessageUpdated,
DiscordInviteFilter.OnMessageUpdated,
},
new[]
{
GlobalMessageCache.OnMessageUpdated,
EmpathySimulationHandler.OnMessageUpdated,
}).OnEvent;
client.MessageDeleted += GlobalMessageCache.OnMessageDeleted;
if (Config.DeletedMessagesLogChannelId > 0)
client.MessageDeleted += DeletedMessagesMonitor.OnMessageDeleted;
client.MessageDeleted += ThumbnailCacheMonitor.OnMessageDeleted;
client.MessageDeleted += EmpathySimulationHandler.OnMessageDeleted;
client.MessagesBulkDeleted += GlobalMessageCache.OnMessagesBulkDeleted;
client.UserUpdated += UsernameSpoofMonitor.OnUserUpdated;
client.UserUpdated += UsernameZalgoMonitor.OnUserUpdated;
client.GuildMemberAdded += Greeter.OnMemberAdded;
client.GuildMemberAdded += UsernameSpoofMonitor.OnMemberAdded;
client.GuildMemberAdded += UsernameZalgoMonitor.OnMemberAdded;
client.GuildMemberAdded += UsernameValidationMonitor.OnMemberAdded;
client.GuildMemberAdded += UsernameRaidMonitor.OnMemberAdded;
client.GuildMemberUpdated += UsernameSpoofMonitor.OnMemberUpdated;
client.GuildMemberUpdated += UsernameZalgoMonitor.OnMemberUpdated;
client.GuildMemberUpdated += UsernameValidationMonitor.OnMemberUpdated;
client.GuildMemberUpdated += UsernameRaidMonitor.OnMemberUpdated;
#if DEBUG
client.ComponentInteractionCreated += (_, args) =>
{
Config.Log.Debug($"ComponentInteraction: type: {args.Interaction.Type}, id: {args.Interaction.Data.CustomId}, user: {args.Interaction.User}");
return Task.CompletedTask;
};
#endif
client.ComponentInteractionCreated += GlobalButtonHandler.OnComponentInteraction;
Watchdog.DisconnectTimestamps.Enqueue(DateTime.UtcNow);
try
{
await client.ConnectAsync().ConfigureAwait(false);
}
catch (Exception e)
{
Config.Log.Error(e, "Failed to connect to Discord: " + e.Message);
throw;
}
ulong? channelId = null;
string? restartMsg = null;
await using (var db = new BotDb())
{
var chState = db.BotState.FirstOrDefault(k => k.Key == "bot-restart-channel");
if (chState != null)
{
if (ulong.TryParse(chState.Value, out var ch))
channelId = ch;
db.BotState.Remove(chState);
}
var msgState = db.BotState.FirstOrDefault(i => i.Key == "bot-restart-msg");
if (msgState != null)
{
restartMsg = msgState.Value;
db.BotState.Remove(msgState);
}
await db.SaveChangesAsync().ConfigureAwait(false);
}
if (string.IsNullOrEmpty(restartMsg))
restartMsg = null;
if (channelId.HasValue)
{
Config.Log.Info($"Found channelId {channelId}");
DiscordChannel channel;
if (channelId == InvalidChannelId)
{
channel = await client.GetChannelAsync(Config.ThumbnailSpamId).ConfigureAwait(false);
await channel.SendMessageAsync(restartMsg ?? "Bot has suffered some catastrophic failure and was restarted").ConfigureAwait(false);
}
else
{
channel = await client.GetChannelAsync(channelId.Value).ConfigureAwait(false);
await channel.SendMessageAsync("Bot is up and running").ConfigureAwait(false);
}
}
else
{
Config.Log.Debug($"Args count: {args.Length}");
var pArgs = args.Select(a => a == Config.Token ? "<Token>" : $"[{a}]");
Config.Log.Debug("Args: " + string.Join(" ", pArgs));
}
Config.Log.Debug("Running RPCS3 update check thread");
backgroundTasks = Task.WhenAll(
backgroundTasks,
NewBuildsMonitor.MonitorAsync(client),
Watchdog.Watch(client),
InviteWhitelistProvider.CleanupAsync(client),
UsernameValidationMonitor.MonitorAsync(client),
Psn.Check.MonitorFwUpdates(client, Config.Cts.Token),
Watchdog.SendMetrics(client),
Watchdog.CheckGCStats(),
mediaScreenshotMonitor.ProcessWorkQueue()
);
while (!Config.Cts.IsCancellationRequested)
{
if (client.Ping > 1000)
Config.Log.Warn($"High ping detected: {client.Ping}");
await Task.Delay(TimeSpan.FromMinutes(1), Config.Cts.Token).ContinueWith(_ => {/* in case it was cancelled */}, TaskScheduler.Default).ConfigureAwait(false);
}
await backgroundTasks.ConfigureAwait(false);
}
catch (Exception e)
{
if (!Config.InMemorySettings.ContainsKey("shutdown"))
Config.Log.Fatal(e, "Experienced catastrophic failure, attempting to restart...");
}
finally
{
Config.TelemetryClient?.Flush();
ShutdownCheck.Release();
if (singleInstanceCheckThread.IsAlive)
singleInstanceCheckThread.Join(100);
}
if (!Config.InMemorySettings.ContainsKey("shutdown"))
Sudo.Bot.Restart(InvalidChannelId, null);
}
}