diff --git a/JetKarmaBot/CommandRouter.cs b/JetKarmaBot/CommandRouter.cs index 1824eaa..9cebeb4 100644 --- a/JetKarmaBot/CommandRouter.cs +++ b/JetKarmaBot/CommandRouter.cs @@ -19,16 +19,24 @@ namespace JetKarmaBot public bool Execute(object sender, MessageEventArgs args) { var text = args.Message.Text; - + if (CommandString.TryParse(text, out var cmd)) { if (cmd.UserName != null && cmd.UserName != BotUser.Username) // directed not at us! return false; - if (commands.ContainsKey(cmd.Command)) - return commands[cmd.Command].Execute(cmd, args); + try + { + if (commands.ContainsKey(cmd.Command)) + return commands[cmd.Command].Execute(cmd, args); + } + catch (Exception e) + { + Console.WriteLine(e); + } } + return false; } diff --git a/JetKarmaBot/Commands/ChangeLocaleCommand.cs b/JetKarmaBot/Commands/ChangeLocaleCommand.cs index aecfa50..86c2a06 100644 --- a/JetKarmaBot/Commands/ChangeLocaleCommand.cs +++ b/JetKarmaBot/Commands/ChangeLocaleCommand.cs @@ -1,16 +1,12 @@ -using System; using System.Collections.Generic; -using System.Linq; using Telegram.Bot; using Telegram.Bot.Args; -using Telegram.Bot.Types; using Perfusion; -using JetKarmaBot.Models; using JetKarmaBot.Services; namespace JetKarmaBot.Commands { - class ChangeLocaleCommand : IChatCommand + class LocaleCommand : IChatCommand { public IReadOnlyCollection Names => new[] { "changelocale", "locale" }; @@ -28,12 +24,14 @@ namespace JetKarmaBot.Commands return true; } db.Chats.Find(args.Message.Chat.Id).Locale = cmd.Parameters[0]; + db.SaveChanges(); + currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale]; + Client.SendTextMessageAsync( args.Message.Chat.Id, currentLocale["jetkarmabot.changelocale.justchanged"], replyToMessageId: args.Message.MessageId); - db.SaveChanges(); return true; } } diff --git a/JetKarmaBot/JetKarmaBot.cs b/JetKarmaBot/JetKarmaBot.cs index 7b95dd7..d7398f3 100644 --- a/JetKarmaBot/JetKarmaBot.cs +++ b/JetKarmaBot/JetKarmaBot.cs @@ -64,7 +64,7 @@ namespace JetKarmaBot Commands.Add(c.ResolveObject(new StartCommand())); Commands.Add(c.ResolveObject(new AwardCommand(Me))); Commands.Add(c.ResolveObject(new StatusCommand())); - Commands.Add(c.ResolveObject(new ChangeLocaleCommand())); + Commands.Add(c.ResolveObject(new LocaleCommand())); } #endregion diff --git a/JetKarmaBot/JetKarmaBot.csproj b/JetKarmaBot/JetKarmaBot.csproj index 259233e..f2f5fd2 100644 --- a/JetKarmaBot/JetKarmaBot.csproj +++ b/JetKarmaBot/JetKarmaBot.csproj @@ -20,6 +20,12 @@ Always + + Always + + + Always + diff --git a/JetKarmaBot/Services/Localization.cs b/JetKarmaBot/Services/Localization.cs index cd0987b..d3809e6 100644 --- a/JetKarmaBot/Services/Localization.cs +++ b/JetKarmaBot/Services/Localization.cs @@ -1,10 +1,7 @@ using System; -using System.Collections; using System.Collections.Generic; -using System.Data; using System.IO; using System.Linq; -using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Perfusion; @@ -18,23 +15,51 @@ namespace JetKarmaBot public Localization(Config cfg) { Log("Initializing..."); - if (!Directory.Exists("lang")) - Directory.CreateDirectory("lang"); + string langsFolder = "lang"; + if (!Directory.Exists(langsFolder)) + Directory.CreateDirectory(langsFolder); - foreach (string lang in Directory.EnumerateFiles("lang")) + foreach (string langFilePath in Directory.EnumerateFiles(langsFolder, "*.json")) { - string langname = Path.GetFileName(lang).Split(".")[0]; - Log("Found " + langname); - locales[langname] = JObject.Parse(File.ReadAllText(lang)).ToObject>(); + try + { + string langName = Path.GetFileNameWithoutExtension(langFilePath); + string langKey = langName.ToLowerInvariant(); + locales[langKey] = JObject.Parse(File.ReadAllText(langFilePath)).ToObject>(); + Log("Found " + langName); + } + catch (Exception e) + { + Log($"Error while parsing {langFilePath}!"); + Log(e); + } } - Log("Initialized!"); + + if (locales.Any()) + Log("Initialized!"); + else + throw new FileNotFoundException($"No locales found in {langsFolder}!"); } public Locale this[string locale] { - get => new Locale(locales[locale], locale); + get + { + locale = locale.ToLowerInvariant(); + return new Locale(locales[locale], locale); + } } - void Log(string Message) => Console.WriteLine($"[{nameof(Localization)}]: {Message}"); + public bool ContainsLocale(string locale) + { + locale = locale.ToLowerInvariant(); + return locales.ContainsKey(locale); + } + + void Log(string Message) + => Console.WriteLine($"[{nameof(Localization)}]: {Message}"); + + void Log(Exception e) => Console.WriteLine(e); + public class Locale { private Dictionary locale; @@ -44,20 +69,7 @@ namespace JetKarmaBot this.locale = locale; this.localeName = localeName; } - public string this[string name] - { - get - { - if (!locale.ContainsKey(name)) - { - return "unknown"; - } - else - { - return locale[name]; - } - } - } + public string this[string name] => locale.ContainsKey(name) ? locale[name] : "unknown"; } } } \ No newline at end of file