Skip to content

Commit

Permalink
refactor!: make user parsers use internal int parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharp-Eyes committed Sep 21, 2024
1 parent fe6c874 commit 545d14a
Showing 1 changed file with 139 additions and 61 deletions.
200 changes: 139 additions & 61 deletions src/disnake/ext/components/impl/parser/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import disnake
from disnake.ext.components.impl.parser import base as parser_base
from disnake.ext.components.impl.parser import helpers, snowflake
from disnake.ext.components.impl.parser import builtins as builtins_parsers
from disnake.ext.components.impl.parser import helpers

__all__: typing.Sequence[str] = (
"GetUserParser",
Expand All @@ -20,31 +21,66 @@
class GetUserParser(parser_base.SourcedParser[disnake.User]): # noqa: D101
# <<docstring inherited from parser_api.Parser>>

def __init__(self) -> None:
super().__init__()
self.dumps = snowflake.snowflake_dumps
int_parser: builtins_parsers.IntParser
allow_fallback: bool

def __init__(
self,
int_parser: typing.Optional[builtins_parsers.IntParser] = None,
*,
allow_fallback: bool = False,
):
self.int_parser = int_parser or builtins_parsers.IntParser.default()
self.allow_fallback = allow_fallback

def loads( # noqa: D102
self, argument: str, *, source: helpers.BotAware
self,
argument: str,
*,
source: typing.Union[helpers.BotAware, helpers.AuthorAware],
) -> disnake.User:
# <<docstring inherited from parser_api.Parser>>

user = source.bot.get_user(int(argument))
user_id = self.int_parser.loads(argument)
if isinstance(source, helpers.BotAware):
user = source.bot.get_user(user_id)
if user:
return user

# First, validate that the source author is a member.
# If allow_fallback is True, return the source member regardless of
# whether the id is correct. Otherwise, validate the id.
if (
isinstance(source, helpers.AuthorAware)
and isinstance(source.author, disnake.User)
and (self.allow_fallback or source.author.id == user_id)
):
return source.author

msg = f"Could not find a user with id {argument!r}."
raise LookupError(msg)

if user is None:
msg = f"Could not find a user with id {argument!r}."
raise LookupError(msg)
def dumps(self, argument: disnake.User) -> str: # noqa: D102
# <<docstring inherited from parser_api.Parser>>

return user
return self.int_parser.dumps(argument.id)


@parser_base.register_parser_for(disnake.Member)
class GetMemberParser(parser_base.SourcedParser[disnake.Member]): # noqa: D101
# <<docstring inherited from parser_api.Parser>>

def __init__(self) -> None:
super().__init__()
self.dumps = snowflake.snowflake_dumps
int_parser: builtins_parsers.IntParser
allow_fallback: bool

def __init__(
self,
int_parser: typing.Optional[builtins_parsers.IntParser] = None,
*,
allow_fallback: bool = False,
):
self.int_parser = int_parser or builtins_parsers.IntParser.default()
self.allow_fallback = allow_fallback

def loads( # noqa: D102
self,
Expand All @@ -54,58 +90,101 @@ def loads( # noqa: D102
helpers.GuildAware,
helpers.MessageAware,
helpers.ChannelAware,
helpers.AuthorAware,
],
) -> disnake.Member:
# <<docstring inherited from parser_api.Parser>>

guild = None
if isinstance(source, helpers.GuildAware):
guild = source.guild

if guild is None and isinstance(source, helpers.MessageAware):
guild = source.message.guild

if guild is None:
msg = (
"Impossible to fetch a role from an"
" interaction that doesn't come from a guild."
)
raise TypeError(msg)
guild = helpers.get_guild_from_source(source)
member_id = self.int_parser.loads(argument)

member = guild.get_member(int(argument))
if member is not None:
member = guild.get_member(member_id)
if member:
return member

# First, validate that the source author is a member.
# If allow_fallback is True, return the source member regardless of
# whether the id is correct. Otherwise, validate the id.
if (
isinstance(source, helpers.AuthorAware)
and isinstance(source.author, disnake.Member)
and (self.allow_fallback or source.author.id == member_id)
):
return source.author

msg = f"Could not find a member with id {argument!r}."
raise LookupError(msg)

def dumps(self, argument: disnake.Member) -> str: # noqa: D102
# <<docstring inherited from parser_api.Parser>>

return self.int_parser.dumps(argument.id)


@parser_base.register_parser_for(disnake.User)
class UserParser(parser_base.SourcedParser[disnake.User]): # noqa: D101
# <<docstring inherited from parser_api.Parser>>

def __init__(self) -> None:
super().__init__()
self.dumps = snowflake.snowflake_dumps
int_parser: builtins_parsers.IntParser
allow_fallback: bool

def __init__(
self,
int_parser: typing.Optional[builtins_parsers.IntParser] = None,
*,
allow_fallback: bool = False,
):
self.int_parser = int_parser or builtins_parsers.IntParser.default()
self.allow_fallback = allow_fallback

async def loads( # noqa: D102
self, argument: str, *, source: helpers.BotAware
self,
argument: str,
*,
source: typing.Union[helpers.BotAware, helpers.AuthorAware],
) -> disnake.User:
# <<docstring inherited from parser_api.Parser>>

return (
source.bot.get_user(int(argument))
or await source.bot.fetch_user(int(argument))
) # fmt: skip
user_id = self.int_parser.loads(argument)
if isinstance(source, helpers.BotAware):
user = source.bot.get_user(user_id)
if user:
return user

try:
return await source.bot.fetch_user(user_id)
except disnake.HTTPException:
pass

# First, validate that the source author is a member.
# If allow_fallback is True, return the source member regardless of
# whether the id is correct. Otherwise, validate the id.
if (
isinstance(source, helpers.AuthorAware)
and isinstance(source.author, disnake.User)
and (self.allow_fallback or source.author.id == user_id)
):
return source.author

msg = f"Could not find a user with id {argument!r}."
raise LookupError(msg)


@parser_base.register_parser_for(disnake.Member)
class MemberParser(parser_base.SourcedParser[disnake.Member]): # noqa: D101
# <<docstring inherited from parser_api.Parser>>

def __init__(self) -> None:
super().__init__()
self.dumps = snowflake.snowflake_dumps
int_parser: builtins_parsers.IntParser
allow_fallback: bool

def __init__(
self,
int_parser: typing.Optional[builtins_parsers.IntParser] = None,
*,
allow_fallback: bool = False,
):
self.int_parser = int_parser or builtins_parsers.IntParser.default()
self.allow_fallback = allow_fallback

async def loads( # noqa: D102
self,
Expand All @@ -115,33 +194,32 @@ async def loads( # noqa: D102
helpers.GuildAware,
helpers.MessageAware,
helpers.ChannelAware,
helpers.AuthorAware,
],
) -> disnake.Member:
# <<docstring inherited from parser_api.Parser>>

guild = None
if isinstance(source, helpers.GuildAware):
guild = source.guild
guild = helpers.get_guild_from_source(source)

if guild is None and isinstance(source, helpers.MessageAware):
guild = source.message.guild
member_id = self.int_parser.loads(argument)
member = guild.get_member(member_id)
if member:
return member

try:
return await guild.fetch_member(member_id)
except disnake.HTTPException:
pass

# First, validate that the source author is a member.
# If allow_fallback is True, return the source member regardless of
# whether the id is correct. Otherwise, validate the id.
if (
guild is None
and isinstance(source, helpers.ChannelAware)
and isinstance(source.channel, helpers.GuildAware)
isinstance(source, helpers.AuthorAware)
and isinstance(source.author, disnake.Member)
and (self.allow_fallback or source.author.id == member_id)
):
guild = source.channel.guild

if guild is None:
msg = (
"Impossible to fetch a member from an"
" interaction that doesn't come from a guild."
)
raise TypeError(msg)

id_ = int(argument)
return (
guild.get_member(id_)
or await guild.fetch_member(id_)
) # fmt: skip
return source.author

msg = f"Could not find a member with id {argument!r}."
raise LookupError(msg)

0 comments on commit 545d14a

Please sign in to comment.