24
24
"""
25
25
from __future__ import annotations
26
26
27
- from typing import TYPE_CHECKING , Optional , Union , TypeVar , Generic , Callable , List , Any , Dict
27
+ from typing import TYPE_CHECKING , Optional , TypeVar , Union
28
28
29
- import discord .utils
29
+ import discord .abc
30
30
31
31
if TYPE_CHECKING :
32
- from . import ApplicationCommand , Option
33
- from ..cog import Cog
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
32
from typing_extensions import ParamSpec
47
33
34
+ import discord
35
+ from discord import Bot
36
+ from discord .state import ConnectionState
37
+
38
+ from .core import ApplicationCommand , Option
39
+ from ..cog import Cog
40
+ from ..webhook import WebhookMessage
41
+ from typing import Callable
42
+
48
43
from ..guild import Guild
49
44
from ..interactions import Interaction , InteractionResponse
50
45
from ..member import Member
63
58
__all__ = ("ApplicationContext" , "AutocompleteContext" )
64
59
65
60
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 ]):
61
+ class ApplicationContext (discord .abc .Messageable ):
78
62
"""Represents a Discord application command interaction context.
79
63
80
64
This class is not created manually and is instead passed to application
@@ -92,9 +76,9 @@ class ApplicationContext(discord.abc.Messageable, Generic[BotT]):
92
76
The command that this context belongs to.
93
77
"""
94
78
95
- def __init__ (self , bot : BotT , interaction : Interaction ) -> None :
96
- self .bot : BotT = bot
97
- self .interaction : Interaction = interaction
79
+ def __init__ (self , bot : Bot , interaction : Interaction ):
80
+ self .bot = bot
81
+ self .interaction = interaction
98
82
99
83
# below attributes will be set after initialization
100
84
self .command : ApplicationCommand = None # type: ignore
@@ -104,7 +88,7 @@ def __init__(self, bot: BotT, interaction: Interaction) -> None:
104
88
105
89
self ._state : ConnectionState = self .interaction ._state
106
90
107
- async def _get_channel (self ) -> Optional [ InteractionChannel ] :
91
+ async def _get_channel (self ) -> discord . abc . Messageable :
108
92
return self .channel
109
93
110
94
async def invoke (self , command : ApplicationCommand [CogT , P , T ], / , * args : P .args , ** kwargs : P .kwargs ) -> T :
@@ -134,7 +118,7 @@ async def invoke(self, command: ApplicationCommand[CogT, P, T], /, *args: P.args
134
118
return await command (self , * args , ** kwargs )
135
119
136
120
@cached_property
137
- def channel (self ) -> Optional [ InteractionChannel ] :
121
+ def channel (self ):
138
122
return self .interaction .channel
139
123
140
124
@cached_property
@@ -149,6 +133,14 @@ def guild(self) -> Optional[Guild]:
149
133
def guild_id (self ) -> Optional [int ]:
150
134
return self .interaction .guild_id
151
135
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
+
152
144
@cached_property
153
145
def me (self ) -> Union [Member , User ]:
154
146
return self .guild .me if self .guild is not None else self .bot .user
@@ -176,14 +168,6 @@ def voice_client(self):
176
168
def response (self ) -> InteractionResponse :
177
169
return self .interaction .response
178
170
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
-
187
171
@property
188
172
def respond (self ) -> Callable [..., Union [Interaction , WebhookMessage ]]:
189
173
"""Callable[..., Union[:class:`~.Interaction`, :class:`~.Webhook`]]: Sends either a response
@@ -211,42 +195,33 @@ def send_followup(self):
211
195
f"Interaction was not yet issued a response. Try using { type (self ).__name__ } .respond() first."
212
196
)
213
197
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 )
198
+ @property
199
+ def defer (self ) :
200
+ return self .interaction .response .defer
217
201
218
202
@property
219
- def followup (self ) -> Webhook :
203
+ def followup (self ):
220
204
return self .interaction .followup
221
205
222
- async def delete (self ) -> None :
206
+ async def delete (self ):
223
207
"""Calls :attr:`~discord.commands.ApplicationContext.respond`.
224
208
If the response is done, then calls :attr:`~discord.commands.ApplicationContext.respond` first."""
225
209
if not self .response .is_done ():
226
210
await self .defer ()
227
211
228
212
return await self .interaction .delete_original_message ()
229
213
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
- )
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
250
225
251
226
252
227
class AutocompleteContext :
@@ -273,24 +248,18 @@ class AutocompleteContext:
273
248
"""
274
249
275
250
__slots__ = ("bot" , "interaction" , "command" , "focused" , "value" , "options" )
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
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
291
260
292
261
@property
293
- def cog (self ) -> Optional [CogT ]:
262
+ def cog (self ) -> Optional [Cog ]:
294
263
"""Optional[:class:`.Cog`]: Returns the cog associated with this context's command. ``None`` if it does not exist."""
295
264
if self .command is None :
296
265
return None
0 commit comments