From 807927c3297d5843b042984eaaa9b4904a9ec150 Mon Sep 17 00:00:00 2001 From: Sojournophile Date: Tue, 4 May 2021 22:23:32 +1000 Subject: [PATCH 1/6] switch deprecated user-types to badges; add is_sub Removes the now-deprecated user-types/tag system in favour of twitch's new "badges". Adds is_sub to determine if the speaking user is a subscriber --- pytwitchchat/py_twitch_chat.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pytwitchchat/py_twitch_chat.py b/pytwitchchat/py_twitch_chat.py index 2111bc1..5465319 100644 --- a/pytwitchchat/py_twitch_chat.py +++ b/pytwitchchat/py_twitch_chat.py @@ -27,8 +27,16 @@ def __get_user(self, line): return user def __is_mod(self, line): - tags = line.split(":", 2)[0] - return True if "user-type=mod" in tags else False + badges = line + badges = tags.split(";", -1) + if "broadcaster" in badges: + return True + if "subscriber" in badges: + self.is_sub = True + if "mod" in badges: + return True + else: + return False def send_message(self, message): messageTemp = "PRIVMSG #" + self.__CHANNEL + " :" + message From f7c5dab238c5541bb67074df5cab9e96a860b071 Mon Sep 17 00:00:00 2001 From: Sojournophile Date: Tue, 4 May 2021 22:24:06 +1000 Subject: [PATCH 2/6] Update py_twitch_chat.py --- pytwitchchat/py_twitch_chat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytwitchchat/py_twitch_chat.py b/pytwitchchat/py_twitch_chat.py index 5465319..f506d5d 100644 --- a/pytwitchchat/py_twitch_chat.py +++ b/pytwitchchat/py_twitch_chat.py @@ -73,7 +73,7 @@ def run(self): user = self.__get_user(line) is_mod = self.__is_mod(line) if self.__HANDLE_METHOD: - self.__HANDLE_METHOD(message, user, is_mod) + self.__HANDLE_METHOD(message, user, is_mod, is_sub) elif "PING" in line: message = "PONG tmi.twitch.tv\r\n".encode() self.__IRC.send(message) From c456bfe9e87c95694a1cff24cec7b4d9cba35900 Mon Sep 17 00:00:00 2001 From: Sojournophile Date: Tue, 4 May 2021 22:29:32 +1000 Subject: [PATCH 3/6] add missing index --- pytwitchchat/py_twitch_chat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytwitchchat/py_twitch_chat.py b/pytwitchchat/py_twitch_chat.py index f506d5d..8854c6a 100644 --- a/pytwitchchat/py_twitch_chat.py +++ b/pytwitchchat/py_twitch_chat.py @@ -28,7 +28,7 @@ def __get_user(self, line): def __is_mod(self, line): badges = line - badges = tags.split(";", -1) + badges = tags.split(";", -1)[1] if "broadcaster" in badges: return True if "subscriber" in badges: From 79eb131830661a4b7841db400b40d36174355ad4 Mon Sep 17 00:00:00 2001 From: Sojournophile Date: Tue, 4 May 2021 22:51:31 +1000 Subject: [PATCH 4/6] fix if_sub --- pytwitchchat/py_twitch_chat.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pytwitchchat/py_twitch_chat.py b/pytwitchchat/py_twitch_chat.py index 8854c6a..6d3f93a 100644 --- a/pytwitchchat/py_twitch_chat.py +++ b/pytwitchchat/py_twitch_chat.py @@ -72,6 +72,7 @@ def run(self): message = self.__get_message(line) user = self.__get_user(line) is_mod = self.__is_mod(line) + if_sub = false if self.__HANDLE_METHOD: self.__HANDLE_METHOD(message, user, is_mod, is_sub) elif "PING" in line: From 301d3e099d908e0b386c068229b04bb59de8f13a Mon Sep 17 00:00:00 2001 From: Sojournophile Date: Wed, 5 May 2021 00:09:48 +1000 Subject: [PATCH 5/6] refactoring, fixed a typo --- pytwitchchat/py_twitch_chat.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pytwitchchat/py_twitch_chat.py b/pytwitchchat/py_twitch_chat.py index 6d3f93a..3fbbd1b 100644 --- a/pytwitchchat/py_twitch_chat.py +++ b/pytwitchchat/py_twitch_chat.py @@ -1,4 +1,5 @@ import socket +import time class TwitchChatClient: @@ -26,13 +27,20 @@ def __get_user(self, line): user = info.split("!", 1)[0] return user + def __is_sub(self, line): + badges = line + badges = badges.split(";", -1)[1] + if "subscriber" in badges: + return True + else: + return False + + def __is_mod(self, line): badges = line - badges = tags.split(";", -1)[1] + badges = badges.split(";", -1)[1] if "broadcaster" in badges: return True - if "subscriber" in badges: - self.is_sub = True if "mod" in badges: return True else: @@ -59,7 +67,7 @@ def connect(self): for line in readbuffer_join.split("\n")[0:-1]: if ("End of /NAMES list" in line): connecting = False - print("Connected!") + print("\nConnected!\n" + str(time.time()) + "\n") def run(self): while True: @@ -72,7 +80,7 @@ def run(self): message = self.__get_message(line) user = self.__get_user(line) is_mod = self.__is_mod(line) - if_sub = false + is_sub = self.__is_sub(line) if self.__HANDLE_METHOD: self.__HANDLE_METHOD(message, user, is_mod, is_sub) elif "PING" in line: From 4e71a8b3b818674435ad0ce1ccfe6cb3a65d3efa Mon Sep 17 00:00:00 2001 From: Sojournophile Date: Tue, 25 May 2021 13:03:08 +1000 Subject: [PATCH 6/6] is_vip, raiding, hosting, nightbot ads --- pytwitchchat/py_twitch_chat.py | 56 +++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/pytwitchchat/py_twitch_chat.py b/pytwitchchat/py_twitch_chat.py index 3fbbd1b..03297eb 100644 --- a/pytwitchchat/py_twitch_chat.py +++ b/pytwitchchat/py_twitch_chat.py @@ -3,14 +3,16 @@ class TwitchChatClient: - def __init__(self, password, username, channel, handle_method=None): + def __init__(self, password, username, channel, port, handle_method=None): self.__SERVER = "irc.twitch.tv" - self.__PORT = 6667 + self.__PORT = port self.__PASSWORD = password # This needs to be an OAuth token self.__USERNAME = username # This needs to be in lowercase self.__CHANNEL = channel # This needs to be in lowercase self.__IRC = socket.socket() self.__HANDLE_METHOD = handle_method + self.is_hosting = False + self.no_ads = False def __is_user_message(self, line): try: @@ -30,11 +32,20 @@ def __get_user(self, line): def __is_sub(self, line): badges = line badges = badges.split(";", -1)[1] - if "subscriber" in badges: + if "subscriber" in badges or "founder" in badges: + print("is sub") return True else: return False + def __is_vip(selfself, line): + badges = line + badges = badges.split(";", -1)[1] + print(str(badges)) + if "vip" in badges: + return True + else: + return False def __is_mod(self, line): badges = line @@ -50,6 +61,42 @@ def send_message(self, message): messageTemp = "PRIVMSG #" + self.__CHANNEL + " :" + message self.__IRC.send((messageTemp + "\n").encode()) + def send_whisper(self, user, message): + messageTemp = "PRIVMSG #" + self.__CHANNEL + " :.w " + user + " " + message + self.__IRC.send((messageTemp + "\n").encode()) + + def host_channel(self, target): + messageTemp = "PRIVMSG #" + self.__CHANNEL + " :.host " + str(target) + self.__IRC.send((messageTemp + "\n").encode()) + self.is_hosting = True + + def raid_channel(self, target): + messageTemp = "PRIVMSG #" + self.__CHANNEL + " :.raid " + str(target) + self.__IRC.send((messageTemp + "\n").encode()) + self.is_hosting = True + + def disable_ads(self): + self.no_ads = True + + def enable_ads(self): + self.no_ads = False + + def stop_host_channel(self): + messageTemp = "PRIVMSG #" + self.__CHANNEL + " :.unhost" + self.__IRC.send((messageTemp + "\n").encode()) + self.is_hosting = False + + def stop_raid_channel(self): + messageTemp = "PRIVMSG #" + self.__CHANNEL + " :.unraid" + self.__IRC.send((messageTemp + "\n").encode()) + self.is_hosting = False + + def run_commercial(self, length): + if self.no_ads is False: + print("Trying to run an ad") + messageTemp = "PRIVMSG #" + self.__CHANNEL + " :!commercial " + str(length) + " silent" + self.__IRC.send((messageTemp + "\n").encode()) + def connect(self): print("Connecting...") connecting = True @@ -81,8 +128,9 @@ def run(self): user = self.__get_user(line) is_mod = self.__is_mod(line) is_sub = self.__is_sub(line) + is_vip = self.__is_vip(line) if self.__HANDLE_METHOD: - self.__HANDLE_METHOD(message, user, is_mod, is_sub) + self.__HANDLE_METHOD(message, user, is_mod, is_sub, is_vip) elif "PING" in line: message = "PONG tmi.twitch.tv\r\n".encode() self.__IRC.send(message)