Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit a76a59a

Browse files
author
DirectiveAthena
committed
Fix: correct early message parsing
1 parent 52019a6 commit a76a59a

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

src/AthenaTwitchBot/functions/twitch_message_constructors.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def _find_bot_only(content:list[str],message:str, bot_name:str) -> TwitchMessage
2626
return TwitchMessageOnlyForBot(text=message)
2727
elif content[0] == f":{bot_name}!{bot_name}@{bot_name}.tmi.twitch.tv":
2828
return TwitchMessageOnlyForBot(text=message)
29+
elif content[0] == f":{bot_name}.tmi.twitch.tv":
30+
return TwitchMessageOnlyForBot(text=message)
31+
elif not message: # content is empty
32+
return TwitchMessageOnlyForBot()
2933
return False
3034

3135
TAG_MAPPING:dict[str:Callable] = {
@@ -45,15 +49,14 @@ def _find_bot_only(content:list[str],message:str, bot_name:str) -> TwitchMessage
4549
"turbo": lambda tm, tag_value: setattr(tm, "turbo", bool(tag_value)),
4650
"user-id": lambda tm, tag_value: setattr(tm, "user_id", int(tag_value)),
4751
"user-type": lambda tm, tag_value: setattr(tm, "user-type", tag_value),
48-
4952
}
5053

5154
# ----------------------------------------------------------------------------------------------------------------------
5255
# - Code -
5356
# ----------------------------------------------------------------------------------------------------------------------
5457
def twitch_message_constructor_tags(message_bytes:bytearray, bot_name:str) -> TwitchMessage:
5558
print(message_bytes)
56-
message = message_bytes.decode("UTF_8").replace("\r\n", "")
59+
message = message_bytes.decode("UTF_8")
5760
content = message.split(" ")
5861

5962
# Certain twitch sent messages have a different consistency than a regular user sent message

src/AthenaTwitchBot/models/twitch_bot_protocol.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,30 @@ def connection_made(self, transport: asyncio.transports.Transport) -> None:
4545
self.transport.write(messages.request_tags)
4646

4747
def data_received(self, data: bytearray) -> None:
48-
match (twitch_message := self.message_constructor(data, bot_name=self.bot.nickname)):
49-
# Keepalive messages : https://dev.twitch.tv/docs/irc#keepalive-messages
50-
case TwitchMessagePing():
51-
print(ForeNest.ForestGreen("PINGED BY TWITCH"))
52-
self.transport.write(pong_message := messages.pong(message=twitch_message.text))
53-
print(pong_message)
48+
for message in data.split(b"\r\n"):
49+
match (twitch_message := self.message_constructor(message, bot_name=self.bot.nickname)):
50+
# Keepalive messages : https://dev.twitch.tv/docs/irc#keepalive-messages
51+
case TwitchMessagePing():
52+
print(ForeNest.ForestGreen("PINGED BY TWITCH"))
53+
self.transport.write(pong_message := messages.pong(message=twitch_message.text))
54+
print(pong_message)
5455

55-
# catch a message which starts with a command:
56-
case TwitchMessage(message=[_,_,_,str(user_message),*user_message_other]) if user_message.startswith(f":{self.bot.prefix}"):
57-
user_message:str
58-
print(ForeNest.ForestGreen("COMMAND CAUGHT"))
59-
try:
60-
user_cmd = user_message.replace(f":{self.bot.prefix}", "")
61-
result = self.bot.commands[user_cmd](self=self.bot,transport=self.transport)
62-
print(result)
63-
except KeyError:
64-
pass
56+
# catch a message which starts with a command:
57+
case TwitchMessage(message=[_,_,_,str(user_message),*user_message_other]) if user_message.startswith(f":{self.bot.prefix}"):
58+
user_message:str
59+
print(ForeNest.ForestGreen("COMMAND CAUGHT"))
60+
try:
61+
user_cmd = user_message.replace(f":{self.bot.prefix}", "")
62+
result = self.bot.commands[user_cmd](self=self.bot,transport=self.transport)
63+
print(result)
64+
except KeyError:
65+
pass
6566

66-
# catch a message which has a command within it:
67-
case TwitchMessage(message=[_,_,_,*messages_parts]):
68-
for message in messages_parts:
69-
if message.startswith(self.bot.prefix):
70-
print(ForeNest.ForestGreen("COMMAND CAUGHT"))
67+
# catch a message which has a command within it:
68+
case TwitchMessage(message=[_,_,_,*messages_parts]):
69+
for message in messages_parts:
70+
if message.startswith(self.bot.prefix):
71+
print(ForeNest.ForestGreen("COMMAND CAUGHT"))
7172

7273
def connection_lost(self, exc: Exception | None) -> None:
7374
self.main_loop.stop()

src/AthenaTwitchBot/models/twitch_message.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class TwitchMessage:
3535
tmi_sent_ts:datetime=field(default_factory=datetime.now)
3636
turbo:bool=False
3737
user_id:int=0
38+
emotes:str=""
39+
flags:str=""
3840
# ----------------------------------------------------------------------------------------------------------------------
3941
# - Special message types -
4042
# ----------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)