private void OnReadLine(object sender, ReadLineEventArgs e) { // Hack to accomodate at least cyrillic characters, possibly more string decodedMessage = Encoding.UTF8.GetString(Encoding.Default.GetBytes(e.Line)); if (_logging) { Console.WriteLine(decodedMessage); } #region Chat Parsing // On Connected if (ChatParsing.detectConnected(decodedMessage)) { _connected = true; OnConnected?.Invoke(null, new OnConnectedArgs { Channel = _channel, Username = TwitchUsername }); return; } // On New Subscriber if (ChatParsing.detectNewSubscriber(decodedMessage, _channel)) { OnNewSubscriber?.Invoke(null, new OnNewSubscriberArgs { Subscriber = new NewSubscriber(decodedMessage), Channel = _channel }); return; } // On Command Received (PURPOSELY DROP THROUGH WITHOUT RETURN) if (ChatParsing.detectCommandReceived(decodedMessage, _channel, _channelEmotes, WillReplaceEmotes, _chatCommandIdentifiers)) { var chatMessage = new ChatMessage(decodedMessage, ref _channelEmotes, WillReplaceEmotes); string command = chatMessage.Message.Split(' ')?[0].Substring(1, chatMessage.Message.Split(' ')[0].Length - 1) ?? chatMessage.Message.Substring(1, chatMessage.Message.Length - 1); var argumentsAsList = chatMessage.Message.Split(' ')?.Where(arg => arg != chatMessage.Message[0] + command).ToList <string>() ?? new List <string>(); string argumentsAsString = chatMessage.Message.Replace(chatMessage.Message.Split(' ')?[0] +" ", "") ?? ""; OnChatCommandReceived?.Invoke(null, new OnChatCommandReceivedArgs { Command = command, ChatMessage = chatMessage, Channel = _channel, ArgumentsAsList = argumentsAsList, ArgumentsAsString = argumentsAsString }); // purposely drop through without return } // On Message Received if (ChatParsing.detectMessageReceived(decodedMessage, _channel)) { var chatMessage = new ChatMessage(decodedMessage, ref _channelEmotes, WillReplaceEmotes); _previousMessage = chatMessage; OnMessageReceived?.Invoke(null, new OnMessageReceivedArgs { ChatMessage = chatMessage }); return; } // On Viewer Joined if (ChatParsing.detectViewerJoined(decodedMessage, _channel)) { OnViewerJoined?.Invoke(null, new OnViewerJoinedArgs { Username = decodedMessage.Split('!')[1].Split('@')[0], Channel = _channel }); return; } // On Viewer Left if (ChatParsing.detectedViewerLeft(decodedMessage, _channel)) { OnViewerLeft?.Invoke(null, new OnViewerLeftArgs { Username = decodedMessage.Split(':')[1].Split('!')[0], Channel = _channel }); return; } // On Moderator Joined if (ChatParsing.detectedModeratorJoined(decodedMessage, _channel)) { OnModeratorJoined?.Invoke(null, new OnModeratorJoinedArgs { Username = decodedMessage.Split(' ')[4], Channel = _channel }); return; } // On Moderator Left if (ChatParsing.detectedModeatorLeft(decodedMessage, _channel)) { OnModeratorLeft?.Invoke(null, new OnModeratorLeftArgs { Username = decodedMessage.Split(' ')[4], Channel = _channel }); return; } // On Incorrect login if (ChatParsing.detectedIncorrectLogin(decodedMessage, _channel)) { _client.Disconnect(); OnIncorrectLogin?.Invoke(null, new OnIncorrectLoginArgs { Exception = new ErrorLoggingInException(decodedMessage, _credentials.TwitchUsername) }); return; } // On Host Left if (ChatParsing.detectedHostLeft(decodedMessage, _channel)) { OnHostLeft?.Invoke(null, null); return; } // On Channel State Changed if (ChatParsing.detectedChannelStateChanged(decodedMessage, _channel)) { OnChannelStateChanged?.Invoke(null, new OnChannelStateChangedArgs { ChannelState = new ChannelState(decodedMessage) }); return; } // On User State Changed if (ChatParsing.detectedUserStateChanged(decodedMessage, _channel)) { OnUserStateChanged?.Invoke(null, new OnUserStateChangedArgs { UserState = new UserState(decodedMessage) }); return; } // On ReSubscriber if (ChatParsing.detectedReSubscriber(decodedMessage, _channel)) { OnReSubscriber?.Invoke(null, new OnReSubscriberArgs { ReSubscriber = new ReSubscriber(decodedMessage) }); return; } // On PING received if (ChatParsing.detectedPing(decodedMessage) && !DisableAutoPong) { SendRaw("PONG :tmi.twitch.tv"); return; } // On Hosting Stopped if (ChatParsing.detectedHostingStopped(decodedMessage)) { int viewers; int.TryParse(decodedMessage.Split(' ')[4], out viewers); OnHostingStopped?.Invoke(null, new OnHostingStoppedArgs() { Viewers = viewers, HostingChannel = decodedMessage.Split(' ')[2].Remove(0, 1) }); return; } // On Hosting Started if (ChatParsing.detectedHostingStarted(decodedMessage)) { int viewers; int.TryParse(decodedMessage.Split(' ')[4], out viewers); OnHostingStarted?.Invoke(null, new OnHostingStartedArgs() { Viewers = viewers, HostingChannel = decodedMessage.Split(' ')[2].Remove(0, 1), TargetChannel = decodedMessage.Split(' ')[3].Remove(0, 1) }); return; } // On Existing Users Detected if (ChatParsing.detectedExistingUsers(decodedMessage, _credentials.TwitchUsername)) { var parsedUsers = decodedMessage.Replace($":{_credentials.TwitchUsername}.tmi.twitch.tv 353 {_credentials.TwitchUsername} = #{_channel} :", "").Split(' '); OnExistingUsersDetected?.Invoke(null, new OnExistingUsersDetectedArgs { Channel = _channel, ExistingUsers = decodedMessage.Replace($":{_credentials.TwitchUsername}.tmi.twitch.tv 353 {_credentials.TwitchUsername} = #{_channel} :", "").Split(' ').ToList <string>() }); return; } #endregion #region Whisper Parsing if (decodedMessage.Split(' ').Count() > 2 && (decodedMessage.Split(' ')[1] == "WHISPER" || decodedMessage.Split(' ')[2] == "WHISPER")) { // On Whisper Message Received if (WhisperParsing.detectedWhisperReceived(decodedMessage, _credentials.TwitchUsername)) { OnWhisperReceived?.Invoke(null, new OnWhisperReceivedArgs { WhisperMessage = new WhisperMessage(decodedMessage, _credentials.TwitchUsername) }); return; } // On Whisper Command Received if (WhisperParsing.detectedWhisperCommandReceived(decodedMessage, _credentials.TwitchUsername, _whisperCommandIdentifiers)) { var whisperMessage = new WhisperMessage(decodedMessage, _credentials.TwitchUsername); string command = whisperMessage.Message.Split(' ')?[0].Substring(1, whisperMessage.Message.Split(' ')[0].Length - 1) ?? whisperMessage.Message.Substring(1, whisperMessage.Message.Length - 1); var argumentsAsList = whisperMessage.Message.Split(' ')?.Where(arg => arg != whisperMessage.Message[0] + command).ToList <string>() ?? new List <string>(); string argumentsAsString = whisperMessage.Message.Replace(whisperMessage.Message.Split(' ')?[0] +" ", "") ?? ""; OnWhisperCommandReceived?.Invoke(null, new OnWhisperCommandReceivedArgs { Command = command, WhisperMessage = whisperMessage, ArgumentsAsList = argumentsAsList, ArgumentsAsString = argumentsAsString }); } } #endregion // Any other messages here if (_logging) { Console.WriteLine($"Unaccounted for: {decodedMessage}"); } }
private void OnReadLine(object sender, ReadLineEventArgs e) { if (_logging) { Console.WriteLine(e.Line); } if (e.Line.Split(':').Length > 2) { if (e.Line.Split(':')[2] == "You are in a maze of twisty passages, all alike.") { _connected = true; OnConnected?.Invoke(null, new OnConnectedArgs { Channel = _channel, Username = TwitchUsername }); } } if (e.Line.Contains($"#{_channel}")) { var splitter = Regex.Split(e.Line, $" #{_channel}"); var readType = splitter[0].Split(' ')[splitter[0].Split(' ').Length - 1]; switch (readType) { case "PRIVMSG": if (e.Line.Split('!')[0] == ":twitchnotify" && (e.Line.Contains("just subscribed!") || e.Line.Contains("subscribed for"))) { var subscriber = new Subscriber(e.Line); OnSubscriber?.Invoke(null, new OnSubscriberArgs { Subscriber = subscriber, Channel = _channel }); } else { var chatMessage = new ChatMessage(e.Line); _previousMessage = chatMessage; OnMessageReceived?.Invoke(null, new OnMessageReceivedArgs { ChatMessage = chatMessage }); if (_commandIdentifier != '\0' && chatMessage.Message[0] == _commandIdentifier) { string command; var argumentsAsString = ""; var argumentsAsList = new List <string>(); if (chatMessage.Message.Contains(" ")) { command = chatMessage.Message.Split(' ')[0].Substring(1, chatMessage.Message.Split(' ')[0].Length - 1); argumentsAsList.AddRange( chatMessage.Message.Split(' ').Where(arg => arg != _commandIdentifier + command)); argumentsAsString = chatMessage.Message.Replace(chatMessage.Message.Split(' ')[0] + " ", ""); } else { command = chatMessage.Message.Substring(1, chatMessage.Message.Length - 1); } OnCommandReceived?.Invoke(null, new OnCommandReceivedArgs { Command = command, ChatMessage = chatMessage, Channel = _channel, ArgumentsAsList = argumentsAsList, ArgumentsAsString = argumentsAsString }); } } break; case "JOIN": //:the_kraken_bot!the_kraken_bot@the_kraken_bot.tmi.twitch.tv JOIN #swiftyspiffy OnViewerJoined?.Invoke(null, new OnViewerJoinedArgs { Username = e.Line.Split('!')[1].Split('@')[0], Channel = _channel }); break; case "MODE": //:jtv MODE #swiftyspiffy +o swiftyspiffy if (e.Line.Split(' ').Length == 4) { OnModeratorJoined?.Invoke(null, new OnModeratorJoinedArgs { Username = e.Line.Split(' ')[4], Channel = _channel }); } else { if (_logging) { Console.WriteLine("FAILED PARSE: " + e.Line); } } break; case "NOTICE": if (e.Line.Contains("Error logging in")) { _client.Disconnect(); OnIncorrectLogin?.Invoke(null, new OnIncorrectLoginArgs { Exception = new ErrorLoggingInException(e.Line, _credentials.TwitchUsername) }); } if (e.Line.Contains("has gone offline")) { OnHostLeft?.Invoke(null, null); } break; case "ROOMSTATE": _state = new ChannelState(e.Line); OnChannelStateChanged?.Invoke(null, new OnChannelStateChangedArgs { ChannelState = _state }); break; case "USERSTATE": //@color=#8A2BE2;display-name=The_Kraken_Bot;emote-sets=0,5628;subscriber=0;turbo=0;user-type=mod :tmi.twitch.tv USERSTATE #swiftyspiffy var userState = new UserState(e.Line); OnUserStateChanged?.Invoke(null, new OnUserStateChangedArgs { UserState = userState }); break; default: if (_logging) { Console.WriteLine("Unaccounted for: {0}", e.Line); } break; } } else { //Special cases if (e.Line == ":tmi.twitch.tv NOTICE * :Error logging in") { _client.Disconnect(); OnIncorrectLogin?.Invoke(null, new OnIncorrectLoginArgs { Exception = new ErrorLoggingInException(e.Line, _credentials.TwitchUsername) }); } else { if (_logging) { Console.WriteLine("Not registered: " + e.Line); } } } }