diff --git a/interactions/api/gateway.py b/interactions/api/gateway.py index 992ad353b..5430efcd7 100644 --- a/interactions/api/gateway.py +++ b/interactions/api/gateway.py @@ -32,7 +32,6 @@ log = get_logger("gateway") - __all__ = ("_Heartbeat", "WebSocketClient") @@ -621,3 +620,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/client.py b/interactions/client.py index 146c619d6..a67c526c6 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 + async 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, 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