Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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))

### Fixed

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 @@ -580,13 +581,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 or a role, preferring Member in guilds."""

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 @@ -614,6 +623,7 @@ async def convert(self, ctx, arg: bool):
SlashCommandOptionType.mentionable: MentionableConverter,
SlashCommandOptionType.number: float,
SlashCommandOptionType.attachment: AttachmentConverter,
discord.Member: MemberConverter,
}


Expand All @@ -622,16 +632,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