Fix bug in TranslationsHandler where language lookup would fail if user messaged the bot directly

This commit is contained in:
Runebaas 2019-03-03 21:01:47 -05:00
parent f4897f7f2e
commit c1ff11ca34
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6

View file

@ -18,8 +18,7 @@ namespace Geekbot.net.Lib.Localization
private readonly IGeekbotLogger _logger;
private readonly Dictionary<ulong, string> _serverLanguages;
private Dictionary<string, Dictionary<string, Dictionary<string, string>>> _translations;
private List<string> _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<string, Dictionary<string, string>>();
var strDict = new Dictionary<string, string>();
strDict.Add(str.Key, lang.Value);
var strDict = new Dictionary<string, string>
{
{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<string>();
SupportedLanguages = new List<string>();
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<string> SupportedLanguages => _supportedLanguages;
public List<string> SupportedLanguages { get; private set; }
private async Task<GuildSettingsModel> GetGuild(ulong guildId)
{