Compare commits

..

2 Commits

Author SHA1 Message Date
Derek 19211278c8 Add support for special twitch events 2021-05-02 08:33:00 -07:00
Derek efabe51fdd Fix twitch chat bugs
Shoutout to arezoa for the assist on air lol
2021-05-02 08:31:00 -07:00
2 changed files with 72 additions and 6 deletions

View File

@ -19,6 +19,21 @@ class STATES(Enum):
FAILURE = auto() FAILURE = auto()
class EVENTS(Enum):
SUB = auto()
RESUB = auto()
SUBGIFT = auto()
ANONSUBGIFT = auto()
SUBMYSTERYGIFT = auto()
GIFTPAIDUPGRADE = auto()
REWARDGIFT = auto()
ANONGIFTPAIDUPGRADE = auto()
RAID = auto()
UNRAID = auto()
RITUAL = auto()
BITSBADGETIER = auto()
class NonBlockingWebsocket(Process): class NonBlockingWebsocket(Process):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -107,6 +122,9 @@ class Process(ChatProcess):
if cmd == 'PRIVMSG': if cmd == 'PRIVMSG':
normalized_message = Process.normalize_message(hostmask, tags, args) normalized_message = Process.normalize_message(hostmask, tags, args)
self._message_queue.put(normalized_message) self._message_queue.put(normalized_message)
elif cmd == 'USERNOTICE':
normalized_message = Process.normalize_event(hostmask, tags, args)
self._message_queue.put(normalized_message)
elif cmd == 'PING': elif cmd == 'PING':
self._ws.send(f"PONG {' '.join(args)}") self._ws.send(f"PONG {' '.join(args)}")
else: else:
@ -119,12 +137,25 @@ class Process(ChatProcess):
return None return None
@classmethod @classmethod
def normalize_message(cls, hostmask, tags, args): def parse_badges(self, tags):
badges = [badge.split('/')[0] for badge in tags.get('badges', '').split(',')] if isinstance(tags.get('badges'), str):
return [badge.split('/')[0] for badge in tags['badges'].split(',')]
else:
return []
@classmethod
def parse_message(self, args):
# First arg is the room (which we dont care about, since it should only ever be #user) # First arg is the room (which we dont care about, since it should only ever be #user)
# First character of the second arg is the data delimiter (:) # First character of the second arg is the data delimiter (:)
message = ' '.join(args[1:])[1:] return ' '.join(args[1:])[1:]
@classmethod
def normalize_message(cls, hostmask, tags, args):
badges = Process.parse_badges(tags)
message = Process.parse_message(args)
monitization = tags.get('bits')
if 'broadcaster' in badges: if 'broadcaster' in badges:
author_type = AUTHOR_TYPES.OWNER author_type = AUTHOR_TYPES.OWNER
@ -135,5 +166,32 @@ class Process(ChatProcess):
else: else:
author_type = AUTHOR_TYPES.USER author_type = AUTHOR_TYPES.USER
return Message(message, tags.get('display-name', hostmask[0]), tags.get('id'), author_type, author_color = tags['color'] if isinstance(tags.get('color'), str) else None
author_color=tags.get('color'))
return Message(message, tags.get('display-name', hostmask[0]), tags.get('user-id'), author_type,
author_color=author_color, monitization=monitization)
@classmethod
def normalize_event(cls, hostmask, tags, args):
badges = Process.parse_badges(tags)
user_message = Process.parse_message(args)
message = f"*{tags['system-msg'].strip()}*"
if user_message != '':
message += f" - {user_message}"
if 'broadcaster' in badges:
author_type = AUTHOR_TYPES.OWNER
elif any(target_badge in badges for target_badge in ['admin', 'moderator', 'global_mod']):
author_type = AUTHOR_TYPES.MODERATOR
elif 'subscriber' in badges:
author_type = AUTHOR_TYPES.PATRON
else:
author_type = AUTHOR_TYPES.USER
author_color = tags['color'] if isinstance(tags.get('color'), str) and tags['color'] != '' else None
event = EVENTS.__members__[tags['msg-id'].upper()]
return Message(message, tags.get('display-name', hostmask[0]), tags.get('user-id'), author_type,
author_color=author_color, for_event=event)

View File

@ -12,7 +12,7 @@ class AUTHOR_TYPES(Enum):
class Message: class Message:
def __init__(self, text, author_name, author_id, author_type, def __init__(self, text, author_name, author_id, author_type,
author_color=None, monitization=None): author_color=None, monitization=None, for_event=None):
self._text = text self._text = text
self._author_name = author_name self._author_name = author_name
self._author_id = author_id self._author_id = author_id
@ -21,6 +21,7 @@ class Message:
self._author_type = author_type self._author_type = author_type
self._author_color = author_color self._author_color = author_color
self._monitization = monitization self._monitization = monitization
self._for_event = for_event
@property @property
def text(self): def text(self):
@ -45,3 +46,10 @@ class Message:
@property @property
def monitization(self): def monitization(self):
return self._monitization return self._monitization
@property
def for_event(self):
return self._for_event
def __repr__(self):
return f"{self._author_name} - \"{self._text}\" : author_type = {self._author_type}, monitization = {self._monitization}, for_event = {self._for_event}"