Skip to content

Commit 59a39e9

Browse files
authored
Integrate Float and Mentionable enum + Add ctx.invoke() (#273)
1 parent 2e94673 commit 59a39e9

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

discord_slash/context.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,39 @@ def cog(self) -> typing.Optional[commands.Cog]:
392392
else:
393393
return None
394394

395+
async def invoke(self, *args, **kwargs):
396+
"""
397+
Invokes a command with the arguments given.\n
398+
Similar to d.py's `ctx.invoke` function and documentation.\n
399+
400+
.. note::
401+
402+
This does not handle converters, checks, cooldowns, pre-invoke,
403+
or after-invoke hooks in any matter. It calls the internal callback
404+
directly as-if it was a regular function.
405+
406+
You must take care in passing the proper arguments when
407+
using this function.
408+
409+
.. warning::
410+
The first parameter passed **must** be the command being invoked.
411+
While using `ctx.defer`, if the command invoked includes usage of that command, do not invoke
412+
`ctx.defer` before calling this function. It can not defer twice.
413+
414+
:param args: Args for the command.
415+
:param kwargs: Keyword args for the command.
416+
417+
:raises: :exc:`TypeError`
418+
"""
419+
420+
try:
421+
command = args[0]
422+
except IndexError:
423+
raise TypeError("Missing command to invoke.") from None
424+
425+
ret = await self.slash.invoke_command(func=command, ctx=self, args=kwargs)
426+
return ret
427+
395428

396429
class ComponentContext(InteractionContext):
397430
"""

discord_slash/model.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import datetime
3+
import typing
34
from contextlib import suppress
45
from enum import IntEnum
56
from inspect import iscoroutinefunction
@@ -456,6 +457,8 @@ class SlashCommandOptionType(IntEnum):
456457
USER = 6
457458
CHANNEL = 7
458459
ROLE = 8
460+
MENTIONABLE = 9
461+
FLOAT = 10
459462

460463
@classmethod
461464
def from_type(cls, t: type):
@@ -465,6 +468,7 @@ def from_type(cls, t: type):
465468
:param t: The type or object to get a SlashCommandOptionType for.
466469
:return: :class:`.model.SlashCommandOptionType` or ``None``
467470
"""
471+
468472
if issubclass(t, str):
469473
return cls.STRING
470474
if issubclass(t, bool):
@@ -478,6 +482,16 @@ def from_type(cls, t: type):
478482
return cls.CHANNEL
479483
if issubclass(t, discord.abc.Role):
480484
return cls.ROLE
485+
# Here's the issue. Typechecking for a **Union** somewhat differs per version (from 3.6.8+)
486+
if (
487+
hasattr(typing, "_GenericAlias")
488+
and isinstance(t, typing._UnionGenericAlias) # noqa
489+
or not hasattr(typing, "_GenericAlias")
490+
and isinstance(t, typing._Union) # noqa
491+
):
492+
return cls.MENTIONABLE
493+
if issubclass(t, float):
494+
return cls.FLOAT
481495

482496

483497
class SlashMessage(ComponentMessage):

0 commit comments

Comments
 (0)