Skip to content

feat: allow conversion to Member in MentionableConverter #2775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2714](https://github.com/Pycord-Development/pycord/pull/2714))
- Added the ability to pass a `datetime.time` object to `format_dt`.
([#2747](https://github.com/Pycord-Development/pycord/pull/2747))
- Added conversion to `Member` in `MentionableConverter`.
([#2775](https://github.com/Pycord-Development/pycord/pull/2775))
- Added `discord.Interaction.created_at`.
([#2801](https://github.com/Pycord-Development/pycord/pull/2801))

Expand Down
32 changes: 27 additions & 5 deletions discord/ext/bridge/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
Converter,
Group,
GuildChannelConverter,
MemberConverter,
RoleConverter,
UserConverter,
)
Expand Down Expand Up @@ -566,13 +567,21 @@ def predicate(func: Callable | ApplicationCommand):


class MentionableConverter(Converter):
"""A converter that can convert a mention to a user or a role."""
"""A converter that can convert a mention to a member, a user or a role."""

async def convert(self, ctx, argument):
try:
return await RoleConverter().convert(ctx, argument)
except BadArgument:
return await UserConverter().convert(ctx, argument)
pass

if ctx.guild:
try:
return await MemberConverter().convert(ctx, argument)
except BadArgument:
pass

return await UserConverter().convert(ctx, argument)


class AttachmentConverter(Converter):
Expand Down Expand Up @@ -600,6 +609,7 @@ async def convert(self, ctx, arg: bool):
SlashCommandOptionType.mentionable: MentionableConverter,
SlashCommandOptionType.number: float,
SlashCommandOptionType.attachment: AttachmentConverter,
discord.Member: MemberConverter,
}


Expand All @@ -608,16 +618,28 @@ class BridgeOption(Option, Converter):
command option and a prefixed command argument for bridge commands.
"""

def __init__(self, input_type, *args, **kwargs):
self.converter = kwargs.pop("converter", None)
super().__init__(input_type, *args, **kwargs)

if self.converter is None:
if input_type == discord.Member:
self.converter = MemberConverter()
else:
self.converter = BRIDGE_CONVERTER_MAPPING.get(input_type)

async def convert(self, ctx, argument: str) -> Any:
try:
if self.converter is not None:
converted = await self.converter.convert(ctx, argument)
else:
converter = BRIDGE_CONVERTER_MAPPING[self.input_type]
if issubclass(converter, Converter):
converter = BRIDGE_CONVERTER_MAPPING.get(self.input_type)
if isinstance(converter, type) and issubclass(converter, Converter):
converted = await converter().convert(ctx, argument) # type: ignore # protocol class
else:
elif callable(converter):
converted = converter(argument)
else:
raise TypeError(f"Invalid converter: {converter}")

if self.choices:
choices_names: list[str | int | float] = [
Expand Down