4444
4545if TYPE_CHECKING :
4646 from .client import Client
47-
48-
4947log = logging .getLogger (__name__ )
5048HOST = "wss://irc-ws.chat.twitch.tv:443"
5149
@@ -103,7 +101,6 @@ def __init__(
103101 self ._initial_channels = _temp_initial_channels
104102 else :
105103 self ._initial_channels = [_temp_initial_channels ]
106-
107104 self ._last_ping = 0
108105 self ._reconnect_requested = False
109106
@@ -122,15 +119,12 @@ async def _connect(self):
122119
123120 if self ._keeper :
124121 self ._keeper .cancel () # Stop our current keep alive.
125-
126122 if self .is_alive :
127123 await self ._websocket .close () # If for some reason we are in a weird state, close it before retrying.
128-
129124 if not self ._client ._http .nick :
130125 data = await self ._client ._http .validate (token = self ._token )
131126 self .nick = data ["login" ]
132127 self .user_id = int (data ["user_id" ])
133-
134128 session = self ._client ._http .session
135129
136130 try :
@@ -141,12 +135,10 @@ async def _connect(self):
141135
142136 await asyncio .sleep (retry )
143137 return asyncio .create_task (self ._connect ())
144-
145138 if time .time () > self ._last_ping + 240 or self ._reconnect_requested :
146139 # Re-authenticate as we have surpassed a PING request or Twitch issued a RECONNECT.
147140 await self .authenticate (self ._initial_channels )
148141 self ._reconnect_requested = False
149-
150142 self ._keeper = asyncio .create_task (self ._keep_alive ()) # Create our keep alive.
151143 self ._ws_ready_event .set ()
152144
@@ -156,14 +148,12 @@ async def _keep_alive(self):
156148
157149 if not self ._last_ping :
158150 self ._last_ping = time .time ()
159-
160151 while not self ._websocket .closed :
161152 msg = await self ._websocket .receive () # Receive data...
162153
163154 if msg .type is aiohttp .WSMsgType .CLOSED :
164155 log .error (f"Websocket connection was closed: { msg .extra } " )
165156 break
166-
167157 data = msg .data
168158 if data :
169159 log .debug (f" < { data } " )
@@ -173,10 +163,8 @@ async def _keep_alive(self):
173163 for event in events :
174164 if not event :
175165 continue
176-
177166 task = asyncio .create_task (self ._process_data (event ))
178167 task .add_done_callback (partial (self ._task_callback , event )) # Process our raw data
179-
180168 asyncio .create_task (self ._connect ())
181169
182170 def _task_callback (self , data , task ):
@@ -201,7 +189,6 @@ async def send(self, message: str):
201189
202190 task = asyncio .create_task (self ._process_data (dummy ))
203191 task .add_done_callback (partial (self ._task_callback , dummy )) # Process our raw data
204-
205192 await self ._websocket .send_str (message + "\r \n " )
206193
207194 async def reply (self , msg_id : str , message : str ):
@@ -216,7 +203,6 @@ async def reply(self, msg_id: str, message: str):
216203 dummy = f"> @reply-parent-msg-id={ msg_id } :{ self .nick } !{ self .nick } @{ self .nick } .tmi.twitch.tv PRIVMSG(ECHO) #{ channel } { content } \r \n "
217204 task = asyncio .create_task (self ._process_data (dummy ))
218205 task .add_done_callback (partial (self ._task_callback , dummy )) # Process our raw data
219-
220206 await self ._websocket .send_str (f"@reply-parent-msg-id={ msg_id } { message } \r \n " )
221207
222208 async def authenticate (self , channels : Union [list , tuple ]):
@@ -236,16 +222,13 @@ async def authenticate(self, channels: Union[list, tuple]):
236222 """
237223 if not self .is_alive :
238224 return
239-
240225 await self .send (f"PASS oauth:{ self ._token } \r \n " )
241226 await self .send (f"NICK { self .nick } \r \n " )
242227
243228 for cap in self .modes :
244229 await self .send (f"CAP REQ :twitch.tv/{ cap } " ) # Ideally no one should overwrite defaults...
245-
246230 if not channels and not self ._initial_channels :
247231 return
248-
249232 channels = channels or self ._initial_channels
250233 await self .join_channels (* channels )
251234
@@ -281,10 +264,8 @@ async def join_channels(self, *channels: str):
281264 if self ._join_handle < time .time (): # Handle is less than the current time
282265 self ._join_tick = 20 # So lets start a new rate limit bucket..
283266 self ._join_handle = time .time () + 11 # Set the handle timeout time
284-
285267 if self ._join_tick == 0 : # We have exhausted the bucket, wait so we can make a new one...
286268 await asyncio .sleep (self ._join_handle - time .time ())
287-
288269 asyncio .create_task (self ._join_channel (channel ))
289270 self ._join_tick -= 1
290271
@@ -322,7 +303,6 @@ async def _process_data(self, data: str):
322303 f'Login unsuccessful with token "{ self ._token } ". ' f'Check your scopes for "chat_login" and try again.'
323304 )
324305 return await self ._close ()
325-
326306 partial_ = self ._actions .get (parsed ["action" ])
327307 if partial_ :
328308 await partial_ (parsed )
@@ -335,10 +315,8 @@ async def _await_futures(self):
335315 fut .exception ()
336316 except asyncio .InvalidStateError :
337317 pass
338-
339318 if fut .done ():
340319 futures .remove (fut )
341-
342320 if futures :
343321 await asyncio .wait (futures )
344322
@@ -350,14 +328,11 @@ async def _code(self, parsed, code: int):
350328 await self .is_ready .wait ()
351329 self .dispatch ("ready" )
352330 self ._init = True
353-
354331 elif code == 353 :
355332 if parsed ["channel" ] == "TWITCHIOFAILURE" :
356333 self ._initial_channels .remove (parsed ["batches" ][0 ])
357-
358334 if parsed ["channel" ] in [c .lower ().lstrip ("#" ) for c in self ._initial_channels ] and not self ._init :
359335 self ._join_load [parsed ["channel" ]] = None
360-
361336 if len (self ._join_load ) == len (self ._initial_channels ):
362337 for channel in self ._initial_channels :
363338 self ._join_load .pop (channel .lower ().lstrip ("#" ))
@@ -391,7 +366,6 @@ async def _part(self, parsed): # TODO
391366 self ._join_pending .pop (channel )
392367 if not self ._retain_cache :
393368 self ._cache .pop (channel , None )
394-
395369 channel = Channel (name = channel , websocket = self )
396370 user = Chatter (name = parsed ["user" ], bot = self ._client , websocket = self , channel = channel , tags = parsed ["badges" ])
397371
@@ -410,7 +384,6 @@ async def _privmsg(self, parsed): # TODO(Update Cache properly)
410384 user = Chatter (
411385 tags = parsed ["badges" ], name = parsed ["user" ], channel = channel , bot = self ._client , websocket = self
412386 )
413-
414387 message = Message (
415388 raw_data = parsed ["data" ],
416389 content = parsed ["message" ],
@@ -462,24 +435,20 @@ async def _join(self, parsed):
462435 pass
463436 else :
464437 self ._join_pending .pop (channel )
465-
466438 if parsed ["user" ] != self ._client .nick :
467439 self ._cache_add (parsed )
468-
469440 channel = Channel (name = channel , websocket = self )
470441 user = Chatter (name = parsed ["user" ], bot = self ._client , websocket = self , channel = channel , tags = parsed ["badges" ])
471442
472443 if user .name == self ._client .nick :
473- self .dispatch ('channel_joined' , channel )
474-
444+ self .dispatch ("channel_joined" , channel )
475445 self .dispatch ("join" , channel , user )
476446
477447 def _cache_add (self , parsed : dict ):
478448 channel = parsed ["channel" ].lstrip ("#" )
479449
480450 if channel not in self ._cache :
481451 self ._cache [channel ] = set ()
482-
483452 channel_ = Channel (name = channel , websocket = self )
484453
485454 if parsed ["batches" ]:
@@ -522,7 +491,6 @@ async def _close(self):
522491
523492 for fut in futures :
524493 fut .cancel ()
525-
526494 if self ._websocket :
527495 await self ._websocket .close ()
528496 if self ._client ._http .session :
0 commit comments