Exemple #1
0
        private (Bot?bot, BotInfo?info) InstantiateNewBot(ConfBot config)
        {
            lock (lockObj)
            {
                if (!string.IsNullOrEmpty(config.Name))
                {
                    var maybeBot = GetBotSave(config.Name);
                    if (maybeBot != null)
                    {
                        return(null, maybeBot.GetInfo());
                    }
                }

                var id = GetFreeId();
                if (id == null)
                {
                    return(null, null);                      // "BotManager is shutting down"
                }
                var botInjector = new BotInjector(coreInjector);
                botInjector.AddModule(botInjector);
                botInjector.AddModule(new Id(id.Value));
                botInjector.AddModule(config);
                if (!botInjector.TryCreate <Bot>(out var bot))
                {
                    return(null, null);                      // "Failed to create new Bot"
                }
                InsertBot(bot);
                return(bot, null);
            }
        }
        public R <BotInfo, string> RunBot(ConfBot config)
        {
            Bot bot;

            lock (lockObj)
            {
                if (!string.IsNullOrEmpty(config.Name))
                {
                    bot = GetBotSave(config.Name);
                    if (bot != null)
                    {
                        return(bot.GetInfo());
                    }
                }

                var id = GetFreeId();
                if (id == null)
                {
                    return("BotManager is shutting down");
                }

                var botInjector = new BotInjector(coreInjector);
                botInjector.AddModule(botInjector);
                botInjector.AddModule(new TS3Client.Helper.Id(id.Value));
                botInjector.AddModule(config);
                if (!botInjector.TryCreate(out bot))
                {
                    return("Failed to create new Bot");
                }
                InsertBot(bot);
            }

            lock (bot.SyncRoot)
            {
                var initializeResult = bot.InitializeBot();
                if (!initializeResult.Ok)
                {
                    StopBot(bot);
                    return($"Bot failed to connect ({initializeResult.Error})");
                }
            }
            return(bot.GetInfo());
        }