11from asyncio import Task , get_running_loop , sleep
22from functools import wraps
3- from typing import TYPE_CHECKING , Callable , Coroutine , Iterable , List , Optional , TypeVar , Union
3+ from typing import (
4+ TYPE_CHECKING ,
5+ Awaitable ,
6+ Callable ,
7+ Coroutine ,
8+ Iterable ,
9+ List ,
10+ Optional ,
11+ TypeVar ,
12+ Union ,
13+ )
414
515from ..api .error import LibraryException
616from ..client .models .component import ActionRow , Button , Component , SelectMenu
717
818if TYPE_CHECKING :
19+ from ..client .bot import Extension
920 from ..client .context import CommandContext
1021
1122__all__ = ("autodefer" , "spread_to_rows" , "search_iterable" , "disable_components" )
@@ -17,7 +28,7 @@ def autodefer(
1728 delay : Union [float , int ] = 2 ,
1829 ephemeral : bool = False ,
1930 edit_origin : bool = False ,
20- ) -> Callable [[Callable [..., Coroutine ]], Callable [..., Coroutine ]]:
31+ ) -> Callable [[Callable [..., Union [ Awaitable , Coroutine ]]] , Callable [..., Awaitable ]]:
2132 """
2233 A decorator that automatically defers a command if it did not respond within ``delay`` seconds.
2334
@@ -41,16 +52,26 @@ async def command(ctx):
4152 :rtype:
4253 """
4354
44- def decorator (coro : Callable [..., Coroutine ]) -> Callable [..., Coroutine ]:
55+ def decorator (coro : Callable [..., Union [ Awaitable , Coroutine ]] ) -> Callable [..., Awaitable ]:
4556 from ..client .context import ComponentContext
4657
4758 @wraps (coro )
48- async def deferring_func (ctx : Union ["CommandContext" , "ComponentContext" ], * args , ** kwargs ):
59+ async def deferring_func (
60+ ctx : Union ["CommandContext" , "ComponentContext" , "Extension" ], * args , ** kwargs
61+ ):
4962 try :
5063 loop = get_running_loop ()
5164 except RuntimeError as e :
5265 raise RuntimeError ("No running event loop detected!" ) from e
53- task : Task = loop .create_task (coro (ctx , * args , ** kwargs ))
66+
67+ if isinstance (args [0 ], (ComponentContext , CommandContext )):
68+ self = ctx
69+ ctx = list (args ).pop (0 )
70+
71+ task : Task = loop .create_task (coro (self , ctx , * args , ** kwargs ))
72+
73+ else :
74+ task : Task = loop .create_task (coro (ctx , * args , ** kwargs ))
5475
5576 await sleep (delay )
5677
0 commit comments