|
24 | 24 | """
|
25 | 25 | from __future__ import annotations
|
26 | 26 |
|
27 |
| -from typing import TYPE_CHECKING, Optional, Union |
| 27 | +from typing import Callable, TYPE_CHECKING, Optional, TypeVar, Union |
28 | 28 |
|
29 | 29 | import discord.abc
|
30 | 30 |
|
31 | 31 | if TYPE_CHECKING:
|
| 32 | + from typing_extensions import ParamSpec |
| 33 | + |
32 | 34 | import discord
|
33 | 35 | from discord import Bot
|
34 | 36 | from discord.state import ConnectionState
|
|
42 | 44 | from ..message import Message
|
43 | 45 | from ..user import User
|
44 | 46 | from ..utils import cached_property
|
| 47 | +from ..webhook import Webhook |
| 48 | + |
| 49 | +T = TypeVar('T') |
| 50 | +CogT = TypeVar('CogT', bound="Cog") |
| 51 | + |
| 52 | +if TYPE_CHECKING: |
| 53 | + P = ParamSpec('P') |
| 54 | +else: |
| 55 | + P = TypeVar('P') |
45 | 56 |
|
46 | 57 | __all__ = (
|
47 | 58 | "ApplicationContext",
|
@@ -81,6 +92,32 @@ def __init__(self, bot: Bot, interaction: Interaction):
|
81 | 92 | async def _get_channel(self) -> discord.abc.Messageable:
|
82 | 93 | return self.channel
|
83 | 94 |
|
| 95 | + async def invoke(self, command: ApplicationCommand[CogT, P, T], /, *args: P.args, **kwargs: P.kwargs) -> T: |
| 96 | + r"""|coro| |
| 97 | + Calls a command with the arguments given. |
| 98 | + This is useful if you want to just call the callback that a |
| 99 | + :class:`.ApplicationCommand` holds internally. |
| 100 | + .. note:: |
| 101 | + This does not handle converters, checks, cooldowns, pre-invoke, |
| 102 | + or after-invoke hooks in any matter. It calls the internal callback |
| 103 | + directly as-if it was a regular function. |
| 104 | + You must take care in passing the proper arguments when |
| 105 | + using this function. |
| 106 | + Parameters |
| 107 | + ----------- |
| 108 | + command: :class:`.ApplicationCommand` |
| 109 | + The command that is going to be called. |
| 110 | + \*args |
| 111 | + The arguments to use. |
| 112 | + \*\*kwargs |
| 113 | + The keyword arguments to use. |
| 114 | + Raises |
| 115 | + ------- |
| 116 | + TypeError |
| 117 | + The command argument to invoke is missing. |
| 118 | + """ |
| 119 | + return await command(self, *args, **kwargs) |
| 120 | + |
84 | 121 | @cached_property
|
85 | 122 | def channel(self):
|
86 | 123 | return self.interaction.channel
|
|
0 commit comments