diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index 0925e54..ba31f3f 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -138,7 +138,7 @@ namespace Geekbot.net.Commands.Admin } await ReplyAsync( - $"That doesn't seem to be a supported language\r\nSupported Languages are {string.Join(", ", _translation.GetSupportedLanguages())}"); + $"That doesn't seem to be a supported language\r\nSupported Languages are {string.Join(", ", _translation.SupportedLanguages)}"); } catch (Exception e) { diff --git a/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs b/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs index 031b266..5e010f9 100644 --- a/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs +++ b/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs @@ -9,9 +9,6 @@ namespace Geekbot.net.Lib.AlmostRedis { private readonly GeekbotLogger _logger; private readonly RunParameters _runParameters; - private IDatabase _database; - private ConnectionMultiplexer _connection; - private IAlmostRedis _almostRedisImplementation; public AlmostRedis(GeekbotLogger logger, RunParameters runParameters) { @@ -21,24 +18,18 @@ namespace Geekbot.net.Lib.AlmostRedis public void Connect() { - _connection = ConnectionMultiplexer.Connect($"{_runParameters.RedisHost}:{_runParameters.RedisPort}"); - _database = _connection.GetDatabase(int.Parse(_runParameters.RedisDatabase)); - _logger.Information(LogSource.Redis, $"Connected to Redis on {_connection.Configuration} at {_database.Database}"); + Connection = ConnectionMultiplexer.Connect($"{_runParameters.RedisHost}:{_runParameters.RedisPort}"); + Db = Connection.GetDatabase(int.Parse(_runParameters.RedisDatabase)); + _logger.Information(LogSource.Redis, $"Connected to Redis on {Connection.Configuration} at {Db.Database}"); } - public IDatabase Db - { - get { return _database; } - } + public IDatabase Db { get; private set; } + + public ConnectionMultiplexer Connection { get; private set; } - public ConnectionMultiplexer Connection - { - get { return _connection; } - } - public IEnumerable GetAllKeys() { - return _connection.GetServer($"{_runParameters.RedisHost}:{_runParameters.RedisPort}", int.Parse(_runParameters.RedisDatabase)).Keys(); + return Connection.GetServer($"{_runParameters.RedisHost}:{_runParameters.RedisPort}", int.Parse(_runParameters.RedisDatabase)).Keys(); } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/ITranslationHandler.cs b/Geekbot.net/Lib/Localization/ITranslationHandler.cs index 3cd9549..df4d707 100644 --- a/Geekbot.net/Lib/Localization/ITranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/ITranslationHandler.cs @@ -9,6 +9,6 @@ namespace Geekbot.net.Lib.Localization Dictionary GetDict(ICommandContext context); Dictionary GetDict(ICommandContext context, string command); bool SetLanguage(ulong guildId, string language); - List GetSupportedLanguages(); + List SupportedLanguages { get; } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index afcd439..f8e2d88 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -16,17 +16,17 @@ namespace Geekbot.net.Lib.Localization { private readonly DatabaseContext _database; private readonly IGeekbotLogger _logger; + private readonly Dictionary _serverLanguages; private Dictionary>> _translations; - private Dictionary _serverLanguages; private List _supportedLanguages; - public TranslationHandler(IReadOnlyCollection clientGuilds, DatabaseContext database, IGeekbotLogger logger) + public TranslationHandler(DatabaseContext database, IGeekbotLogger logger) { _database = database; _logger = logger; _logger.Information(LogSource.Geekbot, "Loading Translations"); LoadTranslations(); - LoadServerLanguages(clientGuilds); + _serverLanguages = new Dictionary(); } private void LoadTranslations() @@ -78,28 +78,37 @@ namespace Geekbot.net.Lib.Localization } } - private void LoadServerLanguages(IReadOnlyCollection clientGuilds) + private string GetServerLanguage(ulong guildId) { - _serverLanguages = new Dictionary(); - foreach (var guild in clientGuilds) + try { - var language = _database.GuildSettings - .FirstOrDefault(g => g.GuildId.Equals(guild.Id.AsLong())) - ?.Language ?? "EN"; - if (string.IsNullOrEmpty(language) || !_supportedLanguages.Contains(language)) + string lang; + try { - _serverLanguages[guild.Id] = "EN"; + lang = _serverLanguages[guildId]; + if (!string.IsNullOrEmpty(lang)) + { + return lang; + } + throw new Exception(); } - else + catch { - _serverLanguages[guild.Id] = language; + lang = GetGuild(guildId).Language ?? "EN"; + _serverLanguages[guildId] = lang; + return lang; } } + catch (Exception e) + { + _logger.Error(LogSource.Geekbot, "Could not get guild langage", e); + return "EN"; + } } public string GetString(ulong guildId, string command, string stringName) { - var translation = _translations[_serverLanguages[guildId]][command][stringName]; + var translation = _translations[GetServerLanguage(guildId)][command][stringName]; if (!string.IsNullOrWhiteSpace(translation)) return translation; translation = _translations[command][stringName]["EN"]; if (string.IsNullOrWhiteSpace(translation)) @@ -114,7 +123,7 @@ namespace Geekbot.net.Lib.Localization try { var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower(); - return _translations[_serverLanguages[context.Guild.Id]][command]; + return _translations[GetServerLanguage(context.Guild.Id)][command]; } catch (Exception e) { @@ -127,7 +136,7 @@ namespace Geekbot.net.Lib.Localization { try { - return _translations[_serverLanguages[context.Guild.Id]][command]; + return _translations[GetServerLanguage(context.Guild.Id)][command]; } catch (Exception e) { @@ -154,10 +163,7 @@ namespace Geekbot.net.Lib.Localization } } - public List GetSupportedLanguages() - { - return _supportedLanguages; - } + public List SupportedLanguages => _supportedLanguages; private GuildSettingsModel GetGuild(ulong guildId) { diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index f818802..86a3507 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -152,7 +152,7 @@ namespace Geekbot.net _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); _logger.Information(LogSource.Geekbot, "Registering Stuff"); - var translationHandler = new TranslationHandler(_client.Guilds, _database, _logger); + var translationHandler = new TranslationHandler(_database, _logger); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); var reactionListener = new ReactionListener(_redis.Db); await _commands.AddModulesAsync(Assembly.GetEntryAssembly());