Exemplo n.º 1
0
        public async Task <bool> Execute(RequestContext ctx)
        {
            var db            = ctx.GetFeature <KarmaContext>();
            var currentLocale = ctx.GetFeature <Locale>();
            var cmd           = ctx.Command;
            var args          = ctx.EventArgs;

            if (cmd.Parameters.Length < 1)
            {
                await ctx.SendMessage(currentLocale["jetkarmabot.changelocale.getlocale"]);

                return(false);
            }
            else if (cmd.Parameters[0] == "list")
            {
                await ctx.SendMessage(currentLocale["jetkarmabot.changelocale.listalltext"] + "\n"
                                      + string.Join("\n", Locale.Select(a => a.Key)));

                return(false);
            }
            else if (cmd.Parameters[0] == "all")
            {
                await ctx.SendMessage(currentLocale["jetkarmabot.changelocale.errorall"]);

                return(false);
            }
            string localeId;

            if (Locale.ContainsLocale(cmd.Parameters[0]))
            {
                localeId = cmd.Parameters[0];
            }
            else
            {
                try
                {
                    localeId = Locale.FindByCommonName(cmd.Parameters[0]).Name;
                }
                catch (LocalizationException e)
                {
                    await ctx.SendMessage(
                        currentLocale["jetkarmabot.changelocale.toomany"] + "\n"
                        + string.Join("\n", (e.Data["LocaleNames"] as Locale[]).Select(x => x.Name)));

                    return(false);
                }
            }
            (await db.Chats.FindAsync(args.Message.Chat.Id)).Locale = localeId;
            log.Debug($"Changed language of chat {args.Message.Chat.Id} to {localeId}");

            currentLocale = Locale[localeId];

            await ctx.SendMessage(
                (currentLocale.HasNote ? currentLocale["jetkarmabot.changelocale.beforenote"] + currentLocale.Note + "\n" : "")
                + currentLocale["jetkarmabot.changelocale.justchanged"]);

            return(true);
        }