24
24
"""
25
25
from __future__ import annotations
26
26
27
- from typing import TYPE_CHECKING , Optional , TypeVar , Union
27
+ from typing import TYPE_CHECKING , Optional , Union , TypeVar , Generic , Callable , List , Any , Dict
28
28
29
- import discord .abc
29
+ import discord .utils
30
30
31
31
if TYPE_CHECKING :
32
- from typing_extensions import ParamSpec
33
-
34
- import discord
35
- from discord import Bot
36
- from discord .state import ConnectionState
37
-
38
- from .core import ApplicationCommand , Option
32
+ from . import ApplicationCommand , Option
39
33
from ..cog import Cog
40
- from ..webhook import WebhookMessage
41
- from typing import Callable
34
+ from ..embeds import Embed
35
+ from ..file import File
36
+ from ..guild import Guild
37
+ from ..interactions import Interaction , InteractionChannel , InteractionResponse , InteractionMessage
38
+ from ..member import Member
39
+ from ..mentions import AllowedMentions
40
+ from ..message import Message
41
+ from ..state import ConnectionState
42
+ from ..user import User
43
+ from ..ui import View
44
+ from ..voice_client import VoiceProtocol
45
+ from ..webhook import Webhook , WebhookMessage
46
+ from typing_extensions import ParamSpec
42
47
43
48
from ..guild import Guild
44
49
from ..interactions import Interaction , InteractionResponse
58
63
__all__ = ("ApplicationContext" , "AutocompleteContext" )
59
64
60
65
61
- class ApplicationContext (discord .abc .Messageable ):
66
+ MISSING : Any = discord .utils .MISSING
67
+
68
+ T = TypeVar ("T" )
69
+ BotT = TypeVar ("BotT" , bound = "Union[discord.Bot, discord.AutoShardedBot]" )
70
+ CogT = TypeVar ("CogT" , bound = "Cog" )
71
+
72
+ if TYPE_CHECKING :
73
+ P = ParamSpec ('P' )
74
+ else :
75
+ P = TypeVar ('P' )
76
+
77
+ class ApplicationContext (discord .abc .Messageable , Generic [BotT ]):
62
78
"""Represents a Discord application command interaction context.
63
79
64
80
This class is not created manually and is instead passed to application
@@ -76,9 +92,9 @@ class ApplicationContext(discord.abc.Messageable):
76
92
The command that this context belongs to.
77
93
"""
78
94
79
- def __init__ (self , bot : Bot , interaction : Interaction ):
80
- self .bot = bot
81
- self .interaction = interaction
95
+ def __init__ (self , bot : BotT , interaction : Interaction ) -> None :
96
+ self .bot : BotT = bot
97
+ self .interaction : Interaction = interaction
82
98
83
99
# below attributes will be set after initialization
84
100
self .command : ApplicationCommand = None # type: ignore
@@ -88,7 +104,7 @@ def __init__(self, bot: Bot, interaction: Interaction):
88
104
89
105
self ._state : ConnectionState = self .interaction ._state
90
106
91
- async def _get_channel (self ) -> discord . abc . Messageable :
107
+ async def _get_channel (self ) -> Optional [ InteractionChannel ] :
92
108
return self .channel
93
109
94
110
async def invoke (self , command : ApplicationCommand [CogT , P , T ], / , * args : P .args , ** kwargs : P .kwargs ) -> T :
@@ -118,7 +134,7 @@ async def invoke(self, command: ApplicationCommand[CogT, P, T], /, *args: P.args
118
134
return await command (self , * args , ** kwargs )
119
135
120
136
@cached_property
121
- def channel (self ):
137
+ def channel (self ) -> Optional [ InteractionChannel ] :
122
138
return self .interaction .channel
123
139
124
140
@cached_property
@@ -133,14 +149,6 @@ def guild(self) -> Optional[Guild]:
133
149
def guild_id (self ) -> Optional [int ]:
134
150
return self .interaction .guild_id
135
151
136
- @cached_property
137
- def locale (self ) -> Optional [str ]:
138
- return self .interaction .locale
139
-
140
- @cached_property
141
- def guild_locale (self ) -> Optional [str ]:
142
- return self .interaction .guild_locale
143
-
144
152
@cached_property
145
153
def me (self ) -> Union [Member , User ]:
146
154
return self .guild .me if self .guild is not None else self .bot .user
@@ -168,6 +176,14 @@ def voice_client(self):
168
176
def response (self ) -> InteractionResponse :
169
177
return self .interaction .response
170
178
179
+ @property
180
+ def cog (self ) -> Optional [Cog ]:
181
+ """Optional[:class:`.Cog`]: Returns the cog associated with this context's command. ``None`` if it does not exist."""
182
+ if self .command is None :
183
+ return None
184
+
185
+ return self .command .cog
186
+
171
187
@property
172
188
def respond (self ) -> Callable [..., Union [Interaction , WebhookMessage ]]:
173
189
"""Callable[..., Union[:class:`~.Interaction`, :class:`~.Webhook`]]: Sends either a response
@@ -195,33 +211,42 @@ def send_followup(self):
195
211
f"Interaction was not yet issued a response. Try using { type (self ).__name__ } .respond() first."
196
212
)
197
213
198
- @property
199
- def defer (self ) :
200
- return self .interaction .response .defer
214
+ @discord . utils . copy_doc ( InteractionResponse . defer )
215
+ async def defer (self , * , ephemeral : bool = False ) -> None :
216
+ return await self .interaction .response .defer ( ephemeral = ephemeral )
201
217
202
218
@property
203
- def followup (self ):
219
+ def followup (self ) -> Webhook :
204
220
return self .interaction .followup
205
221
206
- async def delete (self ):
222
+ async def delete (self ) -> None :
207
223
"""Calls :attr:`~discord.commands.ApplicationContext.respond`.
208
224
If the response is done, then calls :attr:`~discord.commands.ApplicationContext.respond` first."""
209
225
if not self .response .is_done ():
210
226
await self .defer ()
211
227
212
228
return await self .interaction .delete_original_message ()
213
229
214
- @property
215
- def edit (self ):
216
- return self .interaction .edit_original_message
217
-
218
- @property
219
- def cog (self ) -> Optional [Cog ]:
220
- """Optional[:class:`.Cog`]: Returns the cog associated with this context's command. ``None`` if it does not exist."""
221
- if self .command is None :
222
- return None
223
-
224
- return self .command .cog
230
+ async def edit (
231
+ self ,
232
+ * ,
233
+ content : Optional [str ] = MISSING ,
234
+ embeds : List [Embed ] = MISSING ,
235
+ embed : Optional [Embed ] = MISSING ,
236
+ file : File = MISSING ,
237
+ files : List [File ] = MISSING ,
238
+ view : Optional [View ] = MISSING ,
239
+ allowed_mentions : Optional [AllowedMentions ] = None ,
240
+ ) -> InteractionMessage :
241
+ return await self .interaction .edit_original_message (
242
+ content = content ,
243
+ embeds = embeds ,
244
+ embed = embed ,
245
+ file = file ,
246
+ files = files ,
247
+ view = view ,
248
+ allowed_mentions = allowed_mentions ,
249
+ )
225
250
226
251
227
252
class AutocompleteContext :
@@ -248,18 +273,24 @@ class AutocompleteContext:
248
273
"""
249
274
250
275
__slots__ = ("bot" , "interaction" , "command" , "focused" , "value" , "options" )
251
-
252
- def __init__ (self , bot : Bot , interaction : Interaction ) -> None :
253
- self .bot = bot
254
- self .interaction = interaction
255
-
256
- self .command : ApplicationCommand = None # type: ignore
257
- self .focused : Option = None # type: ignore
258
- self .value : str = None # type: ignore
259
- self .options : dict = None # type: ignore
276
+
277
+ def __init__ (
278
+ self ,
279
+ interaction : Interaction ,
280
+ * ,
281
+ command : ApplicationCommand ,
282
+ focused : Option ,
283
+ value : str ,
284
+ options : Dict [str , Any ],
285
+ ) -> None :
286
+ self .interaction : Interaction = interaction
287
+ self .command : ApplicationCommand = command
288
+ self .focused : Option = focused
289
+ self .value : str = value
290
+ self .options : Dict [str , Any ] = options
260
291
261
292
@property
262
- def cog (self ) -> Optional [Cog ]:
293
+ def cog (self ) -> Optional [CogT ]:
263
294
"""Optional[:class:`.Cog`]: Returns the cog associated with this context's command. ``None`` if it does not exist."""
264
295
if self .command is None :
265
296
return None
0 commit comments