Skip to content

Commit

Permalink
[ FIX ] Fix some identation & new Events
Browse files Browse the repository at this point in the history
[ 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;
  • Loading branch information
ArTDsL committed May 25, 2023
1 parent af3d91d commit 60bf986
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
38 changes: 32 additions & 6 deletions BitABit/chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace BitABit {
/// Delegate to deal with received chat messages (user).
/// </summary>
public delegate void OnChatMessageReceived();
public delegate void OnChatLogin();
public delegate void OnChatChannelLeave();
/// <summary>
/// Chat Class.
/// </summary>
Expand Down Expand Up @@ -79,8 +81,12 @@ public class chat {
public static List<MESSAGE_PARSED>? _last_msgCache { get; set; }
/// <summary>Event Receive Chat Message</summary>
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;
/// <returns>Set to <c>false</c> to stop the loop (not recommended if you don't wan't to close the connection).</returns>
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" };
Expand Down Expand Up @@ -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("/");
Expand Down Expand Up @@ -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(",");
Expand All @@ -459,7 +463,7 @@ private async Task ParseInput(string[] data) {
isEmoteSetNull = false;
}
}
}
}
string[] property = splitted_tags[i].Split("=");
switch(property[0]) {
case "color": {
Expand Down Expand Up @@ -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 ;-;)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -692,7 +702,9 @@ private async Task ParseInput(string[] data) {
command = COMMAND,
parameters = _param
});
I_OnChatMessageReceived();
if(message_parsed != null) {
I_OnChatMessageReceived();
}
}
/// <summary>
/// Null returning method to avoid null exception on event creation.
Expand Down Expand Up @@ -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
}
});
Expand All @@ -786,6 +806,12 @@ private void I_OnChatMessageReceived() {
}
});
}
private void I_OnChatChannelLeave() {
NOTIFY_CTChnlLeave = true;
}
private void I_OnChatLogin() {
NOTIFY_CTLogin = true;
}
}
/// <summary>
/// Parsed Messages List
Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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’***<br>
_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’***<br>
_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...**

0 comments on commit 60bf986

Please sign in to comment.