示例#1
0
        private void ReadChatLine()
        {
            if (_twitchClient.Available <= 0)
            {
                return;
            }
            string message = _reader.ReadLine();

            if (message == null)
            {
                return;
            }
            if (message.Length == 0)
            {
                return;
            }

            if (message.Contains("PING"))
            {
                _writer.WriteLine($"PONG #{_twitchConnectConfig.ChannelName}");
                _writer.Flush();
                return;
            }

            if (message.Contains(COMMAND_MESSAGE))
            {
                if (message.Contains(CUSTOM_REWARD_TEXT))
                {
                    ReadChatReward(message);
                }
                else
                {
                    ReadChatMessage(message);
                }
            }
            else if (message.Contains(COMMAND_JOIN))
            {
                string username = _joinRegexp.Match(message).Groups[1].Value;
                TwitchUserManager.AddUser(username);
            }
            else if (message.Contains(COMMAND_PART))
            {
                string username = _partRegexp.Match(message).Groups[1].Value;
                TwitchUserManager.RemoveUser(username);
            }
        }
        private void ReadChatLine()
        {
            if (_twitchClient.Available <= 0)
            {
                return;
            }
            string          source    = _reader.ReadLine();
            TwitchInputLine inputLine = new TwitchInputLine(source, _commandPrefix);

            onTwitchInputLine?.Invoke(inputLine);

            switch (inputLine.Type)
            {
            case TwitchInputType.LOGIN:
                if (inputLine.IsValidLogin(_twitchConnectConfig))
                {
                    _isAuthenticated = true;
                    _onSuccess?.Invoke();
                    _onSuccess = null;
                    Debug.Log("<color=green>¡Success Twitch Connection!</color>");
                }
                else
                {
                    _onError?.Invoke(LOGIN_WRONG_USERNAME);
                    _onError = null;
                    Debug.Log("<color=red>¡Error Twitch Connection: Token is valid but it belongs to another user!</color>");
                }
                break;

            case TwitchInputType.NOTICE:
                string lineMessage = inputLine.Message;
                string userErrorMessage;
                string errorMessage;
                if (lineMessage.Contains(TwitchChatRegex.LOGIN_FAILED_MESSAGE))
                {
                    userErrorMessage = LOGIN_FAILED_MESSAGE;
                    errorMessage     = LOGIN_FAILED_MESSAGE;
                }
                else if (lineMessage.Contains(TwitchChatRegex.LOGIN_WRONG_REQUEST_MESSAGE))
                {
                    userErrorMessage = LOGIN_WRONG_REQUEST_MESSAGE;
                    errorMessage     = LOGIN_WRONG_REQUEST_MESSAGE;
                }
                else
                {
                    userErrorMessage = LOGIN_UNEXPECTED_ERROR_MESSAGE;
                    errorMessage     = lineMessage;
                }
                _onError?.Invoke(userErrorMessage);
                _onError = null;
                Debug.Log($"<color=red>Twitch connection error: {errorMessage}</color>");
                break;

            case TwitchInputType.PING:
                _writer.WriteLine(COMMAND_PONG);
                _writer.Flush();
                break;

            case TwitchInputType.MESSAGE_COMMAND:
            {
                TwitchChatMessageParser payload     = new TwitchChatMessageParser(inputLine);
                TwitchChatCommand       chatCommand = new TwitchChatCommand(payload.User, payload.Sent, payload.Bits, payload.Id);
                onChatCommandReceived?.Invoke(chatCommand);
            }
            break;

            case TwitchInputType.MESSAGE_CHAT:
            {
                TwitchChatMessageParser payload     = new TwitchChatMessageParser(inputLine);
                TwitchChatMessage       chatMessage = new TwitchChatMessage(payload.User, payload.Sent, payload.Bits, payload.Id);
                onChatMessageReceived?.Invoke(chatMessage);
            }
            break;

            case TwitchInputType.MESSAGE_REWARD:
            {
                TwitchChatRewardParser payload    = new TwitchChatRewardParser(inputLine);
                TwitchChatReward       chatReward = new TwitchChatReward(payload.User, payload.Sent, payload.Id);
                onChatRewardReceived?.Invoke(chatReward);
            }
            break;

            case TwitchInputType.JOIN: TwitchUserManager.AddUser(inputLine.UserName); break;

            case TwitchInputType.PART: TwitchUserManager.RemoveUser(inputLine.UserName); break;
            }
        }