22from typing import List , Optional , Union
33
44from ..api .error import LibraryException
5- from ..api .models .attrs_utils import MISSING , DictSerializerMixin , define , field
5+ from ..api .http .client import HTTPClient
6+ from ..api .models .attrs_utils import MISSING , ClientSerializerMixin , define , field
67from ..api .models .channel import Channel
78from ..api .models .guild import Guild
89from ..api .models .member import Member
2526
2627
2728@define ()
28- class _Context (DictSerializerMixin ):
29+ class _Context (ClientSerializerMixin ):
2930 """
3031 The base class of "context" for dispatched event data
3132 from the gateway. The premise of having this class is so
@@ -40,7 +41,7 @@ class _Context(DictSerializerMixin):
4041 :ivar Optional[Guild] guild: The guild data model.
4142 """
4243
43- client = field ()
44+ client : HTTPClient = field (default = None )
4445 message : Optional [Message ] = field (converter = Message , default = None , add_client = True )
4546 author : Member = field (converter = Member , default = None , add_client = True )
4647 member : Member = field (converter = Member , add_client = True )
@@ -62,16 +63,18 @@ class _Context(DictSerializerMixin):
6263 deferred : bool = field (default = False )
6364
6465 def __attrs_post_init__ (self ) -> None :
66+ # backwards compatibility
67+ self .client = self ._client
6568 self .author = self .member
6669
6770 if self .user is None :
6871 self .user = self .member .user if self .member else None
6972
7073 if self .guild is None and self .guild_id is not None :
71- self .guild = self .client .cache .guilds .values .get (self .guild_id , MISSING )
74+ self .guild = self ._client .cache .guilds .values .get (self .guild_id , MISSING )
7275
7376 if self .channel is None :
74- self .channel = self .client .cache .channels .values .get (self .channel_id , MISSING )
77+ self .channel = self ._client .cache .channels .values .get (self .channel_id , MISSING )
7578
7679 async def get_channel (self ) -> Channel :
7780 """
@@ -81,8 +84,8 @@ async def get_channel(self) -> Channel:
8184 :rtype: Channel
8285 """
8386
84- res = await self .client .get_channel (int (self .channel_id ))
85- self .channel = Channel (** res , _client = self .client )
87+ res = await self ._client .get_channel (int (self .channel_id ))
88+ self .channel = Channel (** res , _client = self ._client )
8689 return self .channel
8790
8891 async def get_guild (self ) -> Guild :
@@ -93,8 +96,8 @@ async def get_guild(self) -> Guild:
9396 :rtype: Guild
9497 """
9598
96- res = await self .client .get_guild (int (self .guild_id ))
97- self .guild = Guild (** res , _client = self .client )
99+ res = await self ._client .get_guild (int (self .guild_id ))
100+ self .guild = Guild (** res , _client = self ._client )
98101 return self .guild
99102
100103 async def send (
@@ -266,7 +269,7 @@ async def popup(self, modal: Modal) -> None:
266269 },
267270 }
268271
269- await self .client .create_interaction_response (
272+ await self ._client .create_interaction_response (
270273 token = self .token ,
271274 application_id = int (self .id ),
272275 data = payload ,
@@ -333,39 +336,39 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message:
333336
334337 if self .deferred :
335338 if hasattr (self .message , "id" ) and self .message .id is not None :
336- res = await self .client .edit_message (
339+ res = await self ._client .edit_message (
337340 int (self .channel_id ), int (self .message .id ), payload = payload
338341 )
339- self .message = msg = Message (** res , _client = self .client )
342+ self .message = msg = Message (** res , _client = self ._client )
340343 else :
341- res = await self .client .edit_interaction_response (
344+ res = await self ._client .edit_interaction_response (
342345 token = self .token ,
343346 application_id = str (self .id ),
344347 data = {"type" : self .callback .value , "data" : payload },
345348 message_id = self .message .id if self .message else "@original" ,
346349 )
347350 if res ["flags" ] == 64 :
348351 log .warning ("You can't edit hidden messages." )
349- self .message = payload
350- self .message ._client = self .client
351352 else :
352- await self .client .edit_message (int (self .channel_id ), res ["id" ], payload = payload )
353- self .message = msg = Message (** res , _client = self .client )
353+ await self ._client .edit_message (
354+ int (self .channel_id ), res ["id" ], payload = payload
355+ )
356+ self .message = msg = Message (** res , _client = self ._client )
354357 else :
355- res = await self .client .edit_interaction_response (
358+ res = await self ._client .edit_interaction_response (
356359 token = self .token ,
357360 application_id = str (self .application_id ),
358361 data = {"type" : self .callback .value , "data" : payload },
359362 )
360363 if res ["flags" ] == 64 :
361364 log .warning ("You can't edit hidden messages." )
362365 else :
363- await self .client .edit_message (int (self .channel_id ), res ["id" ], payload = payload )
364- self .message = msg = Message (** res , _client = self .client )
366+ await self ._client .edit_message (int (self .channel_id ), res ["id" ], payload = payload )
367+ self .message = msg = Message (** res , _client = self ._client )
365368
366369 if msg is not None :
367370 return msg
368- return Message (** payload , _client = self .client )
371+ return Message (** payload , _client = self ._client )
369372
370373 async def defer (self , ephemeral : Optional [bool ] = False ) -> None :
371374 """
@@ -379,7 +382,7 @@ async def defer(self, ephemeral: Optional[bool] = False) -> None:
379382 _ephemeral : int = (1 << 6 ) if ephemeral else 0
380383 self .callback = InteractionCallbackType .DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
381384
382- await self .client .create_interaction_response (
385+ await self ._client .create_interaction_response (
383386 token = self .token ,
384387 application_id = int (self .id ),
385388 data = {"type" : self .callback .value , "data" : {"flags" : _ephemeral }},
@@ -396,42 +399,42 @@ async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message:
396399 msg = None
397400 if self .responded or self .deferred :
398401 if self .deferred :
399- res = await self .client .edit_interaction_response (
402+ res = await self ._client .edit_interaction_response (
400403 data = payload ,
401404 token = self .token ,
402405 application_id = str (self .application_id ),
403406 )
404407 self .responded = True
405408 else :
406- res = await self .client ._post_followup (
409+ res = await self ._client ._post_followup (
407410 data = payload ,
408411 token = self .token ,
409412 application_id = str (self .application_id ),
410413 )
411- self .message = msg = Message (** res , _client = self .client )
414+ self .message = msg = Message (** res , _client = self ._client )
412415 else :
413- await self .client .create_interaction_response (
416+ await self ._client .create_interaction_response (
414417 token = self .token ,
415418 application_id = int (self .id ),
416419 data = _payload ,
417420 )
418- __newdata = await self .client .edit_interaction_response (
421+ __newdata = await self ._client .edit_interaction_response (
419422 data = {},
420423 token = self .token ,
421424 application_id = str (self .application_id ),
422425 )
423426 if not __newdata .get ("code" ):
424427 # if sending message fails somehow
425- msg = Message (** __newdata , _client = self .client )
428+ msg = Message (** __newdata , _client = self ._client )
426429 self .message = msg
427430 self .responded = True
428431 if msg is not None :
429432 return msg
430433
431434 return Message (
432435 ** payload ,
433- _client = self .client ,
434- author = {"client " : self .client , "id" : None , "username" : None , "discriminator" : None },
436+ _client = self ._client ,
437+ author = {"_client " : self ._client , "id" : None , "username" : None , "discriminator" : None },
435438 )
436439
437440 async def delete (self ) -> None :
@@ -444,11 +447,11 @@ async def delete(self) -> None:
444447 being present.
445448 """
446449 if self .responded :
447- await self .client .delete_webhook_message (
450+ await self ._client .delete_webhook_message (
448451 webhook_id = int (self .id ), webhook_token = self .token , message_id = int (self .message .id )
449452 )
450453 else :
451- await self .client .delete_original_webhook_message (int (self .id ), self .token )
454+ await self ._client .delete_original_webhook_message (int (self .id ), self .token )
452455 self .message = None
453456
454457 async def populate (self , choices : Union [Choice , List [Choice ]]) -> List [Choice ]:
@@ -487,7 +490,7 @@ async def func():
487490 6 , message = "Autocomplete choice items must be of type Choice"
488491 )
489492
490- await self .client .create_interaction_response (
493+ await self ._client .create_interaction_response (
491494 token = self .token ,
492495 application_id = int (self .id ),
493496 data = {
@@ -514,31 +517,31 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message:
514517
515518 if not self .deferred :
516519 self .callback = InteractionCallbackType .UPDATE_MESSAGE
517- await self .client .create_interaction_response (
520+ await self ._client .create_interaction_response (
518521 data = {"type" : self .callback .value , "data" : payload },
519522 token = self .token ,
520523 application_id = int (self .id ),
521524 )
522525 # self.message = payload
523526 self .responded = True
524527 elif self .callback != InteractionCallbackType .DEFERRED_UPDATE_MESSAGE :
525- res = await self .client ._post_followup (
528+ res = await self ._client ._post_followup (
526529 data = payload ,
527530 token = self .token ,
528531 application_id = str (self .application_id ),
529532 )
530- self .message = Message (** res , _client = self .client )
533+ self .message = Message (** res , _client = self ._client )
531534 else :
532- res = await self .client .edit_interaction_response (
535+ res = await self ._client .edit_interaction_response (
533536 data = payload ,
534537 token = self .token ,
535538 application_id = str (self .application_id ),
536539 )
537540 self .responded = True
538- self .message = Message (** res , _client = self .client )
541+ self .message = Message (** res , _client = self ._client )
539542
540543 if self .message is None :
541- self .message = Message (** payload , _client = self .client )
544+ self .message = Message (** payload , _client = self ._client )
542545
543546 return self .message
544547
@@ -557,34 +560,34 @@ async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message:
557560 or self .callback == InteractionCallbackType .DEFERRED_UPDATE_MESSAGE
558561 ):
559562 if self .deferred :
560- res = await self .client .edit_interaction_response (
563+ res = await self ._client .edit_interaction_response (
561564 data = payload ,
562565 token = self .token ,
563566 application_id = str (self .application_id ),
564567 )
565568 self .responded = True
566569 else :
567- res = await self .client ._post_followup (
570+ res = await self ._client ._post_followup (
568571 data = payload ,
569572 token = self .token ,
570573 application_id = str (self .application_id ),
571574 )
572- self .message = msg = Message (** res , _client = self .client )
575+ self .message = msg = Message (** res , _client = self ._client )
573576
574577 else :
575- await self .client .create_interaction_response (
578+ await self ._client .create_interaction_response (
576579 token = self .token ,
577580 application_id = int (self .id ),
578581 data = _payload ,
579582 )
580- __newdata = await self .client .edit_interaction_response (
583+ __newdata = await self ._client .edit_interaction_response (
581584 data = {},
582585 token = self .token ,
583586 application_id = str (self .application_id ),
584587 )
585588 if not __newdata .get ("code" ):
586589 # if sending message fails somehow
587- msg = Message (** __newdata , _client = self .client )
590+ msg = Message (** __newdata , _client = self ._client )
588591 self .message = msg
589592 self .responded = True
590593
@@ -613,7 +616,7 @@ async def defer(
613616 else :
614617 self .callback = InteractionCallbackType .DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
615618
616- await self .client .create_interaction_response (
619+ await self ._client .create_interaction_response (
617620 token = self .token ,
618621 application_id = int (self .id ),
619622 data = {"type" : self .callback .value , "data" : {"flags" : _ephemeral }},
0 commit comments