diff --git a/Geekbot.net/Commands/Admin.cs b/Geekbot.net/Commands/Admin.cs index a0fcc58..74b0ca8 100644 --- a/Geekbot.net/Commands/Admin.cs +++ b/Geekbot.net/Commands/Admin.cs @@ -5,7 +5,6 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; -using Serilog; using StackExchange.Redis; namespace Geekbot.net.Commands @@ -121,7 +120,8 @@ namespace Geekbot.net.Commands var success = _translation.SetLanguage(Context.Guild.Id, language); if (success) { - await ReplyAsync(_translation.GetString(Context.Guild.Id, "LanguageChanger", "Confirm")); + var trans = _translation.GetDict(Context); + await ReplyAsync(trans["Confirm"]); return; } await ReplyAsync( @@ -132,5 +132,21 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } + + [Command("lang", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Admin)] + [Summary("Change the bots language")] + public async Task getLanguage() + { + try + { + var trans = _translation.GetDict(Context); + await ReplyAsync(trans["GetLanguage"]); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/TranslationHandler.cs b/Geekbot.net/Lib/TranslationHandler.cs index 06587e6..1b90da1 100644 --- a/Geekbot.net/Lib/TranslationHandler.cs +++ b/Geekbot.net/Lib/TranslationHandler.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using Discord.Commands; using Discord.WebSocket; using Serilog; using StackExchange.Redis; @@ -14,7 +15,7 @@ namespace Geekbot.net.Lib private readonly IDatabase _redis; private Dictionary>> _translations; private Dictionary _serverLanguages; - public List _supportedLanguages; + private List _supportedLanguages; public TranslationHandler(IReadOnlyCollection clientGuilds, IDatabase redis, ILogger logger) { @@ -22,7 +23,6 @@ namespace Geekbot.net.Lib _redis = redis; _logger.Information("[Geekbot] Loading Translations"); LoadTranslations(); - CheckSupportedLanguages(); LoadServerLanguages(clientGuilds); } @@ -30,24 +30,40 @@ namespace Geekbot.net.Lib { try { - var translations = File.ReadAllText(Path.GetFullPath("./Storage/Translations.json")); - _translations = - Utf8Json.JsonSerializer.Deserialize>>>( - translations); - } - catch (Exception e) - { - _logger.Fatal(e, "Failed to load Translations"); - Environment.Exit(110); - } - } - - private void CheckSupportedLanguages() - { - try - { + var translationFile = File.ReadAllText(Path.GetFullPath("./Storage/Translations.json")); + var rawTranslations = Utf8Json.JsonSerializer.Deserialize>>>(translationFile); + var sortedPerLanguage = new Dictionary>>(); + foreach (var command in rawTranslations) + { + foreach (var str in command.Value) + { + foreach (var lang in str.Value) + { + if (!sortedPerLanguage.ContainsKey(lang.Key)) + { + var commandDict = new Dictionary>(); + var strDict = new Dictionary(); + strDict.Add(str.Key, lang.Value); + commandDict.Add(command.Key, strDict); + sortedPerLanguage.Add(lang.Key, commandDict); + } + if (!sortedPerLanguage[lang.Key].ContainsKey(command.Key)) + { + var strDict = new Dictionary(); + strDict.Add(str.Key, lang.Value); + sortedPerLanguage[lang.Key].Add(command.Key, strDict); + } + if (!sortedPerLanguage[lang.Key][command.Key].ContainsKey(str.Key)) + { + sortedPerLanguage[lang.Key][command.Key].Add(str.Key, lang.Value); + } + } + } + } + _translations = sortedPerLanguage; + _supportedLanguages = new List(); - foreach (var lang in _translations.First().Value.First().Value) + foreach (var lang in sortedPerLanguage) { _supportedLanguages.Add(lang.Key); } @@ -58,7 +74,7 @@ namespace Geekbot.net.Lib Environment.Exit(110); } } - + private void LoadServerLanguages(IReadOnlyCollection clientGuilds) { _serverLanguages = new Dictionary(); @@ -78,7 +94,7 @@ namespace Geekbot.net.Lib public string GetString(ulong guildId, string command, string stringName) { - var translation = _translations[command][stringName][_serverLanguages[guildId]]; + var translation = _translations[_serverLanguages[guildId]][command][stringName]; if (!string.IsNullOrWhiteSpace(translation)) return translation; translation = _translations[command][stringName]["EN"]; if (string.IsNullOrWhiteSpace(translation)) @@ -88,6 +104,20 @@ namespace Geekbot.net.Lib return translation; } + public Dictionary GetDict(ICommandContext context) + { + try + { + var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower(); + return _translations[_serverLanguages[context.Guild.Id]][command]; + } + catch (Exception e) + { + _logger.Error(e, "lol nope"); + return new Dictionary(); + } + } + public bool SetLanguage(ulong guildId, string language) { try @@ -113,6 +143,7 @@ namespace Geekbot.net.Lib public interface ITranslationHandler { string GetString(ulong guildId, string command, string stringName); + Dictionary GetDict(ICommandContext context); bool SetLanguage(ulong guildId, string language); List GetSupportedLanguages(); } diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 7f397c0..cde241f 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -248,6 +248,7 @@ namespace Geekbot.net case LogSeverity.Critical: case LogSeverity.Error: case LogSeverity.Warning: + if (logMessage.Contains("VOICE_STATE_UPDATE")) break; logger.Error(message.Exception, logMessage); break; default: diff --git a/Geekbot.net/Storage/Translations.json b/Geekbot.net/Storage/Translations.json index b801411..2d32dad 100644 --- a/Geekbot.net/Storage/Translations.json +++ b/Geekbot.net/Storage/Translations.json @@ -1,8 +1,12 @@ { - "LanguageChanger": { - "Confirm": { + "admin": { + "NewLanguageSet": { "EN": "I will in english from now on", - "CHDE": "I werd ab jetzt in schwiizerdüütsch antworte, äuuä" + "CHDE": "I werd ab jetzt uf schwiizerdüütsch antworte, äuuä" + }, + "GetLanguage": { + "EN": "I'm talking english", + "CHDE": "I red schwiizerdüütsch" } } } \ No newline at end of file