RegexBot/Kerobot/Kerobot.cs

82 lines
2.7 KiB
C#
Raw Normal View History

2018-05-11 06:13:00 +00:00
using Discord.WebSocket;
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
{
/// <summary>
/// Gets application instance configuration.
/// </summary>
2018-06-06 22:19:21 +00:00
internal InstanceConfig Config { get; }
/// <summary>
/// Gets the Discord client instance.
/// </summary>
2018-06-06 22:19:21 +00:00
public DiscordSocketClient DiscordClient { get; }
/// <summary>
2018-06-06 22:19:21 +00:00
/// Gets all loaded services in an iterable form.
/// </summary>
2018-06-06 22:19:21 +00:00
internal IReadOnlyCollection<Service> Services { get; }
/// <summary>
2018-06-06 22:19:21 +00:00
/// Gets all loaded modules in an iterable form.
/// </summary>
2018-06-06 22:19:21 +00:00
internal IReadOnlyCollection<ModuleBase> Modules { get; }
internal Kerobot(InstanceConfig conf, DiscordSocketClient client)
{
2018-06-06 22:19:21 +00:00
Config = conf;
DiscordClient = client;
// Get all services started up
2018-06-06 22:19:21 +00:00
Services = InitializeServices();
2018-05-11 06:13:00 +00:00
// Load externally defined functionality
2018-06-06 22:19:21 +00:00
Modules = ModuleLoader.Load(Config, this);
2018-05-11 06:13:00 +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();
}
private IReadOnlyCollection<Service> InitializeServices()
{
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);
_svcGuildState = new Services.GuildState.GuildStateService(this);
svcList.Add(_svcGuildState);
2018-05-11 06:13:00 +00:00
return svcList.AsReadOnly();
}
/// <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)
{
2018-06-06 22:19:21 +00:00
string cs = Config.PostgresConnString;
if (guild.HasValue) cs += ";searchpath=guild_" + guild.Value;
var db = new NpgsqlConnection(cs);
await db.OpenAsync();
return db;
}
}
}