From c1ff11ca34663245a4f10f74280ec50b0f7393d0 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 3 Mar 2019 21:01:47 -0500 Subject: [PATCH] Fix bug in TranslationsHandler where language lookup would fail if user messaged the bot directly --- .../Lib/Localization/TranslationHandler.cs | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index 88e518e..a1b650b 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -18,8 +18,7 @@ namespace Geekbot.net.Lib.Localization private readonly IGeekbotLogger _logger; private readonly Dictionary _serverLanguages; private Dictionary>> _translations; - private List _supportedLanguages; - + public TranslationHandler(DatabaseContext database, IGeekbotLogger logger) { _database = database; @@ -45,8 +44,10 @@ namespace Geekbot.net.Lib.Localization if (!sortedPerLanguage.ContainsKey(lang.Key)) { var commandDict = new Dictionary>(); - var strDict = new Dictionary(); - strDict.Add(str.Key, lang.Value); + var strDict = new Dictionary + { + {str.Key, lang.Value} + }; commandDict.Add(command.Key, strDict); sortedPerLanguage.Add(lang.Key, commandDict); } @@ -65,10 +66,10 @@ namespace Geekbot.net.Lib.Localization } _translations = sortedPerLanguage; - _supportedLanguages = new List(); + SupportedLanguages = new List(); foreach (var lang in sortedPerLanguage) { - _supportedLanguages.Add(lang.Key); + SupportedLanguages.Add(lang.Key); } } catch (Exception e) @@ -101,7 +102,7 @@ namespace Geekbot.net.Lib.Localization } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "Could not get guild langage", e); + _logger.Error(LogSource.Geekbot, "Could not get guild language", e); return "EN"; } } @@ -123,7 +124,8 @@ namespace Geekbot.net.Lib.Localization try { var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower(); - return _translations[await GetServerLanguage(context.Guild.Id)][command]; + var serverLanguage = await GetServerLanguage(context.Guild?.Id ?? 0); + return _translations[serverLanguage][command]; } catch (Exception e) { @@ -136,7 +138,8 @@ namespace Geekbot.net.Lib.Localization { try { - return _translations[await GetServerLanguage(context.Guild.Id)][command]; + var serverLanguage = await GetServerLanguage(context.Guild?.Id ?? 0); + return _translations[serverLanguage][command]; } catch (Exception e) { @@ -149,7 +152,7 @@ namespace Geekbot.net.Lib.Localization { try { - if (!_supportedLanguages.Contains(language)) return false; + if (!SupportedLanguages.Contains(language)) return false; var guild = await GetGuild(guildId); guild.Language = language; _database.GuildSettings.Update(guild); @@ -163,7 +166,7 @@ namespace Geekbot.net.Lib.Localization } } - public List SupportedLanguages => _supportedLanguages; + public List SupportedLanguages { get; private set; } private async Task GetGuild(ulong guildId) {