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
{
///
/// Gets application instance configuration.
///
internal InstanceConfig Config { get; }
///
/// Gets the Discord client instance.
///
public DiscordSocketClient DiscordClient { get; }
///
/// Gets all loaded services in an iterable form.
///
internal IReadOnlyCollection Services { get; }
///
/// Gets all loaded modules in an iterable form.
///
internal IReadOnlyCollection Modules { get; }
internal Kerobot(InstanceConfig conf, DiscordSocketClient client)
{
Config = conf;
DiscordClient = client;
// Get all services started up
Services = InitializeServices();
// Load externally defined functionality
Modules = ModuleLoader.Load(Config, this);
// Everything's ready to go. 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 IReadOnlyCollection InitializeServices()
{
var svcList = new List();
// Put services here as they become usable.
_svcLogging = new Services.Logging.LoggingService(this);
svcList.Add(_svcLogging);
_svcGuildState = new Services.GuildState.GuildStateService(this);
svcList.Add(_svcGuildState);
return 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 = Config.PostgresConnString;
if (guild.HasValue) cs += ";searchpath=guild_" + guild.Value;
var db = new NpgsqlConnection(cs);
await db.OpenAsync();
return db;
}
}
}