public TwitchChatRewardParser(TwitchInputLine command) { BadgesText = TwitchChatRegex.RewardRegex.Match(command.Message).Groups[1].Value; Id = TwitchChatRegex.RewardRegex.Match(command.Message).Groups[2].Value; string displayName = TwitchChatRegex.RewardRegex.Match(command.Message).Groups[3].Value; string idUser = TwitchChatRegex.RewardRegex.Match(command.Message).Groups[4].Value; string username = TwitchChatRegex.RewardRegex.Match(command.Message).Groups[5].Value; Sent = TwitchChatRegex.RewardRegex.Match(command.Message).Groups[6].Value; Badges = TwitchChatRegex.BuildBadges(BadgesText); User = TwitchUserManager.AddUser(username); User.SetData(idUser, displayName, Badges); }
private void ReadChatReward(string message) { string customRewardId = _rewardRegexp.Match(message).Groups[1].Value; string displayName = _rewardRegexp.Match(message).Groups[2].Value; bool isSub = _rewardRegexp.Match(message).Groups[3].Value == "1"; string idUser = _rewardRegexp.Match(message).Groups[4].Value; string username = _rewardRegexp.Match(message).Groups[5].Value; string messageSent = _rewardRegexp.Match(message).Groups[6].Value; TwitchUser twitchUser = TwitchUserManager.AddUser(username); twitchUser.SetData(idUser, displayName, isSub); TwitchChatReward chatReward = new TwitchChatReward(twitchUser, messageSent, customRewardId); onChatRewardReceived?.Invoke(chatReward); }
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 ReadChatMessage(string message) { string displayName = _messageRegexp.Match(message).Groups[1].Value; bool isSub = _messageRegexp.Match(message).Groups[2].Value == "1"; string idUser = _messageRegexp.Match(message).Groups[3].Value; string username = _messageRegexp.Match(message).Groups[4].Value; string messageSent = _messageRegexp.Match(message).Groups[5].Value; int bits = 0; TwitchUser twitchUser = TwitchUserManager.AddUser(username); twitchUser.SetData(idUser, displayName, isSub); if (messageSent.Length == 0) { return; } MatchCollection matches = cheerRegexp.Matches(messageSent); foreach (Match match in matches) { if (match.Groups.Count != 2) { continue; // First group is 'cheerXX', second group is XX. } string value = match.Groups[1].Value; if (!Int32.TryParse(value, out int bitsAmount)) { continue; } bits += bitsAmount; } if (messageSent[0] == commandPrefix[0]) { TwitchChatCommand chatCommand = new TwitchChatCommand(twitchUser, messageSent, bits); onChatCommandReceived?.Invoke(chatCommand); } else { TwitchChatMessage chatMessage = new TwitchChatMessage(twitchUser, messageSent, bits); onChatMessageReceived?.Invoke(chatMessage); } }
public TwitchChatMessageParser(TwitchInputLine command) { Bits = 0; Id = TwitchChatRegex.IdMessageRegex.Match(command.Message).Groups[1].Value; string badgesText = TwitchChatRegex.MessageRegex.Match(command.Message).Groups[1].Value; string displayName = TwitchChatRegex.MessageRegex.Match(command.Message).Groups[2].Value; string idUser = TwitchChatRegex.MessageRegex.Match(command.Message).Groups[3].Value; string username = TwitchChatRegex.MessageRegex.Match(command.Message).Groups[4].Value; Sent = TwitchChatRegex.MessageRegex.Match(command.Message).Groups[5].Value; Badges = TwitchChatRegex.BuildBadges(badgesText); User = TwitchUserManager.AddUser(username); User.SetData(idUser, displayName, Badges); if (Sent.Length == 0) { return; } MatchCollection matches = TwitchChatRegex.CheerRegex.Matches(Sent); foreach (Match match in matches) { if (match.Groups.Count != 2) { continue; // First group is 'cheerXX', second group is XX. } string value = match.Groups[1].Value; if (!int.TryParse(value, out int bitsAmount)) { continue; } Bits += bitsAmount; } }
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; } }