Пример #1
0
        public async Task ProcessMessage(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                return;
            }
            logger.LogDebug($"ProcessMessage: '{message}'");

            var prefix     = string.Empty;
            var command    = string.Empty;
            var parameters = string.Empty;
            var authorName = string.Empty;

            if (message.Contains(":") && !message.StartsWith(":"))
            {
                message = message.Substring(message.IndexOf(':'));
            }

            if (message.StartsWith(":"))
            {
                var index = prefix.IndexOf(' ');
                if (index < 0)
                {
                    index = message.Length;
                }

                prefix = message.Substring(0, index);
                var parts = message.Split(" ");
                command = parts[1];
                if (parts.Length > 2)
                {
                    parameters = string.Join(' ', parts.Skip(2));
                }
            }
            else
            {
                var parts = message.Split(" ");
                command = parts[0];
                if (parts.Length > 1)
                {
                    parameters = string.Join(' ', parts.Skip(1));
                }
            }

            if (prefix.Contains("!"))
            {
                var index = prefix.IndexOf('!') - 1;
                if (index < 1)
                {
                    index = 1;
                }

                authorName = prefix.Substring(1, index);
            }

            if (command != "PRIVMSG")
            {
                return;
            }

            var displayMessage = parameters.Substring(parameters.IndexOf(':') + 1);
            var chatMessage    = new ChatMessage
            {
                Message = displayMessage,
                Date    = DateTime.UtcNow,
                Source  = ApiSource.Twitch,
                Author  = new Author
                {
                    Name   = authorName,
                    Source = ApiSource.Twitch
                },
                Type = "textMessageEvent"
            };
            var twitchUser = await twitchManager.FindUser(authorName);

            if (twitchUser != null)
            {
                chatMessage.Author.Source         = ApiSource.Twitch;
                chatMessage.Author.SourceAuthorId = twitchUser._id.ToString();
                chatMessage.Author.Name           = twitchUser.name;
            }

            try
            {
                await chatService.ProcessIncomingMessage(chatMessage);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.GetBaseException(), "Error processing message", null);
            }
        }