9
9
from aiohttp import FormData
10
10
11
11
from interactions .client import const
12
- from interactions .client .const import get_logger , MISSING
12
+ from interactions .client .const import get_logger , MISSING , ClientT
13
13
from interactions .models .discord .components import BaseComponent
14
14
from interactions .models .discord .file import UPLOADABLE_TYPE
15
15
from interactions .models .discord .poll import Poll
@@ -149,17 +149,14 @@ def from_dict(cls, client: "interactions.Client", data: dict, guild_id: None | S
149
149
return instance
150
150
151
151
152
- class BaseContext (metaclass = abc .ABCMeta ):
152
+ class BaseContext (typing . Generic [ ClientT ], metaclass = abc .ABCMeta ):
153
153
"""
154
154
Base context class for all contexts.
155
155
156
156
Define your own context class by inheriting from this class. For compatibility with the library, you must define a `from_dict` classmethod that takes a dict and returns an instance of your context class.
157
157
158
158
"""
159
159
160
- client : "interactions.Client"
161
- """The client that created this context."""
162
-
163
160
command : BaseCommand
164
161
"""The command this context invokes."""
165
162
@@ -173,8 +170,10 @@ class BaseContext(metaclass=abc.ABCMeta):
173
170
guild_id : typing .Optional [Snowflake ]
174
171
"""The id of the guild this context was invoked in, if any."""
175
172
176
- def __init__ (self , client : "interactions.Client" ) -> None :
177
- self .client = client
173
+ def __init__ (self , client : ClientT ) -> None :
174
+ self .client : ClientT = client
175
+ """The client that created this context."""
176
+
178
177
self .author_id = MISSING
179
178
self .channel_id = MISSING
180
179
self .message_id = MISSING
@@ -218,12 +217,12 @@ def voice_state(self) -> typing.Optional["interactions.VoiceState"]:
218
217
return self .client .cache .get_bot_voice_state (self .guild_id )
219
218
220
219
@property
221
- def bot (self ) -> "interactions.Client " :
220
+ def bot (self ) -> "ClientT " :
222
221
return self .client
223
222
224
223
@classmethod
225
224
@abc .abstractmethod
226
- def from_dict (cls , client : "interactions.Client " , payload : dict ) -> Self :
225
+ def from_dict (cls , client : "ClientT " , payload : dict ) -> Self :
227
226
"""
228
227
Create a context instance from a dict.
229
228
@@ -238,7 +237,7 @@ def from_dict(cls, client: "interactions.Client", payload: dict) -> Self:
238
237
raise NotImplementedError
239
238
240
239
241
- class BaseInteractionContext (BaseContext ):
240
+ class BaseInteractionContext (BaseContext [ ClientT ] ):
242
241
token : str
243
242
"""The interaction token."""
244
243
id : Snowflake
@@ -281,14 +280,14 @@ class BaseInteractionContext(BaseContext):
281
280
kwargs : dict [str , typing .Any ]
282
281
"""The keyword arguments passed to the interaction."""
283
282
284
- def __init__ (self , client : "interactions.Client " ) -> None :
283
+ def __init__ (self , client : "ClientT " ) -> None :
285
284
super ().__init__ (client )
286
285
self .deferred = False
287
286
self .responded = False
288
287
self .ephemeral = False
289
288
290
289
@classmethod
291
- def from_dict (cls , client : "interactions.Client " , payload : dict ) -> Self :
290
+ def from_dict (cls , client : "ClientT " , payload : dict ) -> Self :
292
291
instance = cls (client = client )
293
292
instance .token = payload ["token" ]
294
293
instance .id = Snowflake (payload ["id" ])
@@ -418,7 +417,7 @@ def gather_options(_options: list[dict[str, typing.Any]]) -> dict[str, typing.An
418
417
self .args = list (self .kwargs .values ())
419
418
420
419
421
- class InteractionContext (BaseInteractionContext , SendMixin ):
420
+ class InteractionContext (BaseInteractionContext [ ClientT ] , SendMixin ):
422
421
async def defer (self , * , ephemeral : bool = False , suppress_error : bool = False ) -> None :
423
422
"""
424
423
Defer the interaction.
@@ -657,26 +656,26 @@ async def edit(
657
656
return self .client .cache .place_message_data (message_data )
658
657
659
658
660
- class SlashContext (InteractionContext , ModalMixin ):
659
+ class SlashContext (InteractionContext [ ClientT ] , ModalMixin ):
661
660
@classmethod
662
- def from_dict (cls , client : "interactions.Client " , payload : dict ) -> Self :
661
+ def from_dict (cls , client : "ClientT " , payload : dict ) -> Self :
663
662
return super ().from_dict (client , payload )
664
663
665
664
666
- class ContextMenuContext (InteractionContext , ModalMixin ):
665
+ class ContextMenuContext (InteractionContext [ ClientT ] , ModalMixin ):
667
666
target_id : Snowflake
668
667
"""The id of the target of the context menu."""
669
668
editing_origin : bool
670
669
"""Whether you have deferred the interaction and are editing the original response."""
671
670
target_type : None | CommandType
672
671
"""The type of the target of the context menu."""
673
672
674
- def __init__ (self , client : "interactions.Client " ) -> None :
673
+ def __init__ (self , client : "ClientT " ) -> None :
675
674
super ().__init__ (client )
676
675
self .editing_origin = False
677
676
678
677
@classmethod
679
- def from_dict (cls , client : "interactions.Client " , payload : dict ) -> Self :
678
+ def from_dict (cls , client : "ClientT " , payload : dict ) -> Self :
680
679
instance = super ().from_dict (client , payload )
681
680
instance .target_id = Snowflake (payload ["data" ]["target_id" ])
682
681
instance .target_type = CommandType (payload ["data" ]["type" ])
@@ -739,7 +738,7 @@ def target(self) -> None | Message | User | Member:
739
738
return self .resolved .get (self .target_id )
740
739
741
740
742
- class ComponentContext (InteractionContext , ModalMixin ):
741
+ class ComponentContext (InteractionContext [ ClientT ] , ModalMixin ):
743
742
values : list [str ]
744
743
"""The values of the SelectMenu component, if any."""
745
744
custom_id : str
@@ -750,7 +749,7 @@ class ComponentContext(InteractionContext, ModalMixin):
750
749
"""Whether you have deferred the interaction and are editing the original response."""
751
750
752
751
@classmethod
753
- def from_dict (cls , client : "interactions.Client " , payload : dict ) -> Self :
752
+ def from_dict (cls , client : "ClientT " , payload : dict ) -> Self :
754
753
instance = super ().from_dict (client , payload )
755
754
instance .values = payload ["data" ].get ("values" , [])
756
755
instance .custom_id = payload ["data" ]["custom_id" ]
@@ -920,7 +919,7 @@ def component(self) -> typing.Optional[BaseComponent]:
920
919
return component
921
920
922
921
923
- class ModalContext (InteractionContext ):
922
+ class ModalContext (InteractionContext [ ClientT ] ):
924
923
responses : dict [str , str ]
925
924
"""The responses of the modal. The key is the `custom_id` of the component."""
926
925
custom_id : str
@@ -929,7 +928,7 @@ class ModalContext(InteractionContext):
929
928
"""Whether to edit the original message instead of sending a new one."""
930
929
931
930
@classmethod
932
- def from_dict (cls , client : "interactions.Client " , payload : dict ) -> Self :
931
+ def from_dict (cls , client : "ClientT " , payload : dict ) -> Self :
933
932
instance = super ().from_dict (client , payload )
934
933
instance .responses = {
935
934
comp ["components" ][0 ]["custom_id" ]: comp ["components" ][0 ]["value" ] for comp in payload ["data" ]["components" ]
@@ -990,12 +989,12 @@ async def _defer(self, *, ephemeral: bool = False, edit_origin: bool = False) ->
990
989
self .ephemeral = ephemeral
991
990
992
991
993
- class AutocompleteContext (BaseInteractionContext ):
992
+ class AutocompleteContext (BaseInteractionContext [ ClientT ] ):
994
993
focussed_option : SlashCommandOption # todo: option parsing
995
994
"""The option the user is currently filling in."""
996
995
997
996
@classmethod
998
- def from_dict (cls , client : "interactions.Client " , payload : dict ) -> Self :
997
+ def from_dict (cls , client : "ClientT " , payload : dict ) -> Self :
999
998
return super ().from_dict (client , payload )
1000
999
1001
1000
@property
0 commit comments