Example #1
0
        public async void Identifies_Guitar()
        {
            // Arrange
            string outDescription = "";
            var    chatService    = _Mockery.Create <IChatService>();

            chatService
            .Setup(c => c.SendMessageAsync(It.IsAny <string>()))
            .Callback <string>(msg =>
            {
                outDescription = msg;
                Output.WriteLine(msg);
            })
            .Returns(Task.FromResult(true));

            var sut = new ImageDescriptorCommand(
                "testlocation",
                "testkey");

            // Act
            //var urlToTest = "https://media.discordapp.net/attachments/336580722653528075/550152641632534558/unknown.png?width=1020&height=574";
            await sut.Execute(chatService.Object, "test", url);

            // Assert
            Assert.Contains("guitar", outDescription);
        }
Example #2
0
        public async void Identifies_Guitar()
        {
            // Arrange
            string outDescription = "";
            var    chatService    = _Mockery.Create <IChatService>();

            chatService
            .Setup(c => c.SendMessageAsync(It.IsAny <string>()))
            .Callback <string>(msg =>
            {
                outDescription = msg;
                Output.WriteLine(msg);
            })
            .Returns(Task.FromResult(true));

            var sut = new ImageDescriptorCommand("testlocation", "testkey");

            // Act
            await sut.Execute(chatService.Object, "test", url);

            // Assert
            Assert.Contains("guitar", outDescription);
        }
Example #3
0
        private async Task Chat_ChatMessage(object sender, ChatMessageEventArgs e)
        {
            var          userKey = $"{e.ServiceName}:{e.UserName}";
            ChatUserInfo user;

            if (!_activeUsers.TryGetValue(userKey, out user))
            {
                user = new ChatUserInfo();
            }

            // message is empty OR message doesn't start with ! AND doesn't end with ?
            if (e.Message.EndsWith("?"))
            {
                _logger.LogInformation($"Handling question: \"{e.Message}\" from {e.UserName} on {e.ServiceName}");


                if (CommandsTooFast("qna"))
                {
                    return;
                }
                await HandleAzureQuestion(e.Message, e.UserName, sender as IChatService);

                return;
            }

            // Check for image processing
            var imageCheckPattern = @"http(s)?:?(\/\/[^""']*\.(?:png|jpg|jpeg|gif))";
            var r = new Regex(imageCheckPattern, RegexOptions.IgnoreCase);

            // Match the regular expression pattern against a text string.
            var imageCheck = r.Match(e.Message);

            if (imageCheck.Captures.Count > 0)
            {
                //cal the new comand
                var imageDescCommand = new ImageDescriptorCommand(_config);
                imageDescCommand.ChatService = sender as IChatService;
                await imageDescCommand.Execute(e.UserName, imageCheck.Captures[0].Value);

                return;
            }

            if (string.IsNullOrEmpty(e.Message) || (e.Message[0] != COMMAND_PREFIX & !e.Message.EndsWith("?")))
            {
                return;                                                 // e.Message.StartsWith(...) did not work for some reason ?!?
            }
            var segments = e.Message.Substring(1).Split(' ', StringSplitOptions.RemoveEmptyEntries);

            if (segments.Length == 0)
            {
                return;
            }

            var chatService = sender as IChatService;

            Debug.Assert(chatService != null);
            if (!chatService.IsAuthenticated)
            {
                return;
            }


            // Ignore if the normal user is sending commands to fast
            if (CommandsTooFast(segments[0]))
            {
                return;
            }

            _logger.LogInformation($"!{segments[0]} from {e.UserName} on {e.ServiceName}");

            // Handle commands
            ICommand cmd = null;

            if (_CommandRegistry.TryGetValue(segments[0].ToLowerInvariant(), out cmd))
            {
                cmd.ChatService = chatService;
                await cmd.Execute(e.UserName, e.Message);
            }
            else
            {
                await chatService.SendWhisperAsync(e.UserName, "Unknown command.  Try !help for a list of available commands");

                return;
            }

            // Remember last command time
            user.LastCommandTime = DateTime.UtcNow;
            _activeUsers.AddOrUpdate(userKey, user, (k, v) => user);

            bool CommandsTooFast(string namedCommand)
            {
                if (!e.IsModerator && !e.IsOwner)
                {
                    if (DateTime.UtcNow - user.LastCommandTime < CooldownTime)
                    {
                        _logger.LogWarning($"Ignoring command {namedCommand} from {e.UserName} on {e.ServiceName}. Cooldown active");
                        return(true);
                    }
                }

                return(false);
            }
        }