Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions twitch/chat/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,23 @@ def _message_handler(self, data: bytes) -> None:
# First messages are server connection messages,
# which should be handled by joining the chat room.
if not self.joined:
self.irc.activate_tags()
self.irc.join_channel(self.channel)
self.joined = True

text = data.decode("UTF-8").strip('\n\r')

if text.find('PRIVMSG') >= 0:
sender = text.split('!', 1)[0][1:]
if text[0] == '@':
block = text.split(' :')
tags = dict(t.split('=',1) for t in block[0][1:].split(';'))
sender = block[1].split('!', 1)[0]
else:
sender = text.split('!', 1)[0][1:]
tags = None
message = text.split('PRIVMSG', 1)[1].split(':', 1)[1]
self.on_next(
chat.Message(channel=self.channel, sender=sender, text=message, helix_api=self.helix, chat=self))
chat.Message(channel=self.channel, sender=sender, text=message, helix_api=self.helix, chat=self, tags=tags))

def send(self, message: str) -> None:
while not self.joined:
Expand Down
3 changes: 3 additions & 0 deletions twitch/chat/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def authenticate(self) -> None:
self.send_raw(f'PASS {self.password}')
self.send_raw(f'NICK {self.nickname}')

def activate_tags(self) -> None:
self.send_raw(f'CAP REQ :twitch.tv/tags')

def join_channel(self, channel: str) -> None:
channel = channel.lstrip('#')
self.channels.append(channel)
Expand Down
6 changes: 4 additions & 2 deletions twitch/chat/message.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Dict, Optional

from twitch.helix import User, Helix
from .chat import Chat
Expand All @@ -11,12 +11,14 @@ def __init__(self,
sender: str,
text: str,
helix_api: Optional[Helix] = None,
chat: Optional[Chat] = None):
chat: Optional[Chat] = None,
tags: Optional[Dict] = None):
self.channel: str = channel
self.sender: str = sender
self.text: str = text
self.helix: Optional[Helix] = helix_api
self.chat: Optional[Chat] = chat
self.tags: Optional[Dict] = tags

@property
def user(self) -> Optional[User]:
Expand Down