Skip to content

Commit

Permalink
reworked typing on functions and commands
Browse files Browse the repository at this point in the history
  • Loading branch information
The-CJ committed Jan 14, 2021
1 parent 01cae8d commit 9234abe
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 120 deletions.
8 changes: 4 additions & 4 deletions twitch_irc/Classes/channel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, NewType, TYPE_CHECKING, Optional
from typing import Any, Dict, List, NewType, TYPE_CHECKING, Optional, Union
if TYPE_CHECKING:
from .client import Client as TwitchClient

Expand Down Expand Up @@ -41,7 +41,7 @@ def __init__(self, raw:Optional[str], emergency:bool=False, Msg:Optional[Message
self._subs_only:Optional[bool] = None
self._name:Optional[str] = None

self._viewers:Dict[UserName, User] = UserStore()
self._viewers:Dict[Union[UserName, str], User] = UserStore()
self._host_target:Optional[str] = None
self._me:Optional[UserState] = None

Expand Down Expand Up @@ -216,11 +216,11 @@ def subs_only(self) -> bool:
return bool(self._subs_only)

@property
def users(self) -> Dict[UserName, User]:
def users(self) -> Dict[Union[UserName, str], User]:
return self._viewers

@property
def viewers(self) -> Dict[UserName, User]:
def viewers(self) -> Dict[Union[UserName, str], User]:
return self._viewers

@property
Expand Down
4 changes: 2 additions & 2 deletions twitch_irc/Classes/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def __init__(self, Loop:Optional[asyncio.AbstractEventLoop]=None, **kwargs):
self.ConnectionReader:Optional[asyncio.StreamReader] = None
self.ConnectionWriter:Optional[asyncio.StreamWriter] = None

self.channels:Dict[ChannelName, Channel] = ChannelStore()
self.users:Dict[UserName, User] = UserStore()
self.channels:Dict[Union[ChannelName,str], Channel] = ChannelStore()
self.users:Dict[Union[ChannelName,str], User] = UserStore()

def stop(self, *_, **__) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion twitch_irc/Classes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __repr__(self):
def __str__(self):
return self.name or ""

def __init__(self, raw:str, emergency:bool=False, Msg:Optional[Message]=None):
def __init__(self, raw:Optional[str], emergency:bool=False, Msg:Optional[Message]=None):
self._name:Optional[str] = None
self._display_name:Optional[str] = None # *
self._user_id:Optional[str] = None # *
Expand Down
8 changes: 4 additions & 4 deletions twitch_irc/Utils/cmd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from ..Classes.client import Client
from ..Classes.client import Client

import logging
Log:logging.Logger = logging.getLogger("twitch_irc")
Expand All @@ -11,20 +11,20 @@ async def sendPong(cls:"Client"):
- this should never be called by custom code
"""
Log.debug("Sending: PONG")
await cls.sendContent( "PONG :tmi.twitch.tv\r\n", ignore_limit=True )
await cls.sendContent("PONG :tmi.twitch.tv\r\n", ignore_limit=True)

async def sendNick(cls:"Client"):
"""
Sends a NICK together with the nickname to twitch, it also ignores the request limit
- this should never be called by custom code
"""
Log.debug("Sending: NICK: " + cls.nickname[:10])
await cls.sendContent( f"NICK {cls.nickname}\r\n", ignore_limit=True )
await cls.sendContent(f"NICK {cls.nickname}\r\n", ignore_limit=True)

async def sendPass(cls:"Client"):
"""
Sends a PASS together with the token to twitch, it also ignores the request limit
- this should never be called by custom code
"""
Log.debug("Sending: PASS: *****")
await cls.sendContent( f"PASS {cls.token}\r\n", ignore_limit=True )
await cls.sendContent(f"PASS {cls.token}\r\n", ignore_limit=True)
68 changes: 34 additions & 34 deletions twitch_irc/Utils/detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,84 +21,84 @@
ReUserNotice, ReNotice, ReHostTarget
)

async def garbageDetector(cls:"Client", payload:str) -> bool:
async def garbageDetector(_cls:"Client", payload:str) -> bool:
"""
This detector is suppost to catch all known patterns that are also known as trash.
This detector is suppose to catch all known patterns that are also known as trash.
Like this: `:tmi.twitch.tv 002 phaazebot :Your host is tmi.twitch.tv`
"""
if re.match(ReGarbage, payload) != None:
if re.match(ReGarbage, payload) is not None:
return True

return False

async def mainEventDetector(cls:"Client", payload:str) -> bool:
"""
This detector is suppost to catch all events we can somehow process, if not, give back False.
This detector is suppose to catch all events we can somehow process, if not, give back False.
If that happens the Client `cls` makes additional handling
"""

#response to PING
if re.match(RePing, payload) != None:
# response to PING
if re.match(RePing, payload) is not None:
cls.last_ping = time.time()
await sendPong(cls)
return True

# handels events: onMessage
if re.match(RePrivMessage, payload) != None:
# handles events: onMessage
if re.match(RePrivMessage, payload) is not None:
return await handlePrivMessage(cls, payload)

# handels events: None
if re.match(ReUserState, payload) != None:
# handles events: None
if re.match(ReUserState, payload) is not None:
return await handleUserState(cls, payload)

# handels events: onBan, onTimeout, onClearChat
if re.match(ReClearChat, payload) != None:
# handles events: onBan, onTimeout, onClearChat
if re.match(ReClearChat, payload) is not None:
return await handleClearChat(cls, payload)

# handles events: way to many
if re.match(ReUserNotice, payload) != None:
if re.match(ReUserNotice, payload) is not None:
return await handleUserNotice(cls, payload)

# handels events: onChannelUpdate
if re.match(ReRoomState, payload) != None:
# handles events: onChannelUpdate
if re.match(ReRoomState, payload) is not None:
return await handleRoomState(cls, payload)

# handels events: onMemberJoin
if re.match(ReJoin, payload) != None:
# handles events: onMemberJoin
if re.match(ReJoin, payload) is not None:
return await handleJoin(cls, payload)

# handels events: onMemberPart
if re.match(RePart, payload) != None:
# handles events: onMemberPart
if re.match(RePart, payload) is not None:
return await handlePart(cls, payload)

# handels events: onClearMsg
if re.match(ReClearMsg, payload) != None:
# handles events: onClearMsg
if re.match(ReClearMsg, payload) is not None:
return await handleClearMsg(cls, payload)

# handels events: None
if re.match(ReUserList, payload) != None:
# handles events: None
if re.match(ReUserList, payload) is not None:
return await handleUserList(cls, payload)

# handels events: None
if re.match(ReNotice, payload) != None:
# handles events: None
if re.match(ReNotice, payload) is not None:
return await handleNotice(cls, payload)

# handels events: None
if re.match(ReHostTarget, payload) != None:
# handles events: None
if re.match(ReHostTarget, payload) is not None:
return await handleHostTarget(cls, payload)

# handels events: onReady, onReconnect
if re.match(ReOnReady, payload) != None:
# handles events: onReady, onReconnect
if re.match(ReOnReady, payload) is not None:
if cls.auth_success:
#means we got a reconnect
asyncio.ensure_future( cls.onReconnect() )
# means we got a reconnect
asyncio.ensure_future(cls.onReconnect())
cls.auth_success = True
asyncio.ensure_future( cls.onReady() )
asyncio.ensure_future(cls.onReady())
return True

# wrong_auth
if not cls.auth_success:
if re.match(ReWrongAuth, payload) != None:
raise InvalidAuth( payload )
if re.match(ReWrongAuth, payload) is not None:
raise InvalidAuth(payload)

return False
10 changes: 5 additions & 5 deletions twitch_irc/Utils/errors.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class InvalidAuth(Exception):
"""
Raised when twitch gives us an error login response.
Don't get confused with `InvalidCredentials`, where twitch don't even talkes with us.
Don't get confused with `InvalidCredentials`, where twitch don't even takes with us.
"""

class InvalidCredentials(Exception):
"""
Raised when twitch refuses to talk with us.
This only happens when the credentials are in a invalid syntax.
Most likly it a space in oauth or so:
Most likely it a space in oauth or so:
Wrong:
oauth: 895463214654235456
Expand All @@ -20,11 +20,11 @@ class InvalidCredentials(Exception):
class PingTimeout(Exception):
"""
Raised when twitch didn't responded with a PING in some time.
Most likly means something on twitch site is broken.
Most likely means something on twitch site is broken.
"""

class EmptyPayload(Exception):
"""
Raised when twitch sended empty data.
Most likly means we lost connection without getting a event for it.
Raised when twitch sent empty data.
Most likely means we lost connection without getting a event for it.
"""
Loading

0 comments on commit 9234abe

Please sign in to comment.