2018-05-11 06:13:00 +00:00
|
|
|
|
using Discord.WebSocket;
|
2018-05-06 20:09:17 +00:00
|
|
|
|
using Kerobot.Services;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Kerobot
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Kerobot main class, and the most accessible and useful class in the whole program.
|
|
|
|
|
/// Provides an interface for any part of the program to call into all existing services.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class Kerobot
|
|
|
|
|
{
|
|
|
|
|
// Partial class: Services are able to add their own methods and properties to this class.
|
2018-05-11 06:13:00 +00:00
|
|
|
|
// This is to prevent this file from having too many references to different and unrelated features.
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
|
|
|
|
private readonly InstanceConfig _icfg;
|
|
|
|
|
private readonly DiscordSocketClient _client;
|
2018-06-03 00:33:31 +00:00
|
|
|
|
private IReadOnlyCollection<Service> _services;
|
|
|
|
|
private IReadOnlyCollection<ModuleBase> _modules;
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets application instance configuration.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal InstanceConfig Config => _icfg;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the Discord client instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DiscordSocketClient DiscordClient => _client;
|
2018-06-03 00:33:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// All loaded services in an iterable form.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal IReadOnlyCollection<Service> Services => _services;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// All loaded modules in an iterable form.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal IReadOnlyCollection<ModuleBase> Modules => _modules;
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
|
|
|
|
internal Kerobot(InstanceConfig conf, DiscordSocketClient client)
|
|
|
|
|
{
|
|
|
|
|
_icfg = conf;
|
|
|
|
|
_client = client;
|
|
|
|
|
|
2018-05-11 06:13:00 +00:00
|
|
|
|
// 'Ready' event handler. Because there's no other place for it.
|
|
|
|
|
_client.Ready += async delegate
|
|
|
|
|
{
|
|
|
|
|
await InstanceLogAsync(true, "Kerobot", "Connected and ready.");
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-05 00:15:18 +00:00
|
|
|
|
// Get all services started up
|
|
|
|
|
_services = InitializeServices();
|
2018-05-11 06:13:00 +00:00
|
|
|
|
|
2018-06-05 00:15:18 +00:00
|
|
|
|
// Load externally defined functionality
|
|
|
|
|
_modules = ModuleLoader.Load(_icfg, this);
|
2018-05-11 06:13:00 +00:00
|
|
|
|
|
2018-06-05 00:15:18 +00:00
|
|
|
|
// Everything's ready to go. Print the welcome message here.
|
2018-05-11 06:13:00 +00:00
|
|
|
|
var ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
|
|
|
|
InstanceLogAsync(false, "Kerobot",
|
|
|
|
|
$"This is Kerobot v{ver.ToString(3)}. https://github.com/Noikoio/Kerobot").Wait();
|
2018-05-06 20:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-05 00:15:18 +00:00
|
|
|
|
private IReadOnlyCollection<Service> InitializeServices()
|
2018-05-06 20:09:17 +00:00
|
|
|
|
{
|
2018-05-11 06:13:00 +00:00
|
|
|
|
var svcList = new List<Service>();
|
|
|
|
|
|
|
|
|
|
// Put services here as they become usable.
|
|
|
|
|
_svcLogging = new Services.Logging.LoggingService(this);
|
|
|
|
|
svcList.Add(_svcLogging);
|
2018-06-05 00:15:18 +00:00
|
|
|
|
_svcGuildState = new Services.GuildState.GuildStateService(this);
|
|
|
|
|
svcList.Add(_svcGuildState);
|
2018-05-11 06:13:00 +00:00
|
|
|
|
|
2018-06-05 00:15:18 +00:00
|
|
|
|
return svcList.AsReadOnly();
|
2018-05-06 20:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns an open NpgsqlConnection instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="guild">
|
|
|
|
|
/// If manipulating guild-specific information, this parameter sets the database connection's search path.
|
|
|
|
|
/// </param>
|
|
|
|
|
internal async Task<NpgsqlConnection> GetOpenNpgsqlConnectionAsync(ulong? guild)
|
|
|
|
|
{
|
|
|
|
|
string cs = _icfg.PostgresConnString;
|
|
|
|
|
if (guild.HasValue) cs += ";searchpath=guild_" + guild.Value;
|
|
|
|
|
|
|
|
|
|
var db = new NpgsqlConnection(cs);
|
|
|
|
|
await db.OpenAsync();
|
|
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|