Exemple #1
0
        [InlineData("This is a normal message", null)]  //On a normal message the bot should not respond
        public async Task MessageTriggersExpectedOutput(string inputMessage, string expectedResponse)
        {
            string response = null;

            Mock <IMessage> socketMessageMock = new Mock <IMessage>();

            socketMessageMock
            .SetupGet(m => m.Content)
            .Returns(inputMessage);
            socketMessageMock
            .SetupGet(m => m.Source)
            .Returns(MessageSource.User);
            Mock <IMessageChannel> messageChannelMock = new Mock <IMessageChannel>();

            messageChannelMock
            .Setup(c => c.SendMessageAsync(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <Embed>(), It.IsAny <RequestOptions>()))
            .Callback <string, bool, Embed, RequestOptions>((msg, tts, embed, reqOptions) => response = msg)
            .Returns(() => Task.FromResult <IUserMessage>(default(SocketUserMessage)));
            socketMessageMock
            .Setup(x => x.Channel)
            .Returns(messageChannelMock.Object);

            NewMessageHandler messageHandler = new NewMessageHandler(new ServiceCollection().BuildServiceProvider(), GetPingCommandContainer(), Array.Empty <CommandFilter>());
            await messageHandler.HandleMessage(socketMessageMock.Object);

            messageChannelMock.Verify(c => c.SendMessageAsync(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <Embed>(), It.IsAny <RequestOptions>()), expectedResponse == null ? Times.Never() : Times.Once());
            Assert.Equal(expectedResponse, response);
        }
        public async Task ReceiveNewMessage(TripletDto message)
        {
            NewMessageHandler handler = NewMessageReceive;

            if (handler != null)
            {
                await handler(message);
            }
        }
Exemple #3
0
        public void Should_be_able_to_send_using_base_type()
        {
            var someMessageHandler = new NewMessageHandler();
            var eventAggregator    = new EventAggregator();

            eventAggregator.AddListener(someMessageHandler);

            eventAggregator.SendMessage <BaseMessage>(new NewMessage());

            someMessageHandler.NewMessageWasHandled.ShouldBeTrue();
        }
Exemple #4
0
        public void Message_inheritance_default_should_not_be_able_to_send_class_using_inheritance()
        {
            var newMessageHandler  = new NewMessageHandler();
            var baseMessageHandler = new BaseMessageHandler();
            var eventAggregator    = new EventAggregator();

            eventAggregator.AddListener(newMessageHandler);
            eventAggregator.AddListener(baseMessageHandler);

            eventAggregator.SendMessage <object>(new NewMessage());

            baseMessageHandler.BaseMessageWasHandled.ShouldBeFalse();
            newMessageHandler.NewMessageWasHandled.ShouldBeTrue();
        }
Exemple #5
0
        public void Should_be_able_to_send_using_object_type()
        {
            var newMessageHandler  = new NewMessageHandler();
            var baseMessageHandler = new BaseMessageHandler();
            var eventAggregator    = new EventAggregator();

            eventAggregator.AddListener(newMessageHandler);
            eventAggregator.AddListener(baseMessageHandler);

            eventAggregator.SendMessage <object>(new BaseMessage());

            baseMessageHandler.BaseMessageWasHandled.ShouldBeTrue();
            newMessageHandler.NewMessageWasHandled.ShouldBeFalse();
        }
Exemple #6
0
        public void Should_match_same_type_only()
        {
            var newMessageHandler  = new NewMessageHandler();
            var baseMessageHandler = new BaseMessageHandler();
            var eventAggregator    = new EventAggregator();

            eventAggregator.AddListener(newMessageHandler);
            eventAggregator.AddListener(baseMessageHandler);

            eventAggregator.SendMessage(new BaseMessage());

            baseMessageHandler.BaseMessageWasHandled.ShouldBeTrue();
            newMessageHandler.NewMessageWasHandled.ShouldBeFalse();
        }
Exemple #7
0
        public void Message_inheritance_enabled_should_be_able_to_send_class_using_inheritance()
        {
            var newMessageHandler  = new NewMessageHandler();
            var baseMessageHandler = new BaseMessageHandler();
            var eventAggregator    = new EventAggregator(new EventAggregator.Config {
                SupportMessageInheritance = true
            });

            eventAggregator.AddListener(newMessageHandler);
            eventAggregator.AddListener(baseMessageHandler);

            eventAggregator.SendMessage <object>(new NewMessage());

            baseMessageHandler.BaseMessageWasHandled.ShouldBeTrue();
            newMessageHandler.NewMessageWasHandled.ShouldBeTrue();
        }
Exemple #8
0
        public void Message_inheritance_default_should_not_be_able_to_listen_using_interface()
        {
            var newMessageHandler  = new NewMessageHandler();
            var baseMessageHandler = new BaseMessageHandler();
            var messageHandler     = new InterfaceHandler();
            var eventAggregator    = new EventAggregator();

            eventAggregator.AddListener(newMessageHandler);
            eventAggregator.AddListener(baseMessageHandler);
            eventAggregator.AddListener(messageHandler);

            eventAggregator.SendMessage(new BaseMessage());

            baseMessageHandler.BaseMessageWasHandled.ShouldBeTrue();
            newMessageHandler.NewMessageWasHandled.ShouldBeFalse();
            messageHandler.MessageWasHandled.ShouldBeFalse();
        }
Exemple #9
0
        public void Should_be_able_to_send_subclass_using_interface()
        {
            var newMessageHandler  = new NewMessageHandler();
            var baseMessageHandler = new BaseMessageHandler();
            var messageHandler     = new InterfaceHandler();
            var eventAggregator    = new EventAggregator();

            eventAggregator.AddListener(newMessageHandler);
            eventAggregator.AddListener(baseMessageHandler);
            eventAggregator.AddListener(messageHandler);

            eventAggregator.SendMessage <IMessage>(new BaseMessage());

            baseMessageHandler.BaseMessageWasHandled.ShouldBeTrue();
            newMessageHandler.NewMessageWasHandled.ShouldBeFalse();
            messageHandler.MessageWasHandled.ShouldBeTrue();
        }
        protected BotProgram()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            _logger.Info("Starting Bot v{0} on {1}({2})", Assembly.GetEntryAssembly().GetName().Version, Environment.MachineName, Environment.OSVersion.VersionString);

            //Configure Dependency Injection
            IServiceCollection serviceCollection = new ServiceCollection();

            ConfigureServices(serviceCollection);
            serviceCollection.AddSingleton(_discordClient);
            serviceCollection.TryAddSingleton(new BotConfiguration());  //Add default bot configuration if not already added
            IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            //Configure Command Container
            BotConfiguration  config    = serviceProvider.GetService <BotConfiguration>();
            ICommandContainer container = BuildCommandContainer();

            container.AddCommandHandler <HelpCommand>();
            container.AddCommandHandler <PrivateCommand>();
            container.AddCommandHandler <PingCommand>();
            if (config.EnableDebugCommandsForAdmins)
            {
                container.AddCommandHandler <InspectDscCommand>();
                container.AddCommandHandler <InfoCommand>();
            }
            else
            {
                _logger.Debug("Debug commands are disabled. Not adding them to command container");
            }
            _logger.Info("Added {0} command(s) to container", container.Count());
            serviceCollection.AddSingleton(container);
            serviceProvider = serviceCollection.BuildServiceProvider(); //Build ServiceProvider agein with ICommandContainer

            //Configure global filter
            IEnumerable <CommandFilter> globalFilter = ConfigureGlobalFilter().ToList();

            foreach (CommandFilter commandFilter in globalFilter)
            {
                _logger.Debug("Loaded global filter \"{0}\"", commandFilter.GetType().FullName);
            }
            _logger.Info("Added {0} global filter(s)", globalFilter.Count());

            //Configure Event Handler
            _newMessageHandler = new NewMessageHandler(serviceProvider, container, globalFilter);
        }