Skip to content

Commit a82f562

Browse files
Lumabotspre-commit-ci[bot]plun1331Dorukyum
authored
feat: allow conversion to Member in MentionableConverter (#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]>
1 parent beadea9 commit a82f562

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
)
@@ -566,13 +567,21 @@ def predicate(func: Callable | ApplicationCommand):
566567

567568

568569
class MentionableConverter(Converter):
569-
"""A converter that can convert a mention to a user or a role."""
570+
"""A converter that can convert a mention to a member, a user or a role."""
570571

571572
async def convert(self, ctx, argument):
572573
try:
573574
return await RoleConverter().convert(ctx, argument)
574575
except BadArgument:
575-
return await UserConverter().convert(ctx, argument)
576+
pass
577+
578+
if ctx.guild:
579+
try:
580+
return await MemberConverter().convert(ctx, argument)
581+
except BadArgument:
582+
pass
583+
584+
return await UserConverter().convert(ctx, argument)
576585

577586

578587
class AttachmentConverter(Converter):
@@ -600,6 +609,7 @@ async def convert(self, ctx, arg: bool):
600609
SlashCommandOptionType.mentionable: MentionableConverter,
601610
SlashCommandOptionType.number: float,
602611
SlashCommandOptionType.attachment: AttachmentConverter,
612+
discord.Member: MemberConverter,
603613
}
604614

605615

@@ -608,16 +618,24 @@ class BridgeOption(Option, Converter):
608618
command option and a prefixed command argument for bridge commands.
609619
"""
610620

621+
def __init__(self, input_type, *args, **kwargs):
622+
self.converter = kwargs.pop("converter", None)
623+
super().__init__(input_type, *args, **kwargs)
624+
625+
self.converter = self.converter or BRIDGE_CONVERTER_MAPPING.get(input_type)
626+
611627
async def convert(self, ctx, argument: str) -> Any:
612628
try:
613629
if self.converter is not None:
614-
converted = await self.converter.convert(ctx, argument)
630+
converted = await self.converter().convert(ctx, argument)
615631
else:
616-
converter = BRIDGE_CONVERTER_MAPPING[self.input_type]
617-
if issubclass(converter, Converter):
632+
converter = BRIDGE_CONVERTER_MAPPING.get(self.input_type)
633+
if isinstance(converter, type) and issubclass(converter, Converter):
618634
converted = await converter().convert(ctx, argument) # type: ignore # protocol class
619-
else:
635+
elif callable(converter):
620636
converted = converter(argument)
637+
else:
638+
raise TypeError(f"Invalid converter: {converter}")
621639

622640
if self.choices:
623641
choices_names: list[str | int | float] = [

0 commit comments

Comments
 (0)