From e7d1bef9762a9600876deeb30b2d81ef0a25b0c0 Mon Sep 17 00:00:00 2001 From: Damego Date: Tue, 6 Dec 2022 18:51:28 +0500 Subject: [PATCH 1/2] refactor: add missed http clients and some optimizing --- interactions/api/gateway/ratelimit.py | 2 + interactions/api/models/gw.py | 12 ++---- interactions/client/context.py | 57 +++++++++++++-------------- 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/interactions/api/gateway/ratelimit.py b/interactions/api/gateway/ratelimit.py index 01e30466f..57125b0bf 100644 --- a/interactions/api/gateway/ratelimit.py +++ b/interactions/api/gateway/ratelimit.py @@ -6,6 +6,8 @@ log = logging.getLogger("gateway.ratelimit") +__all__ = ("WSRateLimit", ) + class WSRateLimit: """ diff --git a/interactions/api/models/gw.py b/interactions/api/models/gw.py index a56412723..bdf123c27 100644 --- a/interactions/api/models/gw.py +++ b/interactions/api/models/gw.py @@ -132,7 +132,7 @@ class ApplicationCommandPermission(DictSerializerMixin): .. code-block:: python - interactions.Permission( + interactions.ApplicationCommandPermission( id=1234567890, type=interactions.PermissionType.USER, permission=True, @@ -147,9 +147,6 @@ class ApplicationCommandPermission(DictSerializerMixin): type: PermissionType = field(converter=PermissionType) permission: bool = field() - def __attrs_post_init__(self): - self._json["type"] = self.type.value - @define() class ApplicationCommandPermissions(ClientSerializerMixin, IDMixin): @@ -219,7 +216,7 @@ class GuildBan(ClientSerializerMixin): """ guild_id: Snowflake = field(converter=Snowflake) - user: User = field(converter=User) + user: User = field(converter=User, add_client=True) @define() @@ -232,7 +229,7 @@ class GuildEmojis(ClientSerializerMixin): """ guild_id: Snowflake = field(converter=Snowflake) - emojis: List[Emoji] = field(converter=convert_list(Emoji)) + emojis: List[Emoji] = field(converter=convert_list(Emoji), add_client=True) @define() @@ -330,9 +327,8 @@ class GuildRole(ClientSerializerMixin): """ guild_id: Snowflake = field(converter=Snowflake) - role: Optional[Role] = field(converter=Role, default=None) + role: Optional[Role] = field(converter=Role, add_client=True, default=None) role_id: Optional[Snowflake] = field(converter=Snowflake, default=None) - guild_hashes = field() # TODO: investigate what this is. @define() diff --git a/interactions/client/context.py b/interactions/client/context.py index 16cb549c5..1ba4cedc1 100644 --- a/interactions/client/context.py +++ b/interactions/client/context.py @@ -651,39 +651,36 @@ async def populate(self, choices: Union[Choice, List[Choice]]) -> List[Choice]: :rtype: List[Choice] """ - async def func(): - _choices: Union[list, None] = [] - - if not choices or (isinstance(choices, list) and len(choices) == 0): - _choices = None - elif isinstance(choices, Choice): - _choices.append(choices._json) - elif isinstance(choices, list) and all( - isinstance(choice, Choice) for choice in choices - ): - _choices = [choice._json for choice in choices] - elif all( - isinstance(choice, dict) and all(isinstance(x, str) for x in choice) - for choice in choices - ): - _choices = list(choices) - else: - raise LibraryException( - 6, message="Autocomplete choice items must be of type Choice" - ) - - await self._client.create_interaction_response( - token=self.token, - application_id=int(self.id), - data={ - "type": InteractionCallbackType.APPLICATION_COMMAND_AUTOCOMPLETE_RESULT.value, - "data": {"choices": _choices}, - }, + _choices: Union[list, None] = [] + + if not choices or (isinstance(choices, list) and len(choices) == 0): + _choices = None + elif isinstance(choices, Choice): + _choices.append(choices._json) + elif isinstance(choices, list) and all( + isinstance(choice, Choice) for choice in choices + ): + _choices = [choice._json for choice in choices] + elif all( + isinstance(choice, dict) and all(isinstance(x, str) for x in choice) + for choice in choices + ): + _choices = list(choices) + else: + raise LibraryException( + 6, message="Autocomplete choice items must be of type Choice" ) - return _choices + await self._client.create_interaction_response( + token=self.token, + application_id=int(self.id), + data={ + "type": InteractionCallbackType.APPLICATION_COMMAND_AUTOCOMPLETE_RESULT.value, + "data": {"choices": _choices}, + }, + ) - return await func() + return _choices @define() From 35ad98e1bf11937ba0497a205ce935aa0167a37a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 Dec 2022 14:07:10 +0000 Subject: [PATCH 2/2] ci: correct from checks. --- interactions/api/gateway/ratelimit.py | 2 +- interactions/client/context.py | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/interactions/api/gateway/ratelimit.py b/interactions/api/gateway/ratelimit.py index 57125b0bf..2e538eefd 100644 --- a/interactions/api/gateway/ratelimit.py +++ b/interactions/api/gateway/ratelimit.py @@ -6,7 +6,7 @@ log = logging.getLogger("gateway.ratelimit") -__all__ = ("WSRateLimit", ) +__all__ = ("WSRateLimit",) class WSRateLimit: diff --git a/interactions/client/context.py b/interactions/client/context.py index 1ba4cedc1..164f956a3 100644 --- a/interactions/client/context.py +++ b/interactions/client/context.py @@ -657,9 +657,7 @@ async def populate(self, choices: Union[Choice, List[Choice]]) -> List[Choice]: _choices = None elif isinstance(choices, Choice): _choices.append(choices._json) - elif isinstance(choices, list) and all( - isinstance(choice, Choice) for choice in choices - ): + elif isinstance(choices, list) and all(isinstance(choice, Choice) for choice in choices): _choices = [choice._json for choice in choices] elif all( isinstance(choice, dict) and all(isinstance(x, str) for x in choice) @@ -667,9 +665,7 @@ async def populate(self, choices: Union[Choice, List[Choice]]) -> List[Choice]: ): _choices = list(choices) else: - raise LibraryException( - 6, message="Autocomplete choice items must be of type Choice" - ) + raise LibraryException(6, message="Autocomplete choice items must be of type Choice") await self._client.create_interaction_response( token=self.token,