using Discord.WebSocket; using Kerobot.Services; using Npgsql; using System.Collections.Generic; using System.Threading.Tasks; namespace Kerobot { /// /// 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. /// public partial class Kerobot { // Partial class: Services are able to add their own methods and properties to this class. // This is to prevent this file from having too many references to different and unrelated features. private readonly InstanceConfig _icfg; private readonly DiscordSocketClient _client; private IReadOnlyCollection _services; private IReadOnlyCollection _modules; /// /// Gets application instance configuration. /// internal InstanceConfig Config => _icfg; /// /// Gets the Discord client instance. /// public DiscordSocketClient DiscordClient => _client; /// /// All loaded services in an iterable form. /// internal IReadOnlyCollection Services => _services; /// /// All loaded modules in an iterable form. /// internal IReadOnlyCollection Modules => _modules; internal Kerobot(InstanceConfig conf, DiscordSocketClient client) { _icfg = conf; _client = client; // 'Ready' event handler. Because there's no other place for it. _client.Ready += async delegate { await InstanceLogAsync(true, "Kerobot", "Connected and ready."); }; InitializeServices(); // TODO prepare modules here // Everything's ready to go by now. Print the welcome message here. var ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; InstanceLogAsync(false, "Kerobot", $"This is Kerobot v{ver.ToString(3)}. https://github.com/Noikoio/Kerobot").Wait(); } private void InitializeServices() { var svcList = new List(); // Put services here as they become usable. _svcLogging = new Services.Logging.LoggingService(this); svcList.Add(_svcLogging); _services = svcList.AsReadOnly(); } /// /// Returns an open NpgsqlConnection instance. /// /// /// If manipulating guild-specific information, this parameter sets the database connection's search path. /// internal async Task 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; } } }