27
27
from typing import TYPE_CHECKING , Optional , TypeVar , Union
28
28
29
29
import discord .abc
30
+ from discord .interactions import InteractionMessage
30
31
31
32
if TYPE_CHECKING :
32
33
from typing_extensions import ParamSpec
33
34
34
35
import discord
35
- from discord import Bot
36
- from discord .state import ConnectionState
36
+ from .. import Bot
37
+ from ..state import ConnectionState
38
+ from ..voice_client import VoiceProtocol
37
39
38
40
from .core import ApplicationCommand , Option
41
+ from ..interactions import Interaction , InteractionResponse , InteractionChannel
42
+ from ..guild import Guild
43
+ from ..member import Member
44
+ from ..message import Message
45
+ from ..user import User
46
+ from ..client import ClientUser
47
+ from discord .webhook .async_ import Webhook
48
+
39
49
from ..cog import Cog
40
50
from ..webhook import WebhookMessage
41
- from typing import Callable
51
+
52
+ from typing import Callable , Awaitable
42
53
43
- from ..guild import Guild
44
- from ..interactions import Interaction , InteractionResponse
45
- from ..member import Member
46
- from ..message import Message
47
- from ..user import User
48
- from ..utils import cached_property
54
+ from ..utils import _cached_property as cached_property
49
55
50
56
T = TypeVar ('T' )
51
57
CogT = TypeVar ('CogT' , bound = "Cog" )
@@ -88,8 +94,8 @@ def __init__(self, bot: Bot, interaction: Interaction):
88
94
89
95
self ._state : ConnectionState = self .interaction ._state
90
96
91
- async def _get_channel (self ) -> discord . abc . Messageable :
92
- return self .channel
97
+ async def _get_channel (self ) -> Optional [ InteractionChannel ] :
98
+ return self .interaction . channel
93
99
94
100
async def invoke (self , command : ApplicationCommand [CogT , P , T ], / , * args : P .args , ** kwargs : P .kwargs ) -> T :
95
101
r"""|coro|
@@ -118,7 +124,7 @@ async def invoke(self, command: ApplicationCommand[CogT, P, T], /, *args: P.args
118
124
return await command (self , * args , ** kwargs )
119
125
120
126
@cached_property
121
- def channel (self ):
127
+ def channel (self ) -> Optional [ InteractionChannel ] :
122
128
return self .interaction .channel
123
129
124
130
@cached_property
@@ -142,8 +148,8 @@ def guild_locale(self) -> Optional[str]:
142
148
return self .interaction .guild_locale
143
149
144
150
@cached_property
145
- def me (self ) -> Union [Member , User ]:
146
- return self .guild .me if self .guild is not None else self .bot .user
151
+ def me (self ) -> Optional [ Union [Member , ClientUser ] ]:
152
+ return self .interaction . guild .me if self . interaction .guild is not None else self .bot .user
147
153
148
154
@cached_property
149
155
def message (self ) -> Optional [Message ]:
@@ -153,66 +159,64 @@ def message(self) -> Optional[Message]:
153
159
def user (self ) -> Optional [Union [Member , User ]]:
154
160
return self .interaction .user
155
161
156
- @cached_property
157
- def author (self ) -> Optional [Union [Member , User ]]:
158
- return self .user
162
+ author = user
159
163
160
164
@property
161
- def voice_client (self ):
162
- if self .guild is None :
165
+ def voice_client (self ) -> Optional [ VoiceProtocol ] :
166
+ if self .interaction . guild is None :
163
167
return None
164
168
165
- return self .guild .voice_client
169
+ return self .interaction . guild .voice_client
166
170
167
171
@cached_property
168
172
def response (self ) -> InteractionResponse :
169
173
return self .interaction .response
170
174
171
175
@property
172
- def respond (self ) -> Callable [..., Union [Interaction , WebhookMessage ]]:
176
+ def respond (self ) -> Callable [..., Awaitable [ Union [Interaction , WebhookMessage ] ]]:
173
177
"""Callable[..., Union[:class:`~.Interaction`, :class:`~.Webhook`]]: Sends either a response
174
178
or a followup response depending if the interaction has been responded to yet or not."""
175
- if not self .response .is_done ():
179
+ if not self .interaction . response .is_done ():
176
180
return self .interaction .response .send_message # self.response
177
181
else :
178
182
return self .followup .send # self.send_followup
179
183
180
184
@property
181
- def send_response (self ):
182
- if not self .response .is_done ():
185
+ def send_response (self ) -> Callable [..., Awaitable [ Interaction ]] :
186
+ if not self .interaction . response .is_done ():
183
187
return self .interaction .response .send_message
184
188
else :
185
189
raise RuntimeError (
186
190
f"Interaction was already issued a response. Try using { type (self ).__name__ } .send_followup() instead."
187
191
)
188
192
189
193
@property
190
- def send_followup (self ):
191
- if self .response .is_done ():
194
+ def send_followup (self ) -> Callable [..., Awaitable [ WebhookMessage ]] :
195
+ if self .interaction . response .is_done ():
192
196
return self .followup .send
193
197
else :
194
198
raise RuntimeError (
195
199
f"Interaction was not yet issued a response. Try using { type (self ).__name__ } .respond() first."
196
200
)
197
201
198
202
@property
199
- def defer (self ):
203
+ def defer (self ) -> Callable [..., Awaitable [ None ]] :
200
204
return self .interaction .response .defer
201
205
202
206
@property
203
- def followup (self ):
207
+ def followup (self ) -> Webhook :
204
208
return self .interaction .followup
205
209
206
210
async def delete (self ):
207
211
"""Calls :attr:`~discord.commands.ApplicationContext.respond`.
208
212
If the response is done, then calls :attr:`~discord.commands.ApplicationContext.respond` first."""
209
- if not self .response .is_done ():
213
+ if not self .interaction . response .is_done ():
210
214
await self .defer ()
211
215
212
216
return await self .interaction .delete_original_message ()
213
217
214
218
@property
215
- def edit (self ):
219
+ def edit (self ) -> Callable [..., Awaitable [ InteractionMessage ]] :
216
220
return self .interaction .edit_original_message
217
221
218
222
@property
@@ -249,7 +253,7 @@ class AutocompleteContext:
249
253
250
254
__slots__ = ("bot" , "interaction" , "command" , "focused" , "value" , "options" )
251
255
252
- def __init__ (self , bot : Bot , interaction : Interaction ) -> None :
256
+ def __init__ (self , bot : Bot , interaction : Interaction ):
253
257
self .bot = bot
254
258
self .interaction = interaction
255
259
0 commit comments