Skip to content

Commit 2036257

Browse files
Lumabotspre-commit-ci[bot]plun1331Dorukyum
authored andcommitted
feat: allow conversion to Member in MentionableConverter (Pycord-Development#2775)
* Update CHANGELOG.md Signed-off-by: Lumouille <[email protected]> * fix member not being converted Signed-off-by: Lumouille <[email protected]> * fix member not being converted Signed-off-by: Lumouille <[email protected]> * style(pre-commit): auto fixes from pre-commit.com hooks * Update CHANGELOG.md Signed-off-by: Lumouille <[email protected]> * Update CHANGELOG.md Signed-off-by: Lumouille <[email protected]> * Update CHANGELOG.md Co-authored-by: plun1331 <[email protected]> Signed-off-by: Lumouille <[email protected]> * refactor(bridge): remove redundant constructor in BridgeOption * feat(bridge): enhance BridgeOption initialization with converter support * Update discord/ext/bridge/core.py Co-authored-by: Dorukyum <[email protected]> Signed-off-by: Lumouille <[email protected]> * improved code quality --------- Signed-off-by: Lumouille <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: plun1331 <[email protected]> Co-authored-by: Dorukyum <[email protected]> (cherry picked from commit a82f562)
1 parent 4f3a729 commit 2036257

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ These changes are available on the `master` branch, but have not yet been releas
5555
([#2714](https://github.com/Pycord-Development/pycord/pull/2714))
5656
- Added the ability to pass a `datetime.time` object to `format_dt`.
5757
([#2747](https://github.com/Pycord-Development/pycord/pull/2747))
58+
- Added conversion to `Member` in `MentionableConverter`.
59+
([#2775](https://github.com/Pycord-Development/pycord/pull/2775))
5860
- Added `discord.Interaction.created_at`.
5961
([#2801](https://github.com/Pycord-Development/pycord/pull/2801))
6062

discord/ext/bridge/core.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
Converter,
5252
Group,
5353
GuildChannelConverter,
54+
MemberConverter,
5455
RoleConverter,
5556
UserConverter,
5657
)
@@ -553,13 +554,21 @@ def predicate(func: Callable | ApplicationCommand):
553554

554555

555556
class MentionableConverter(Converter):
556-
"""A converter that can convert a mention to a user or a role."""
557+
"""A converter that can convert a mention to a member, a user or a role."""
557558

558559
async def convert(self, ctx, argument):
559560
try:
560561
return await RoleConverter().convert(ctx, argument)
561562
except BadArgument:
562-
return await UserConverter().convert(ctx, argument)
563+
pass
564+
565+
if ctx.guild:
566+
try:
567+
return await MemberConverter().convert(ctx, argument)
568+
except BadArgument:
569+
pass
570+
571+
return await UserConverter().convert(ctx, argument)
563572

564573

565574
class AttachmentConverter(Converter):
@@ -587,6 +596,7 @@ async def convert(self, ctx, arg: bool):
587596
SlashCommandOptionType.mentionable: MentionableConverter,
588597
SlashCommandOptionType.number: float,
589598
SlashCommandOptionType.attachment: AttachmentConverter,
599+
discord.Member: MemberConverter,
590600
}
591601

592602

@@ -595,16 +605,24 @@ class BridgeOption(Option, Converter):
595605
command option and a prefixed command argument for bridge commands.
596606
"""
597607

608+
def __init__(self, input_type, *args, **kwargs):
609+
self.converter = kwargs.pop("converter", None)
610+
super().__init__(input_type, *args, **kwargs)
611+
612+
self.converter = self.converter or BRIDGE_CONVERTER_MAPPING.get(input_type)
613+
598614
async def convert(self, ctx, argument: str) -> Any:
599615
try:
600616
if self.converter is not None:
601-
converted = await self.converter.convert(ctx, argument)
617+
converted = await self.converter().convert(ctx, argument)
602618
else:
603-
converter = BRIDGE_CONVERTER_MAPPING[self.input_type]
604-
if issubclass(converter, Converter):
619+
converter = BRIDGE_CONVERTER_MAPPING.get(self.input_type)
620+
if isinstance(converter, type) and issubclass(converter, Converter):
605621
converted = await converter().convert(ctx, argument) # type: ignore # protocol class
606-
else:
622+
elif callable(converter):
607623
converted = converter(argument)
624+
else:
625+
raise TypeError(f"Invalid converter: {converter}")
608626

609627
if self.choices:
610628
choices_names: list[str | int | float] = [choice.name for choice in self.choices]

0 commit comments

Comments
 (0)