From a1b5bd1955e50185f20404849c1a5051686b009f Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 13 May 2018 18:47:01 +0200 Subject: [PATCH] Add more run parameters, remove first launch stuff, store token in globals --- Geekbot.net/Database/RedisMigration.cs | 4 +- .../Lib/ReactionListener/ReactionListener.cs | 18 +++---- Geekbot.net/Lib/RunParameters.cs | 47 ++++++++++++++----- Geekbot.net/Program.cs | 43 ++++------------- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 1cd73b0..237ce60 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -29,7 +29,7 @@ namespace Geekbot.net.Database _client = client; } - public async Task Migrate() + public Task Migrate() { _logger.Information(LogSource.Geekbot, "Starting migration process"); foreach (var guild in _client.Guilds) @@ -283,6 +283,8 @@ namespace Geekbot.net.Database _logger.Information(LogSource.Geekbot, $"Finished Migration for {guild.Name}"); } _logger.Information(LogSource.Geekbot, "Finished migration process"); + + return Task.CompletedTask;; } private QuoteModel CreateQuoteObject(ulong guild, QuoteObjectDto quote) diff --git a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs index 950edd1..18490db 100644 --- a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs +++ b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs @@ -9,22 +9,22 @@ namespace Geekbot.net.Lib.ReactionListener { public class ReactionListener : IReactionListener { - private readonly IDatabase _database; + private readonly IDatabase _redis; private Dictionary> _listener; - public ReactionListener(IDatabase database) + public ReactionListener(IDatabase redis) { - _database = database; + _redis = redis; LoadListeners(); } private Task LoadListeners() { - var ids = _database.SetMembers("MessageIds"); + var ids = _redis.SetMembers("MessageIds"); _listener = new Dictionary>(); foreach (var id in ids) { - var reactions = _database.HashGetAll($"Messages:{id}"); + var reactions = _redis.HashGetAll($"Messages:{id}"); var messageId = id; var emojiDict = new Dictionary(); foreach (var r in reactions) @@ -54,12 +54,12 @@ namespace Geekbot.net.Lib.ReactionListener public Task AddRoleToListener(string messageId, IEmote emoji, IRole role) { - if (_database.SetMembers("MessageIds").All(e => e.ToString() != messageId)) + if (_redis.SetMembers("MessageIds").All(e => e.ToString() != messageId)) { - _database.SetAdd("MessageIds", messageId); + _redis.SetAdd("MessageIds", messageId); } - _database.HashSet($"Messages:{messageId}", new[] {new HashEntry(emoji.ToString(), role.Id.ToString())}); - _database.SetAdd("MessageIds", messageId); + _redis.HashSet($"Messages:{messageId}", new[] {new HashEntry(emoji.ToString(), role.Id.ToString())}); + _redis.SetAdd("MessageIds", messageId); if (_listener.ContainsKey(messageId)) { _listener[messageId].Add(emoji, role.Id); diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index eb3ca37..7942b76 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -1,20 +1,17 @@ -using System; -using CommandLine; +using CommandLine; namespace Geekbot.net.Lib { public class RunParameters { - /** - * General Parameters - */ - [Option('V', "verbose", Default = false, HelpText = "Prints all messages to standard output.")] + /************************************ + * General * + ************************************/ + + [Option('V', "verbose", Default = false, HelpText = "Logs everything.")] public bool Verbose { get; set; } - [Option('r', "reset", Default = false, HelpText = "Resets the bot")] - public bool Reset { get; set; } - - [Option('j', "log-json", Default = false, HelpText = "Logs messages as json")] + [Option('j', "log-json", Default = false, HelpText = "Logger outputs json")] public bool LogJson { get; set; } [Option('a', "disable-api", Default = false, HelpText = "Disables the web api")] @@ -26,9 +23,10 @@ namespace Geekbot.net.Lib [Option("token", Default = null, HelpText = "Set a new bot token")] public string Token { get; set; } - /** - * Database Stuff - */ + /************************************ + * Database * + ************************************/ + [Option("in-memory", Default = false, HelpText = "Uses the in-memory database instead of postgresql")] public bool InMemory { get; set; } @@ -47,5 +45,28 @@ namespace Geekbot.net.Lib [Option("db-password", Default = "", HelpText = "Set a posgresql password")] public string DbPassword { get; set; } + + /************************************ + * Redis * + ************************************/ + + [Option("redis-host", Default = "127.0.0.1", HelpText = "Set a redis host")] + public string RedisHost { get; set; } + + [Option("redis-port", Default = "6379", HelpText = "Set a redis port")] + public string RedisPort { get; set; } + + [Option("redis-database", Default = "6", HelpText = "Select a redis database (1-15)")] + public string RedisDatabase { get; set; } + + /************************************ + * WebApi * + ************************************/ + + [Option("api-host", Default = "127.0.0.1", HelpText = "Host on which the WebApi listens")] + public string ApiHost { get; set; } + + [Option("api-port", Default = "12995", HelpText = "Port on which the WebApi listens")] + public string ApiPort { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 42dc856..8084127 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -38,7 +38,6 @@ namespace Geekbot.net private RedisValue _token; private GeekbotLogger _logger; private IUserRepository _userRepository; - private bool _firstStart; private RunParameters _runParameters; private static void Main(string[] args) @@ -84,10 +83,13 @@ namespace Geekbot.net _client.Log += discordLogger.Log; _commands = new CommandService(); + _database = new DatabaseInitializer(runParameters, logger).Initzialize(); + _globalSettings = new GlobalSettings(_database); + try { - var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379"); - _redis = redisMultiplexer.GetDatabase(6); + var redisMultiplexer = ConnectionMultiplexer.Connect($"{runParameters.RedisHost}:{runParameters.RedisPort}"); + _redis = redisMultiplexer.GetDatabase(int.Parse(runParameters.RedisDatabase)); logger.Information(LogSource.Redis, $"Connected to db {_redis.Database}"); } catch (Exception e) @@ -96,20 +98,16 @@ namespace Geekbot.net Environment.Exit(GeekbotExitCode.RedisConnectionFailed.GetHashCode()); } - _token = runParameters.Token ?? _redis.StringGet("discordToken"); + _token = runParameters.Token ?? _globalSettings.GetKey("DiscordToken"); if (_token.IsNullOrEmpty) { Console.Write("Your bot Token: "); var newToken = Console.ReadLine(); - _redis.StringSet("discordToken", newToken); - _redis.StringSet("Game", "Ping Pong"); + _globalSettings.SetKey("DiscordToken", newToken); + _globalSettings.SetKey("Game", "Ping Pong"); _token = newToken; - _firstStart = true; } - _database = new DatabaseInitializer(runParameters, logger).Initzialize(); - _globalSettings = new GlobalSettings(_database); - _services = new ServiceCollection(); _userRepository = new UserRepository(_database, logger); @@ -177,13 +175,7 @@ namespace Geekbot.net _client.UserLeft += handlers.UserLeft; _client.ReactionAdded += handlers.ReactionAdded; _client.ReactionRemoved += handlers.ReactionRemoved; - - if (_firstStart || _runParameters.Reset) - { - _logger.Information(LogSource.Geekbot, "Finishing setup"); - await FinishSetup(); - _logger.Information(LogSource.Geekbot, "Setup finished"); - } + if (!_runParameters.DisableApi) { StartWebApi(); @@ -209,24 +201,9 @@ namespace Geekbot.net private void StartWebApi() { _logger.Information(LogSource.Api, "Starting Webserver"); - var webApiUrl = new Uri("http://localhost:12995"); + var webApiUrl = new Uri($"http://{_runParameters.ApiHost}:{_runParameters.ApiPort}"); new NancyHost(webApiUrl).Start(); _logger.Information(LogSource.Api, $"Webserver now running on {webApiUrl}"); } - - private async Task FinishSetup() - { - try - { - // ToDo: Set bot avatar - var appInfo = await _client.GetApplicationInfoAsync(); - _redis.StringSet("botOwner", appInfo.Owner.Id); - } - catch (Exception e) - { - _logger.Warning(LogSource.Geekbot, "Setup Failed, couldn't retrieve discord application data", e); - } - return Task.CompletedTask; - } } } \ No newline at end of file