2017-06-23 19:31:47 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using System;
|
2017-07-26 22:36:59 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Noikoio.RegexBot
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-11-12 03:12:24 +00:00
|
|
|
|
/// Main class. On start, initializes bot modules and passes the DiscordSocketClient to them
|
2017-06-23 19:31:47 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
class RegexBot
|
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
private static Configuration _config;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-11-12 03:12:24 +00:00
|
|
|
|
private BotModule[] _modules;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
internal static Configuration Config => _config;
|
2017-11-12 03:12:24 +00:00
|
|
|
|
internal IEnumerable<BotModule> Modules => _modules;
|
2017-07-26 22:36:59 +00:00
|
|
|
|
|
|
|
|
|
internal RegexBot()
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
// Load configuration
|
|
|
|
|
_config = new Configuration(this);
|
|
|
|
|
if (!_config.LoadInitialConfig())
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
Console.WriteLine("Press any key to exit.");
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
#endif
|
|
|
|
|
Environment.Exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-23 19:31:47 +00:00
|
|
|
|
_client = new DiscordSocketClient(new DiscordSocketConfig()
|
|
|
|
|
{
|
|
|
|
|
LogLevel = LogSeverity.Info,
|
2017-10-30 02:36:35 +00:00
|
|
|
|
AlwaysDownloadUsers = true,
|
2017-06-23 19:31:47 +00:00
|
|
|
|
DefaultRetryMode = RetryMode.AlwaysRetry,
|
2017-11-06 04:55:57 +00:00
|
|
|
|
MessageCacheSize = 0
|
2017-06-23 19:31:47 +00:00
|
|
|
|
});
|
|
|
|
|
|
2017-07-22 03:51:00 +00:00
|
|
|
|
// Hook up handlers for basic functions
|
2017-06-23 19:31:47 +00:00
|
|
|
|
_client.Connected += _client_Connected;
|
2017-07-22 03:51:00 +00:00
|
|
|
|
|
2017-11-12 03:12:24 +00:00
|
|
|
|
// Initialize modules
|
|
|
|
|
_modules = new BotModule[]
|
2017-07-26 22:36:59 +00:00
|
|
|
|
{
|
2017-11-17 20:13:47 +00:00
|
|
|
|
new Module.DMLogger.DMLogger(_client),
|
2017-11-12 03:12:24 +00:00
|
|
|
|
new Module.AutoMod.AutoMod(_client),
|
|
|
|
|
new Module.ModTools.ModTools(_client),
|
|
|
|
|
new Module.AutoRespond.AutoRespond(_client),
|
|
|
|
|
new Module.EntityCache.EntityCache(_client) // EntityCache goes before anything else that uses its data
|
2017-07-26 22:36:59 +00:00
|
|
|
|
};
|
|
|
|
|
var dlog = Logger.GetLogger("Discord.Net");
|
|
|
|
|
_client.Log += async (arg) =>
|
|
|
|
|
await dlog(
|
|
|
|
|
String.Format("{0}: {1}{2}", arg.Source, ((int)arg.Severity < 3 ? arg.Severity + ": " : ""),
|
|
|
|
|
arg.Message));
|
|
|
|
|
|
2017-11-12 03:12:24 +00:00
|
|
|
|
// With modules initialized, finish loading configuration
|
2017-08-26 17:24:37 +00:00
|
|
|
|
var conf = _config.ReloadServerConfig().Result;
|
|
|
|
|
if (conf == false)
|
2017-07-26 22:36:59 +00:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Failed to load server configuration.");
|
|
|
|
|
#if DEBUG
|
|
|
|
|
Console.WriteLine("Press any key to exit.");
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
#endif
|
|
|
|
|
Environment.Exit(1);
|
|
|
|
|
}
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal async Task Start()
|
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
|
|
|
|
|
await _client.LoginAsync(TokenType.Bot, Config.BotUserToken);
|
2017-06-23 19:31:47 +00:00
|
|
|
|
await _client.StartAsync();
|
|
|
|
|
|
|
|
|
|
await Task.Delay(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 20:14:39 +00:00
|
|
|
|
private async Task _client_Connected() => await _client.SetGameAsync(Config.CurrentGame);
|
2017-06-23 19:31:47 +00:00
|
|
|
|
|
|
|
|
|
// Defined within this class because a reference to the client is required
|
|
|
|
|
public void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
Logger.GetLogger("Runtime")("Caught cancel key. Will attempt to disconnect...").Wait();
|
|
|
|
|
_client.LogoutAsync().Wait();
|
|
|
|
|
_client.Dispose();
|
|
|
|
|
#if DEBUG
|
|
|
|
|
Console.WriteLine("Press enter to exit.");
|
|
|
|
|
Console.ReadLine();
|
|
|
|
|
#endif
|
|
|
|
|
Environment.Exit(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|