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-11-29 23:33:08 +00:00
|
|
|
|
// Welcome message
|
|
|
|
|
string name = nameof(RegexBot);
|
|
|
|
|
string sv = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
|
|
|
|
|
Logger.GetLogger(name)
|
|
|
|
|
.Invoke($"{name} v{sv} - https://github.com/Noikoio/RegexBot")
|
|
|
|
|
.Wait();
|
|
|
|
|
|
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-12-23 06:20:06 +00:00
|
|
|
|
// Set Discord client settings
|
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-12-23 06:20:06 +00:00
|
|
|
|
// Hook up basic handlers and other references
|
2017-06-23 19:31:47 +00:00
|
|
|
|
_client.Connected += _client_Connected;
|
2017-12-23 06:20:06 +00:00
|
|
|
|
EntityCache.EntityCache.SetClient(_client);
|
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),
|
2018-03-22 06:27:19 +00:00
|
|
|
|
new Module.ModCommands.ModCommands(_client),
|
2017-11-12 03:12:24 +00:00
|
|
|
|
new Module.AutoRespond.AutoRespond(_client),
|
2018-03-21 21:06:32 +00:00
|
|
|
|
new Module.EntryAutoRole.EntryAutoRole(_client),
|
2018-08-17 07:45:26 +00:00
|
|
|
|
new Module.VoiceRoleSync.VoiceRoleSync(_client),
|
2018-02-20 20:25:29 +00:00
|
|
|
|
|
|
|
|
|
// EntityCache loads before anything using it
|
2018-04-04 18:00:58 +00:00
|
|
|
|
new EntityCache.ECModule(_client),
|
2018-02-20 20:25:29 +00:00
|
|
|
|
new Module.ModLogs.ModLogs(_client)
|
2017-07-26 22:36:59 +00:00
|
|
|
|
};
|
2017-12-23 06:20:06 +00:00
|
|
|
|
|
|
|
|
|
// Set up logging
|
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-12-23 06:20:06 +00:00
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|