Skip to content

Commit 369e44d

Browse files
MiddledotDorukyumEmmmaTechLulalabyBobDotCom
authored
ext.bridge improvements (#1496)
Co-Authored-By: Dorukyum <[email protected]> Co-Authored-By: Emre Terzioglu <[email protected]> Co-authored-by: Lala Sabathil <[email protected]> Co-authored-by: BobDotCom <[email protected]>
1 parent 413d012 commit 369e44d

File tree

9 files changed

+344
-97
lines changed

9 files changed

+344
-97
lines changed

discord/commands/core.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def __new__(cls, *args, **kwargs) -> SlashCommand:
619619
self.__original_kwargs__ = kwargs.copy()
620620
return self
621621

622-
def __init__(self, func: Callable, **kwargs) -> None:
622+
def __init__(self, func: Callable, *args, **kwargs) -> None:
623623
super().__init__(func, **kwargs)
624624
if not asyncio.iscoroutinefunction(func):
625625
raise TypeError("Callback must be a coroutine.")
@@ -1016,10 +1016,10 @@ def __init__(
10161016
parent: Optional[SlashCommandGroup] = None,
10171017
**kwargs,
10181018
) -> None:
1019-
self.name = name
1020-
self.description = description or "No description provided"
1021-
validate_chat_input_name(self.name)
1022-
validate_chat_input_description(self.description)
1019+
validate_chat_input_name(name)
1020+
validate_chat_input_description(description)
1021+
self.name = str(name)
1022+
self.description = description
10231023
self.input_type = SlashCommandOptionType.sub_command_group
10241024
self.subcommands: List[Union[SlashCommand, SlashCommandGroup]] = self.__initial_commands__
10251025
self.guild_ids = guild_ids
@@ -1065,9 +1065,9 @@ def to_dict(self) -> Dict:
10651065

10661066
return as_dict
10671067

1068-
def command(self, **kwargs) -> Callable[[Callable], SlashCommand]:
1069-
def wrap(func) -> SlashCommand:
1070-
command = SlashCommand(func, parent=self, **kwargs)
1068+
def command(self, cls: Type[T] = SlashCommand, **kwargs) -> Callable[[Callable], SlashCommand]:
1069+
def wrap(func) -> T:
1070+
command = cls(func, parent=self, **kwargs)
10711071
self.subcommands.append(command)
10721072
return command
10731073

discord/ext/bridge/bot.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from ..commands import AutoShardedBot as ExtAutoShardedBot
3131
from ..commands import Bot as ExtBot
3232
from .context import BridgeApplicationContext, BridgeExtContext
33-
from .core import BridgeCommand, bridge_command
33+
from .core import BridgeCommand, BridgeCommandGroup, bridge_command, bridge_group
3434

3535
__all__ = ("Bot", "AutoShardedBot")
3636

@@ -75,6 +75,22 @@ def decorator(func) -> BridgeCommand:
7575

7676
return decorator
7777

78+
def bridge_group(self, **kwargs):
79+
"""A decorator that is used to wrap a function as a bridge command group.
80+
81+
Parameters
82+
----------
83+
kwargs: Optional[Dict[:class:`str`, Any]]
84+
Keyword arguments that are directly passed to the respective command constructors. (:class:`.SlashCommandGroup` and :class:`.ext.commands.Group`)
85+
"""
86+
87+
def decorator(func) -> BridgeCommandGroup:
88+
result = bridge_group(**kwargs)(func)
89+
self.add_bridge_command(result)
90+
return result
91+
92+
return decorator
93+
7894

7995
class Bot(BotBase, ExtBot):
8096
"""Represents a discord bot, with support for cross-compatibility between command types.

discord/ext/bridge/context.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class BridgeContext(ABC):
5050
5151
@bot.bridge_command()
5252
async def example(ctx: BridgeContext):
53-
if isinstance(ctx, BridgeExtContext):
54-
command_type = "Traditional (prefix-based) command"
55-
elif isinstance(ctx, BridgeApplicationContext):
53+
if ctx.is_app:
5654
command_type = "Application command"
55+
else:
56+
command_type = "Traditional (prefix-based) command"
5757
await ctx.send(f"This command was invoked with a(n) {command_type}.")
5858
5959
.. versionadded:: 2.0
@@ -118,6 +118,11 @@ async def edit(self, *args, **kwargs) -> Union[InteractionMessage, Message]:
118118
def _get_super(self, attr: str) -> Any:
119119
return getattr(super(), attr)
120120

121+
@property
122+
def is_app(self) -> bool:
123+
"""bool: Whether the context is an :class:`BridgeApplicationContext` or not."""
124+
return isinstance(self, BridgeApplicationContext)
125+
121126

122127
class BridgeApplicationContext(BridgeContext, ApplicationContext):
123128
"""

0 commit comments

Comments
 (0)