2018-07-19 12:42:48 +00:00
|
|
|
|
using System;
|
2018-08-05 14:36:16 +00:00
|
|
|
|
using System.Threading;
|
2018-07-19 12:42:48 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using CompatBot.Commands;
|
2018-07-19 20:55:54 +00:00
|
|
|
|
using CompatBot.Commands.Converters;
|
2018-07-19 12:42:48 +00:00
|
|
|
|
using CompatBot.Database;
|
2018-08-29 16:52:47 +00:00
|
|
|
|
using CompatBot.Database.Providers;
|
2018-07-19 12:42:48 +00:00
|
|
|
|
using CompatBot.EventHandlers;
|
2018-08-03 16:39:57 +00:00
|
|
|
|
using CompatBot.ThumbScrapper;
|
2018-07-19 12:42:48 +00:00
|
|
|
|
using DSharpPlus;
|
|
|
|
|
using DSharpPlus.CommandsNext;
|
2018-08-03 16:39:57 +00:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-07-19 12:42:48 +00:00
|
|
|
|
|
|
|
|
|
namespace CompatBot
|
|
|
|
|
{
|
|
|
|
|
internal static class Program
|
|
|
|
|
{
|
2018-08-05 14:36:16 +00:00
|
|
|
|
private static readonly SemaphoreSlim InstanceCheck = new SemaphoreSlim(0, 1);
|
|
|
|
|
private static readonly SemaphoreSlim ShutdownCheck = new SemaphoreSlim(0, 1);
|
|
|
|
|
|
2018-07-19 12:42:48 +00:00
|
|
|
|
internal static async Task Main(string[] args)
|
|
|
|
|
{
|
2018-09-08 14:59:59 +00:00
|
|
|
|
var singleInstanceCheckThread = new Thread(() =>
|
2018-08-05 14:36:16 +00:00
|
|
|
|
{
|
|
|
|
|
using (var instanceLock = new Mutex(false, @"Global\RPCS3 Compatibility Bot"))
|
|
|
|
|
{
|
|
|
|
|
if (instanceLock.WaitOne(1000))
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
InstanceCheck.Release();
|
|
|
|
|
ShutdownCheck.Wait();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
instanceLock.ReleaseMutex();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-09-08 14:59:59 +00:00
|
|
|
|
var rpcs3UpdateCheckThread = new Thread(client =>
|
|
|
|
|
{
|
2018-09-10 08:09:44 +00:00
|
|
|
|
try
|
2018-09-08 14:59:59 +00:00
|
|
|
|
{
|
2018-09-10 08:09:44 +00:00
|
|
|
|
while (!Config.Cts.IsCancellationRequested)
|
2018-09-08 15:41:56 +00:00
|
|
|
|
{
|
2018-09-10 08:09:44 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CompatList.CheckForRpcs3Updates((DiscordClient)client, null).ConfigureAwait(false).GetAwaiter().GetResult();
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
Task.Delay(TimeSpan.FromMinutes(1), Config.Cts.Token).ConfigureAwait(false).GetAwaiter().GetResult();
|
2018-09-08 15:41:56 +00:00
|
|
|
|
}
|
2018-09-08 14:59:59 +00:00
|
|
|
|
}
|
2018-09-10 08:09:44 +00:00
|
|
|
|
catch (TaskCanceledException) { }
|
2018-10-05 17:56:44 +00:00
|
|
|
|
}){ IsBackground = true };
|
2018-09-08 14:59:59 +00:00
|
|
|
|
|
2018-08-05 14:36:16 +00:00
|
|
|
|
try
|
2018-07-19 12:42:48 +00:00
|
|
|
|
{
|
2018-09-08 14:59:59 +00:00
|
|
|
|
singleInstanceCheckThread.Start();
|
2018-08-05 14:36:16 +00:00
|
|
|
|
if (!InstanceCheck.Wait(1000))
|
|
|
|
|
{
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Fatal("Another instance is already running.");
|
2018-08-03 16:39:57 +00:00
|
|
|
|
return;
|
2018-08-05 14:36:16 +00:00
|
|
|
|
}
|
2018-08-03 16:39:57 +00:00
|
|
|
|
|
2018-08-05 14:36:16 +00:00
|
|
|
|
if (string.IsNullOrEmpty(Config.Token))
|
|
|
|
|
{
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Fatal("No token was specified.");
|
2018-08-03 16:39:57 +00:00
|
|
|
|
return;
|
2018-08-05 14:36:16 +00:00
|
|
|
|
}
|
2018-08-29 16:52:47 +00:00
|
|
|
|
var amdDriverRefreshTask = AmdDriverVersionProvider.RefreshAsync();
|
2018-07-19 12:42:48 +00:00
|
|
|
|
|
2018-08-05 14:36:16 +00:00
|
|
|
|
using (var db = new BotDb())
|
|
|
|
|
if (!await DbImporter.UpgradeAsync(db, Config.Cts.Token))
|
|
|
|
|
return;
|
2018-07-19 12:42:48 +00:00
|
|
|
|
|
2018-08-05 14:36:16 +00:00
|
|
|
|
using (var db = new ThumbnailDb())
|
|
|
|
|
if (!await DbImporter.UpgradeAsync(db, Config.Cts.Token))
|
|
|
|
|
return;
|
2018-07-19 12:42:48 +00:00
|
|
|
|
|
2018-08-18 21:21:50 +00:00
|
|
|
|
var psnScrappingTask = new PsnScraper().RunAsync(Config.Cts.Token);
|
|
|
|
|
var gameTdbScrapingTask = GameTdbScraper.RunAsync(Config.Cts.Token);
|
2018-08-29 16:52:47 +00:00
|
|
|
|
await amdDriverRefreshTask.ConfigureAwait(false);
|
2018-08-05 14:36:16 +00:00
|
|
|
|
|
|
|
|
|
var config = new DiscordConfiguration
|
2018-07-19 12:42:48 +00:00
|
|
|
|
{
|
2018-08-05 14:36:16 +00:00
|
|
|
|
Token = Config.Token,
|
|
|
|
|
TokenType = TokenType.Bot,
|
|
|
|
|
UseInternalLogHandler = true,
|
|
|
|
|
//LogLevel = LogLevel.Debug,
|
|
|
|
|
};
|
2018-07-19 12:42:48 +00:00
|
|
|
|
|
2018-08-05 14:36:16 +00:00
|
|
|
|
using (var client = new DiscordClient(config))
|
2018-07-19 12:42:48 +00:00
|
|
|
|
{
|
2018-08-05 14:36:16 +00:00
|
|
|
|
var commands = client.UseCommandsNext(new CommandsNextConfiguration
|
|
|
|
|
{
|
|
|
|
|
StringPrefixes = new[] {Config.CommandPrefix},
|
|
|
|
|
Services = new ServiceCollection().BuildServiceProvider(),
|
|
|
|
|
});
|
|
|
|
|
commands.RegisterConverter(new CustomDiscordChannelConverter());
|
|
|
|
|
commands.RegisterCommands<Misc>();
|
|
|
|
|
commands.RegisterCommands<CompatList>();
|
|
|
|
|
commands.RegisterCommands<Sudo>();
|
|
|
|
|
commands.RegisterCommands<Antipiracy>();
|
|
|
|
|
commands.RegisterCommands<Warnings>();
|
|
|
|
|
commands.RegisterCommands<Explain>();
|
|
|
|
|
commands.RegisterCommands<Psn>();
|
2018-08-18 15:37:58 +00:00
|
|
|
|
commands.RegisterCommands<Invites>();
|
2018-09-10 13:58:44 +00:00
|
|
|
|
commands.RegisterCommands<Moderation>();
|
2018-07-19 12:42:48 +00:00
|
|
|
|
|
2018-08-05 14:36:16 +00:00
|
|
|
|
client.Ready += async r =>
|
|
|
|
|
{
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Info("Bot is ready to serve!");
|
|
|
|
|
Config.Log.Info("");
|
|
|
|
|
Config.Log.Info($"Bot user id : {r.Client.CurrentUser.Id} ({r.Client.CurrentUser.Username})");
|
|
|
|
|
Config.Log.Info($"Bot admin id : {Config.BotAdminId} ({(await r.Client.GetUserAsync(Config.BotAdminId)).Username})");
|
|
|
|
|
Config.Log.Info("");
|
2018-08-05 14:36:16 +00:00
|
|
|
|
};
|
2018-08-18 15:37:58 +00:00
|
|
|
|
client.GuildAvailable += async gaArgs =>
|
|
|
|
|
{
|
2018-09-08 15:41:56 +00:00
|
|
|
|
if (gaArgs.Guild.Id != Config.BotGuildId)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Warn($"Unknown discord server {gaArgs.Guild.Id} ({gaArgs.Guild.Name})");
|
2018-09-08 15:41:56 +00:00
|
|
|
|
#else
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Warn($"Unknown discord server {gaArgs.Guild.Id} ({gaArgs.Guild.Name}), leaving...");
|
2018-09-08 15:41:56 +00:00
|
|
|
|
await gaArgs.Guild.LeaveAsync().ConfigureAwait(false);
|
|
|
|
|
#endif
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Info($"Server {gaArgs.Guild.Name} is available now");
|
|
|
|
|
Config.Log.Info($"Checking moderation backlogs in {gaArgs.Guild.Name}...");
|
2018-09-08 15:30:11 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Task.WhenAll(
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Starbucks.CheckBacklogAsync(gaArgs.Client, gaArgs.Guild).ContinueWith(_ => Config.Log.Info($"Starbucks backlog checked in {gaArgs.Guild.Name}.")),
|
|
|
|
|
DiscordInviteFilter.CheckBacklogAsync(gaArgs.Client, gaArgs.Guild).ContinueWith(_ => Config.Log.Info($"Discord invites backlog checked in {gaArgs.Guild.Name}."))
|
2018-09-08 15:30:11 +00:00
|
|
|
|
).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Warn(e, "Error running backlog tasks");
|
2018-09-08 15:30:11 +00:00
|
|
|
|
}
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Info($"All moderation backlogs checked in {gaArgs.Guild.Name}.");
|
2018-08-18 15:37:58 +00:00
|
|
|
|
};
|
|
|
|
|
client.GuildUnavailable += guArgs =>
|
|
|
|
|
{
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Warn($"{guArgs.Guild.Name} is unavailable");
|
2018-08-18 15:37:58 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-05 14:36:16 +00:00
|
|
|
|
client.MessageReactionAdded += Starbucks.Handler;
|
2018-07-19 12:42:48 +00:00
|
|
|
|
|
2018-08-05 14:36:16 +00:00
|
|
|
|
client.MessageCreated += AntipiracyMonitor.OnMessageCreated; // should be first
|
2018-08-10 15:24:20 +00:00
|
|
|
|
client.MessageCreated += ProductCodeLookup.OnMessageCreated;
|
2018-08-05 14:36:16 +00:00
|
|
|
|
client.MessageCreated += LogInfoHandler.OnMessageCreated;
|
|
|
|
|
client.MessageCreated += LogsAsTextMonitor.OnMessageCreated;
|
2018-08-18 15:37:58 +00:00
|
|
|
|
client.MessageCreated += DiscordInviteFilter.OnMessageCreated;
|
2018-08-10 15:24:20 +00:00
|
|
|
|
client.MessageCreated += BotShutupHandler.OnMessageCreated;
|
2018-09-10 13:31:27 +00:00
|
|
|
|
client.MessageCreated += NewBuildsMonitor.OnMessageCreated;
|
2018-08-05 14:36:16 +00:00
|
|
|
|
|
2018-08-18 15:37:58 +00:00
|
|
|
|
client.MessageUpdated += AntipiracyMonitor.OnMessageUpdated;
|
|
|
|
|
client.MessageUpdated += DiscordInviteFilter.OnMessageUpdated;
|
2018-08-05 14:36:16 +00:00
|
|
|
|
|
|
|
|
|
client.MessageDeleted += ThumbnailCacheMonitor.OnMessageDeleted;
|
|
|
|
|
|
2018-09-12 16:25:30 +00:00
|
|
|
|
client.UserUpdated += UsernameSpoofMonitor.OnUserUpdated;
|
|
|
|
|
client.GuildMemberAdded += UsernameSpoofMonitor.OnMemberAdded;
|
|
|
|
|
client.GuildMemberUpdated += UsernameSpoofMonitor.OnMemberUpdated;
|
|
|
|
|
|
2018-10-05 17:56:44 +00:00
|
|
|
|
var botLog = NLog.LogManager.GetLogger("botLogger");
|
|
|
|
|
client.DebugLogger.LogMessageReceived += (sender, eventArgs) =>
|
|
|
|
|
{
|
|
|
|
|
Action<string> logLevel = botLog.Info;
|
|
|
|
|
if (eventArgs.Level == LogLevel.Debug)
|
|
|
|
|
logLevel = botLog.Debug;
|
|
|
|
|
//else if (eventArgs.Level == LogLevel.Info)
|
|
|
|
|
// logLevel = botLog.Info;
|
|
|
|
|
else if (eventArgs.Level == LogLevel.Warning)
|
|
|
|
|
logLevel = botLog.Warn;
|
|
|
|
|
else if (eventArgs.Level == LogLevel.Error)
|
|
|
|
|
logLevel = botLog.Error;
|
|
|
|
|
else if (eventArgs.Level == LogLevel.Critical)
|
|
|
|
|
logLevel = botLog.Fatal;
|
|
|
|
|
logLevel(eventArgs.Message);
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-05 14:36:16 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await client.ConnectAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Fatal(e, "Terminating");
|
2018-08-05 14:36:16 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (args.Length > 1 && ulong.TryParse(args[1], out var channelId))
|
|
|
|
|
{
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Info($"Found channelId: {args[1]}");
|
2018-08-05 14:36:16 +00:00
|
|
|
|
var channel = await client.GetChannelAsync(channelId).ConfigureAwait(false);
|
|
|
|
|
await channel.SendMessageAsync("Bot is up and running").ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 17:56:44 +00:00
|
|
|
|
Config.Log.Debug("Running RPC3 update check thread");
|
2018-09-08 14:59:59 +00:00
|
|
|
|
rpcs3UpdateCheckThread.Start(client);
|
2018-08-05 14:36:16 +00:00
|
|
|
|
|
|
|
|
|
while (!Config.Cts.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
if (client.Ping > 1000)
|
|
|
|
|
await client.ReconnectAsync();
|
|
|
|
|
await Task.Delay(TimeSpan.FromMinutes(1), Config.Cts.Token).ContinueWith(dt => {/* in case it was cancelled */}).ConfigureAwait(false);
|
|
|
|
|
}
|
2018-07-19 12:42:48 +00:00
|
|
|
|
}
|
2018-08-18 21:21:50 +00:00
|
|
|
|
await Task.WhenAll(
|
|
|
|
|
psnScrappingTask,
|
|
|
|
|
gameTdbScrapingTask
|
|
|
|
|
).ConfigureAwait(false);
|
2018-08-05 14:36:16 +00:00
|
|
|
|
}
|
|
|
|
|
catch (TaskCanceledException)
|
|
|
|
|
{
|
|
|
|
|
}
|
2018-10-05 17:56:44 +00:00
|
|
|
|
catch(Exception e)
|
|
|
|
|
{
|
|
|
|
|
Config.Log.Fatal(e);
|
|
|
|
|
}
|
2018-08-05 14:36:16 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
ShutdownCheck.Release();
|
2018-10-05 17:56:44 +00:00
|
|
|
|
if (singleInstanceCheckThread.IsAlive)
|
|
|
|
|
singleInstanceCheckThread.Join(100);
|
|
|
|
|
if (rpcs3UpdateCheckThread.IsAlive)
|
|
|
|
|
rpcs3UpdateCheckThread.Join(100);
|
|
|
|
|
Config.Log.Info("Exiting");
|
2018-07-19 12:42:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|