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);
            }
        }
Exemple #2
0
 public Bot(Id id, ConfBot config, BotInjector injector, ResourceFactory resourceFactory, CommandManager commandManager)
 {
     this.Id              = id;
     this.config          = config;
     this.Injector        = injector;
     this.resourceFactory = resourceFactory;
     this.commandManager  = commandManager;
 }
        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());
        }
Exemple #4
0
 public Bot(Id id, ConfBot config, BotInjector injector)
 {
     this.Id       = id;
     this.config   = config;
     this.Injector = injector;
 }
Exemple #5
0
        public Bot(Id id, ConfBot config, BotInjector injector)
        {
            this.Id       = id;
            this.config   = config;
            this.Injector = injector;

            // Registering config changes
            config.Language.Changed += async(s, e) =>
            {
                var langResult = await localization.LoadLanguage(e.NewValue, true);

                if (!langResult.Ok)
                {
                    Log.Error("Failed to load language file ({0})", langResult.Error);
                }
            };
            config.Events.IdleDelay.Changed += (s, e) => EnableIdleTickWorker();
            config.Events.OnIdle.Changed    += (s, e) => EnableIdleTickWorker();

            var builder = new DependencyBuilder(Injector);

            Injector.HideParentModule <CommandManager>();
            Injector.HideParentModule <DedicatedTaskScheduler>();
            Injector.AddModule(this);
            Injector.AddModule(config);
            Injector.AddModule(Injector);
            Injector.AddModule(config.Playlists);
            Injector.AddModule(config.History);
            Injector.AddModule(Id);
            builder.RequestModule <PlaylistIO>();
            builder.RequestModule <PlaylistManager>();
            builder.RequestModule <DedicatedTaskScheduler>();
            builder.RequestModule <TsFullClient>();
            builder.RequestModule <TsBaseFunctions, TsFullClient>();
            builder.RequestModule <Ts3Client>();
            builder.RequestModule <Player>();
            builder.RequestModule <CustomTargetPipe>();
            builder.RequestModule <IVoiceTarget, CustomTargetPipe>();
            builder.RequestModule <SessionManager>();
            builder.RequestModule <ResolveContext>();
            builder.RequestModule <CommandManager>();
            builder.RequestModule <LocalizationManager>();
            if (config.History.Enabled)
            {
                builder.RequestModule <HistoryManager>();
            }
            builder.RequestModule <PlayManager>();

            if (!builder.Build())
            {
                Log.Error("Missing bot module dependency");
                throw new Exception("Could not load all bot modules");
            }
            Injector.ClearHiddenParentModules();

            resourceResolver = Injector.GetModuleOrThrow <ResolveContext>();
            ts3FullClient    = Injector.GetModuleOrThrow <TsFullClient>();
            ts3client        = Injector.GetModuleOrThrow <Ts3Client>();
            player           = Injector.GetModuleOrThrow <Player>();
            Scheduler        = Injector.GetModuleOrThrow <DedicatedTaskScheduler>();
            var customTarget = Injector.GetModuleOrThrow <CustomTargetPipe>();

            player.SetTarget(customTarget);
            Injector.AddModule(ts3FullClient.Book);
            playManager    = Injector.GetModuleOrThrow <PlayManager>();
            targetManager  = Injector.GetModuleOrThrow <IVoiceTarget>();
            sessionManager = Injector.GetModuleOrThrow <SessionManager>();
            stats          = Injector.GetModuleOrThrow <Stats>();
            var commandManager = Injector.GetModuleOrThrow <CommandManager>();

            localization = Injector.GetModuleOrThrow <LocalizationManager>();

            idleTickWorker = Scheduler.Invoke(() => Scheduler.CreateTimer(OnIdle, TimeSpan.MaxValue, false)).Result;

            player.OnSongEnd     += playManager.SongStoppedEvent;
            player.OnSongUpdated += (s, e) => playManager.Update(e);
            // Update idle status events
            playManager.BeforeResourceStarted += (s, e) => { DisableIdleTickWorker(); return(Task.CompletedTask); };
            playManager.PlaybackStopped       += (s, e) => { EnableIdleTickWorker(); return(Task.CompletedTask); };
            // Used for custom scripts, like voice_mode, onsongstart
            playManager.BeforeResourceStarted += BeforeResourceStarted;
            playManager.AfterResourceStarted  += AfterResourceStarted;
            // Update the own status text to the current song title
            playManager.AfterResourceStarted += (s, e) => UpdateBotStatus();
            playManager.PlaybackStopped      += (s, e) => UpdateBotStatus();
            playManager.OnResourceUpdated    += (s, e) => UpdateBotStatus();
            // Log our resource in the history
            if (Injector.TryGet <HistoryManager>(out var historyManager))
            {
                playManager.AfterResourceStarted += (s, e) =>
                {
                    if (e.PlayInfo != null)
                    {
                        historyManager.LogAudioResource(new HistorySaveData(e.PlayResource.AudioResource, e.PlayInfo.ResourceOwnerUid));
                    }
                    return(Task.CompletedTask);
                }
            }
            ;
            // Update our thumbnail
            playManager.AfterResourceStarted += (s, e) => GenerateStatusImage(true, e);
            playManager.PlaybackStopped      += (s, e) => GenerateStatusImage(false, null);
            // Stats
            playManager.AfterResourceStarted += (s, e) => { stats.TrackSongStart(Id, e.ResourceData.AudioType); return(Task.CompletedTask); };
            playManager.ResourceStopped      += (s, e) => { stats.TrackSongStop(Id); return(Task.CompletedTask); };
            // Register callback for all messages happening
            ts3client.OnMessageReceived += OnMessageReceived;
            // Register callback to remove open private sessions, when user disconnects
            ts3FullClient.OnEachClientLeftView += OnClientLeftView;
            ts3client.OnBotConnected           += OnBotConnected;
            ts3client.OnBotDisconnected        += OnBotDisconnected;
            ts3client.OnBotStoppedReconnecting += OnBotStoppedReconnecting;
            // Alone mode
            ts3client.OnAloneChanged += OnAloneChanged;
            ts3client.OnAloneChanged += (s, e) => { customTarget.Alone = e.Alone; return(Task.CompletedTask); };
            // Whisper stall
            ts3client.OnWhisperNoTarget += (s, e) => player.SetStall();

            commandManager.RegisterCollection(MainCommands.Bag);
            // TODO remove after plugin rework
            var pluginManager = Injector.GetModuleOrThrow <PluginManager>();

            foreach (var plugin in pluginManager.Plugins)
            {
                if (plugin.Type == PluginType.CorePlugin || plugin.Type == PluginType.Commands)
                {
                    commandManager.RegisterCollection(plugin.CorePlugin.Bag);
                }
            }
            // Restore all alias from the config
            foreach (var alias in config.Commands.Alias.GetAllItems())
            {
                commandManager.RegisterAlias(alias.Key, alias.Value).UnwrapToLog(Log);
            }
        }
Exemple #6
0
 public Bot(ConfBot config, BotInjector injector)
 {
     this.config   = config;
     this.Injector = injector;
 }