Esempio n. 1
0
        private static FritzBot CreateFritzBot(IChatService chatService)
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IChatService>(chatService)
            .AddLogging();

            var config = new ConfigurationBuilder()
                         .AddJsonFile("appsettings.json", true)
                         .AddUserSecrets("78c713a0-80e0-4e16-956a-33cf16f08a02")                // Same as Fritz.StreamTools
                         .Build();

            serviceCollection.AddSingleton <IConfiguration>(config);

            serviceCollection.AddHttpClient("ShoutoutCommand", c =>
            {
                c.BaseAddress = new Uri("https://api.twitch.tv/kraken/channels/");
                c.DefaultRequestHeaders.Add("client-id", config["StreamServices:Twitch:ClientId"]);
            });

            FritzBot.RegisterCommands(serviceCollection);
            var svcProvider   = serviceCollection.BuildServiceProvider();
            var loggerFactory = svcProvider.GetService <ILoggerFactory>()
                                .AddConsole(LogLevel.Information);

            return(new FritzBot(config, svcProvider, loggerFactory));
        }
        public void ShouldLogIfCommandsToFast()
        {
            var sut = new FritzBot(_config.Object, _serviceProvider.Object, _loggerFactory.Object);

            Task.WaitAll(sut.StartAsync(new CancellationToken()));

            var args = new ChatMessageEventArgs
            {
                Message     = "!help",
                ServiceName = "TestService",
                UserName    = "******",
                IsModerator = false,
                IsOwner     = false,
            };

            _chatservice.Raise(cs => cs.ChatMessage += null, args);
            _chatservice.Raise(cs => cs.ChatMessage += null, args);

            const string expectWarning = "Ignoring command help from testusername on TestService. Cooldown active";
            //_logger.Verify(
            //			m => m.Log(
            //						 LogLevel.Warning,
            //						 It.IsAny<EventId>(),

            //						 //It.Is<FormattedLogValues>(v => v.ToString().Contains(expectWarning)),
            //						 It.IsAny<Exception>(),
            //						 It.IsAny<Func<object, Exception, string>>())
            //);
        }
Esempio n. 3
0
        private static FritzBot CreateFritzBot(IChatService chatService)
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IChatService>(chatService)
            .AddLogging();

            var config = new ConfigurationBuilder()
                         .AddJsonFile("appsettings.json", true)
                         .AddUserSecrets("78c713a0-80e0-4e16-956a-33cf16f08a02")                // Same as Fritz.StreamTools
                         .Build();

            serviceCollection.AddSingleton <IConfiguration>(config);

            serviceCollection.AddHttpClient("ShoutoutCommand", c =>
            {
                c.DefaultRequestHeaders.Add("client-id", config["StreamServices:Twitch:ClientId"]);
            });

            FritzBot.RegisterCommands(serviceCollection);

            var loggerService = LoggerFactory.Create(configure =>
                                                     configure.AddSimpleConsole(options =>
            {
                options.IncludeScopes = true;
            })
                                                     .SetMinimumLevel(LogLevel.Information)
                                                     );
            var svcProvider   = serviceCollection.BuildServiceProvider();
            var loggerFactory = svcProvider.GetService <ILoggerFactory>();

            return(new FritzBot(config, svcProvider, loggerFactory));
        }
Esempio n. 4
0
        public static void Execute(
            IServiceCollection services,
            IConfiguration configuration)
        {
            Configuration = configuration;

            services.AddSingleton <RundownRepository>();
            services.Configure <FollowerGoalConfiguration>(configuration.GetSection("FollowerGoal"));
            services.Configure <FollowerCountConfiguration>(configuration.GetSection("FollowerCount"));
            services.AddStreamingServices(configuration);
            services.Configure <GitHubConfiguration>(configuration.GetSection("GitHub"));
            services.AddSingleton <FollowerClient>();
            services.AddAspNetFeatures();

            services.AddSingleton <IConfigureOptions <SignalrTagHelperOptions>, ConfigureSignalrTagHelperOptions>();
            services.AddSingleton <SignalrTagHelperOptions>(cfg => cfg.GetService <IOptions <SignalrTagHelperOptions> >().Value);

            services.AddSingleton <IAttentionClient, AttentionHub>();

            services.AddSingleton <IHostedService, FritzBot>();
            services.AddSingleton(new GitHubClient(new ProductHeaderValue("Fritz.StreamTools")));
            FritzBot.RegisterCommands(services);

            services.AddLazyCache();

            RegisterGitHubServices(services, configuration);
        }
    public static DiscordClient AddFritzBot(this DiscordClient client)
    {
        _Bot = new FritzBot();
        _Bot.Initialize(client);

        return(client);
    }
Esempio n. 6
0
        public void ShouldIgnoreCommandsToFastIfModerator()
        {
            var sut = new FritzBot(_config.Object, _serviceProvider.Object, _loggerFactory.Object);

            Task.WaitAll(sut.StartAsync(new CancellationToken()));

            var args = new ChatMessageEventArgs
            {
                Message     = "!help",
                ServiceName = "TestService",
                UserName    = "******",
                IsModerator = true,
                IsOwner     = false
            };

            _chatservice.Raise(cs => cs.ChatMessage += null, args);
            _chatservice.Raise(cs => cs.ChatMessage += null, args);

            var verifyTimes = Moq.Times.Once();

#if !DEBUG
            verifyTimes = Moq.Times.Exactly(2);
#endif

            _chatservice.Verify(sm => sm.SendMessageAsync(
                                    It.Is <string>(x => x.StartsWith("Supported commands:"))),
                                verifyTimes);
        }
Esempio n. 7
0
        public void ShouldSetDefaultCooldownTimeIsMissingInConfiguration()
        {
            var sut = new FritzBot(_config.Object, _serviceProvider.Object);

            Task.WaitAll(sut.StartAsync(new CancellationToken()));

            Assert.Equal(TimeSpan.Zero, sut.CooldownTime);
        }
Esempio n. 8
0
        public void ShouldConfigureCooldown()
        {
            // arrange

            // act
            var sut = new FritzBot(_Config, _LoggerFactory);

            // assert
            Assert.Equal(_Config[FritzBot.CONFIGURATION_ROOT + ":CooldownTime"], sut.CooldownTime.ToString());
        }
Esempio n. 9
0
        public void ShouldSetCooldownTimeWhenSetInConfiguration(int cooldownTime)
        {
            _config.SetupGet(s => s[FritzBot.ConfigurationRoot + ":CooldownTime"]).Returns(cooldownTime.ToString());

            var sut = new FritzBot(_config.Object, _serviceProvider.Object);

            Task.WaitAll(sut.StartAsync(new CancellationToken()));

            Assert.Equal(TimeSpan.Parse(cooldownTime.ToString()), sut.CooldownTime);
        }
Esempio n. 10
0
        public void ShouldRegisterCommands()
        {
            // Arrange

            // Act
            var sut = new FritzBot();

            sut.Initialize(_Config, null, _LoggerFactory);

            // Assert
            Assert.NotEmpty(FritzBot._CommandRegistry);
        }
		public void ShouldReturnEchoMessage()
		{
			var sut = new FritzBot(_config.Object, _serviceProvider.Object, _loggerFactory.Object);
			Task.WaitAll(sut.StartAsync(new CancellationToken()));
			var args = new ChatMessageEventArgs
			{
				Message = "!echo Test Message",
				UserName = "******",
				IsModerator = false,
				IsOwner = false
			};

			_chatservice.Raise(cs => cs.ChatMessage += null, args);
			_chatservice.Verify(sm => sm.SendWhisperAsync(
						It.IsAny<string>()
						, It.Is<string>(x => x.StartsWith("Echo reply: Test Message"))), Times.AtLeastOnce);
		}
		public void ShouldReturnHelpMessage()
		{

			var sut = new FritzBot(_config.Object, _serviceProvider.Object, _loggerFactory.Object);
			Task.WaitAll(sut.StartAsync(new CancellationToken()));
			var args = new ChatMessageEventArgs
			{
				Message = "!help",
				UserName = "******",
				IsModerator = false,
				IsOwner = false
			};

			_chatservice.Raise(cs => cs.ChatMessage += null, args);
			_chatservice.Verify(sm => sm.SendMessageAsync(
<<<<<<< HEAD
						It.Is<string>(x => x.Contains("Supported commands: "))), Times.AtLeastOnce);
Esempio n. 13
0
        public void ShouldReturnInfoWhenTryInvokeNonRegisterCommand()
        {
            var sut = new FritzBot(_config.Object, _serviceProvider.Object);

            Task.WaitAll(sut.StartAsync(new CancellationToken()));

            var args = new ChatMessageEventArgs
            {
                Message  = "!SpecialNonExistingCommand",
                UserName = "******"
            };

            _chatservice.Raise(cs => cs.ChatMessage += null, args);
            _chatservice.Verify(sm => sm.SendWhisperAsync(
                                    It.IsAny <string>(),
                                    It.Is <string>(x => x.Equals(FritzBot.UnknownCommandMessage))),
                                Times.Once);
        }
Esempio n. 14
0
        public void ShouldInvokeExtendedCommand()
        {
            var sut = new FritzBot(_config.Object, _serviceProvider.Object);

            Task.WaitAll(sut.StartAsync(new CancellationToken()));

            var args = new ChatMessageEventArgs
            {
                Message  = "FakeExtendedCommand",
                UserName = "******"
            };

            _chatservice.Raise(cs => cs.ChatMessage += null, args);
            _chatservice.Verify(sm => sm.SendWhisperAsync(
                                    It.IsAny <string>(),
                                    It.Is <string>(x => x.Equals(_extendedCommand.ExecutedAnswer))),
                                Times.Once);
        }
        public void ShouldReturnLinkTitle()
        {
            var sut = new FritzBot(_config.Object, _serviceProvider.Object, _loggerFactory.Object);

            Task.WaitAll(sut.StartAsync(new CancellationToken()));

            var args = new ChatMessageEventArgs
            {
                Message     = "Hey! Check this link: www.google.com",
                UserName    = "******",
                IsModerator = false,
                IsOwner     = false
            };

            _chatservice.Raise(cs => cs.ChatMessage += null, args);
            _chatservice.Verify(sm => sm.SendMessageAsync(
                                    It.Is <string>(x => x.StartsWith("testusername's linked page title:"))),
                                Times.Once);
        }
Esempio n. 16
0
        public void ShouldIgnoreCooldownIfSendByModerator()
        {
            var sut = new FritzBot(_config.Object, _serviceProvider.Object);

            Task.WaitAll(sut.StartAsync(new CancellationToken()));

            var args = new ChatMessageEventArgs
            {
                Message     = "!FakeCooldownBasicCommand",
                UserName    = "******",
                IsModerator = true
            };

            _chatservice.Raise(cs => cs.ChatMessage += null, args);
            _chatservice.Raise(cs => cs.ChatMessage += null, args);
            _chatservice.Verify(sm => sm.SendWhisperAsync(
                                    It.IsAny <string>(),
                                    It.Is <string>(x => x.Equals(_basicCommandWithCooldown.ExecutedAnswer))),
                                Times.Exactly(2));
        }
        public void ShouldReturnHelpSkeetMessage()
        {
            var sut = new FritzBot(_config.Object, _serviceProvider.Object, _loggerFactory.Object);

            Task.WaitAll(sut.StartAsync(new CancellationToken()));
            var command = "skeet";
            var args    = new ChatMessageEventArgs
            {
                Message     = $"!help {command}",
                UserName    = "******",
                IsModerator = false,
                IsOwner     = false
            };

            var description = new SkeetCommand().Description;

            _chatservice.Raise(cs => cs.ChatMessage += null, args);

            _chatservice.Verify(sm => sm.SendMessageAsync(
                                    It.Is <string>(x => x.StartsWith($"{command}: {description}"))), Times.AtLeastOnce);
        }
Esempio n. 18
0
        public void ShouldInovkeOnlyFinalExtendedCommandIfBasicCommandPatternAlsoMatch()
        {
            var sut = new FritzBot(_config.Object, _serviceProvider.Object);

            Task.WaitAll(sut.StartAsync(new CancellationToken()));

            var args = new ChatMessageEventArgs
            {
                Message  = "!FakeExtendedCommandWithFinal",
                UserName = "******"
            };

            _chatservice.Raise(cs => cs.ChatMessage += null, args);
            _chatservice.Verify(sm => sm.SendWhisperAsync(
                                    It.IsAny <string>(),
                                    It.Is <string>(x => x.Equals(_finalBasicCommandWithExtendedCommandPattern.ExecutedAnswer))),
                                Times.Never);
            _chatservice.Verify(sm => sm.SendWhisperAsync(
                                    It.IsAny <string>(),
                                    It.Is <string>(x => x.Equals(_finalExtendedCommand.ExecutedAnswer))),
                                Times.Once);
        }
        public static void Execute(IServiceCollection services, IConfiguration configuration, Dictionary <Type, string[]> servicesRequiredConfiguration)
        {
            _Configuration = configuration;
            _ServicesRequiredConfiguration = servicesRequiredConfiguration;

            services.AddSingleton <RundownItemRepository>();
            services.AddSingleton <RundownRepository>();
            services.AddSingleton <IRundownService, RundownService>();
            services.Configure <FollowerGoalConfiguration>(configuration.GetSection("FollowerGoal"));
            services.Configure <FollowerCountConfiguration>(configuration.GetSection("FollowerCount"));
            services.Configure <Dictionary <string, SoundFxDefinition> >(configuration.GetSection("FritzBot:SoundFxCommands"));
            services.Configure <GitHubConfiguration>(configuration.GetSection("GitHub"));
            services.AddSingleton <FollowerClient>();
            services.AddAspNetFeatures();
            services.AddStreamingServices(configuration);

            services.AddSingleton <IConfigureOptions <SignalrTagHelperOptions>, ConfigureSignalrTagHelperOptions>();
            services.AddSingleton <SignalrTagHelperOptions>(cfg => cfg.GetService <IOptions <SignalrTagHelperOptions> >().Value);

            services.AddSingleton <IAttentionClient, AttentionHub>();
            services.AddSingleton <ObsHub>();

            // Add the SentimentSink
            //services.AddSingleton<Fritz.Chatbot.Commands.SentimentSink>();

            services.AddHostedService <FritzBot>();

            services.AddSingleton(new GitHubClient(new ProductHeaderValue("Fritz.StreamTools")));
            FritzBot.RegisterCommands(services);

            services.AddLazyCache();

            services.RegisterTwitchPubSub();

            RegisterConfiguredServices(services, configuration);
            RegisterGitHubServices(services, configuration);
        }