From 60bf9869fc5e443c1eb7fd9f3718eb25f408e7ed Mon Sep 17 00:00:00 2001 From: Arthur Dias Date: Wed, 24 May 2023 22:17:23 -0300 Subject: [PATCH] [ FIX ] Fix some identation & new Events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [ Add ] Added new events: • OnChatChannelLeave() - when user leaves channel; • OnChatLogin() - On user successfully login into Twitch IRC; [ Add ] New info about new events in README.md [ FIX ] Fix some identation on parse; --- BitABit/chat.cs | 38 ++++++++++++++++++++++++++++++++------ README.md | 23 ++++++++++++++++++++++- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/BitABit/chat.cs b/BitABit/chat.cs index 88ae6da..ca699e1 100644 --- a/BitABit/chat.cs +++ b/BitABit/chat.cs @@ -41,6 +41,8 @@ namespace BitABit { /// Delegate to deal with received chat messages (user). /// public delegate void OnChatMessageReceived(); + public delegate void OnChatLogin(); + public delegate void OnChatChannelLeave(); /// /// Chat Class. /// @@ -79,8 +81,12 @@ public class chat { public static List? _last_msgCache { get; set; } /// Event Receive Chat Message public event OnChatMessageReceived OnChatMessageReceived = new OnChatMessageReceived(fnull); + public event OnChatLogin OnChatLogin = new OnChatLogin(fnull); + public event OnChatChannelLeave OnChatChannelLeave = new OnChatChannelLeave(fnull); //Event Variables (loop) private static bool NOTIFY_MsgRecv = false; + private static bool NOTIFY_CTLogin = false; + private static bool NOTIFY_CTChnlLeave = false; /// Set to false to stop the loop (not recommended if you don't wan't to close the connection). private static bool IRCLoop; private static readonly string[] IRCCMDS = new string[] { "JOIN", "NICK", "NOTICE", "PART", "PASS", "PING", "PONG", "PRIVMSG", "CLEARCHAT", "CLEARMSG", "GLOBALUSERSTATE", "HOSTTARGET", "NOTICE", "RECONNECT", "ROOMSTATE", "USERNOTICE", "USERSTATE", "WHISPER", "CAP" }; @@ -341,9 +347,8 @@ private async Task ParseInput(string[] data) { } } } - } else //parsing emotes - if(splitted_tags[i].Contains("emotes=")) { + } else if(splitted_tags[i].Contains("emotes=")) { if(splitted_tags[i].Contains("/")) { //multi emote parsing string[] emotes_spplited = splitted_tags[i].Replace("emotes=", "").Split("/"); @@ -434,9 +439,8 @@ private async Task ParseInput(string[] data) { } } } - } else //parsing emote sets - if(splitted_tags[i].Contains("emote-sets=")) { + } else if(splitted_tags[i].Contains("emote-sets=")) { if(splitted_tags[i].Contains(",")) { //more than 1 string[] _emote_sets = splitted_tags[i].Replace("emote-sets=", "").Split(","); @@ -459,7 +463,7 @@ private async Task ParseInput(string[] data) { isEmoteSetNull = false; } } - } + } string[] property = splitted_tags[i].Split("="); switch(property[0]) { case "color": { @@ -566,6 +570,11 @@ private async Task ParseInput(string[] data) { } //--------------- ↑↑ Test Only [ will be removed ] ↑↑ --------------- switch(_cmd) { + case "PART": { + I_OnChatChannelLeave(); + _userful.SendConsoleLog("Twitch Chat", "OnTwitchIRCMessageReceived()", "Disconnect from channel " + _channel, DebugMessageType.INFO); + break; + } //normal IRC messages case "NOTICE": { //login failed - Don't need to be parsed cause don't come with CAPS (not that i know ;-;) @@ -652,6 +661,7 @@ private async Task ParseInput(string[] data) { login_count = 0; retry = 0; IsRetrying = false; + I_OnChatLogin(); //Connect user to channel if(_channel != null) { await JoinChannel(_channel); @@ -692,7 +702,9 @@ private async Task ParseInput(string[] data) { command = COMMAND, parameters = _param }); - I_OnChatMessageReceived(); + if(message_parsed != null) { + I_OnChatMessageReceived(); + } } /// /// Null returning method to avoid null exception on event creation. @@ -766,6 +778,14 @@ private void Callback_Exec() { OnChatMessageReceived.Invoke(); chat.NOTIFY_MsgRecv = false; } + if(chat.NOTIFY_CTChnlLeave == true) { + OnChatChannelLeave.Invoke(); + chat.NOTIFY_CTChnlLeave = false; + } + if(chat.NOTIFY_CTLogin == true) { + OnChatLogin.Invoke(); + chat.NOTIFY_CTLogin = false; + } Thread.Yield(); //avoid deadlocks } }); @@ -786,6 +806,12 @@ private void I_OnChatMessageReceived() { } }); } + private void I_OnChatChannelLeave() { + NOTIFY_CTChnlLeave = true; + } + private void I_OnChatLogin() { + NOTIFY_CTLogin = true; + } } /// /// Parsed Messages List diff --git a/README.md b/README.md index b500f48..f454f0c 100644 --- a/README.md +++ b/README.md @@ -115,8 +115,29 @@ _executes every time that a chat user sent a message_ Chat.OnChatMessageReceived += Chat_OnChatMessageReceived; private static void Chat_OnChatMessageReceived() { Console.WriteLine("New message received."); - /* Do your stuff ... */ + /* Do your stuff (exec after the message has been parsed)... */ } ``` +***Event ‘Chat_OnChatLogin’***
+_executes when user authenticate with success in IRC_ +```csharp +Chat.OnChatLogin += Chat_OnChatLogin; +private static void Chat_OnChatLogin() { + Console.WriteLine("Login has been made :)"); + /* Do your stuff (exec after user authenticate with success)... */ +} +``` + +***Event ‘OnChatChannelLeave’***
+_executes after user left the channel in any case_ +```csharp +Chat.OnChatChannelLeave += Chat_OnChatChannelLeave; +private static void Chat_OnChatChannelLeave() { + Console.WriteLine("User has left channel {0}", chat._channel); + /* Do your stuff (exec after user left the channel with success)... */ +} +``` + + **more comming soon...** \ No newline at end of file