From a9d82cfc1d842ce1264b3573dee88623a7f04462 Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Fri, 11 Mar 2022 18:24:12 -0500 Subject: [PATCH 1/4] feat: Implement changing presences on runtime. --- interactions/api/gateway.py | 19 ++++++++++++++++++- interactions/api/gateway.pyi | 1 + interactions/models/component.py | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/interactions/api/gateway.py b/interactions/api/gateway.py index 992ad353b..4e30a7276 100644 --- a/interactions/api/gateway.py +++ b/interactions/api/gateway.py @@ -32,7 +32,6 @@ log = get_logger("gateway") - __all__ = ("_Heartbeat", "WebSocketClient") @@ -196,6 +195,7 @@ async def _establish_connection( while not self._closed: stream = await self.__receive_packet_stream + print(stream) if stream is None: continue @@ -621,3 +621,20 @@ def shard(self) -> Optional[List[Tuple[int]]]: def presence(self) -> Optional[ClientPresence]: """Returns the current presence.""" return self.__presence + + async def _update_presence(self, presence: ClientPresence) -> None: + """ + Sends an ``UPDATE_PRESENCE`` packet to the gateway. + + .. note:: + There is a ratelimit to using this method (5 per minute). + As there's no gateway ratelimiter yet, breaking this ratelimit + will force your bot to disconnect. + + :param presence: The presence to change the bot to on identify. + :type presence: ClientPresence + """ + payload: dict = {"op": OpCodeType.PRESENCE, "d": presence._json} + await self._send_packet(payload) + log.debug(f"UPDATE_PRESENCE: {presence._json}") + self.__presence = presence diff --git a/interactions/api/gateway.pyi b/interactions/api/gateway.pyi index 6665c76b6..cce247c90 100644 --- a/interactions/api/gateway.pyi +++ b/interactions/api/gateway.pyi @@ -79,3 +79,4 @@ class WebSocketClient: @property def presence(self) -> None: ... async def restart(self): ... + async def _update_presence(self, presence: ClientPresence) -> None: ... diff --git a/interactions/models/component.py b/interactions/models/component.py index 16c6e5106..9bafe9547 100644 --- a/interactions/models/component.py +++ b/interactions/models/component.py @@ -2,7 +2,7 @@ from ..api.error import InteractionException from ..api.models.message import Emoji -from ..api.models.misc import MISSING, DictSerializerMixin +from ..api.models.misc import DictSerializerMixin from ..enums import ButtonStyle, ComponentType, TextStyleType From fa25dc9041dbc8619a2116ed3690f4bec7bbf6a8 Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Fri, 11 Mar 2022 18:35:43 -0500 Subject: [PATCH 2/4] feat: Implement helper method for changing runtime beyond the WebSocket. --- interactions/client.py | 15 +++++++++++++++ interactions/client.pyi | 1 + 2 files changed, 16 insertions(+) diff --git a/interactions/client.py b/interactions/client.py index 146c619d6..535a95d7d 100644 --- a/interactions/client.py +++ b/interactions/client.py @@ -17,6 +17,7 @@ from .api.models.flags import Intents from .api.models.guild import Guild from .api.models.misc import MISSING, Snowflake +from .api.models.presence import ClientPresence from .api.models.team import Application from .base import get_logger from .decor import command @@ -330,6 +331,20 @@ def event(self, coro: Coroutine, name: Optional[str] = MISSING) -> Callable[..., self._websocket._dispatch.register(coro, name if name is not MISSING else coro.__name__) return coro + def change_presence(self, presence: ClientPresence) -> None: + """ + A method that changes the current client's presence on runtime. + + .. note:: + There is a ratelimit to using this method (5 per minute). + As there's no gateway ratelimiter yet, breaking this ratelimit + will force your bot to disconnect. + + :param presence: The presence to change the bot to on identify. + :type presence: ClientPresence + """ + await self._websocket._update_presence(presence) + def __check_command( self, command: ApplicationCommand, diff --git a/interactions/client.pyi b/interactions/client.pyi index 11e22dd9f..12af602a4 100644 --- a/interactions/client.pyi +++ b/interactions/client.pyi @@ -48,6 +48,7 @@ class Client: async def _login(self) -> None: ... async def wait_until_ready(self) -> None: ... def event(self, coro: Coroutine, name: Optional[str] = None) -> Callable[..., Any]: ... + def change_presence(self, presence: ClientPresence) -> None: ... def __check_command( self, command: ApplicationCommand, From 8c483c4310a4163f337b2a6ba2f37693c216b7f4 Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Fri, 11 Mar 2022 18:37:00 -0500 Subject: [PATCH 3/4] chore: Remove print statement. --- interactions/api/gateway.py | 1 - 1 file changed, 1 deletion(-) diff --git a/interactions/api/gateway.py b/interactions/api/gateway.py index 4e30a7276..5430efcd7 100644 --- a/interactions/api/gateway.py +++ b/interactions/api/gateway.py @@ -195,7 +195,6 @@ async def _establish_connection( while not self._closed: stream = await self.__receive_packet_stream - print(stream) if stream is None: continue From 461521618896ce3900bfe4935c1db32cde086a94 Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Fri, 11 Mar 2022 18:41:57 -0500 Subject: [PATCH 4/4] fix!: Change function typo to async syntax. --- interactions/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interactions/client.py b/interactions/client.py index 535a95d7d..a67c526c6 100644 --- a/interactions/client.py +++ b/interactions/client.py @@ -331,7 +331,7 @@ def event(self, coro: Coroutine, name: Optional[str] = MISSING) -> Callable[..., self._websocket._dispatch.register(coro, name if name is not MISSING else coro.__name__) return coro - def change_presence(self, presence: ClientPresence) -> None: + async def change_presence(self, presence: ClientPresence) -> None: """ A method that changes the current client's presence on runtime.