コード例 #1
0
        public async Task RunBots(bool interactive)
        {
            var botConfigList = confRoot.GetAllBots();

            if (botConfigList is null)
            {
                return;
            }

            if (botConfigList.Length == 0)
            {
                if (!interactive)
                {
                    Log.Warn("No bots are configured in the load list.");
                    return;
                }

                Console.WriteLine("It seems like there are no bots configured.");
                Console.WriteLine("Fill out this quick setup to get started.");

                var newBot = CreateNewBot();
                newBot.Run.Value = true;

                var address = await Interactive.LoopActionAsync("Please enter the ip, domain or nickname (with port; default: 9987) where to connect to:", async addr =>
                {
                    if (await TsDnsResolver.TryResolve(addr) != null)
                    {
                        return(true);
                    }
                    Console.WriteLine("The address seems invalid or could not be resolved, continue anyway? [y/N]");
                    return(Interactive.UserAgree(defaultTo: false));
                });

                if (address is null)
                {
                    return;
                }
                newBot.Connect.Address.Value = address;
                Console.WriteLine("Please enter the server password (or leave empty for none):");
                newBot.Connect.ServerPassword.Password.Value = Console.ReadLine();

                if (!newBot.SaveNew(ConfigHelper.DefaultBotName))
                {
                    Log.Error("Could not save new bot. Ensure that the bot has access to the directory.");
                    return;
                }

                if (!confRoot.Save())
                {
                    Log.Error("Could not save root config. The bot won't start by default.");
                }

                var runResult = await RunBot(newBot);

                if (!runResult.Ok)
                {
                    Log.Error("Could not run bot ({0})", runResult.Error);
                }
                return;
            }

            var launchBotTasks = new List <Task <R <BotInfo, string> > >(botConfigList.Length);

            foreach (var instance in botConfigList)
            {
                if (!instance.Run)
                {
                    continue;
                }
                launchBotTasks.Add(RunBot(instance).ContinueWith(async t =>
                {
                    var result = await t;
                    if (!result.Ok)
                    {
                        Log.Error("Could not instantiate bot: {0}", result.Error);
                    }
                    return(result);
                }).Unwrap());
            }
            await Task.WhenAll(launchBotTasks);
        }