Skip to content

Commit 74e742a

Browse files
authored
feat: Implement changing presences on runtime. (#637)
* feat: Implement changing presences on runtime. * feat: Implement helper method for changing runtime beyond the WebSocket. * chore: Remove print statement. * fix!: Change function typo to async syntax.
1 parent e37ee3a commit 74e742a

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

interactions/api/gateway.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
log = get_logger("gateway")
3434

35-
3635
__all__ = ("_Heartbeat", "WebSocketClient")
3736

3837

@@ -612,3 +611,20 @@ def shard(self) -> Optional[List[Tuple[int]]]:
612611
def presence(self) -> Optional[ClientPresence]:
613612
"""Returns the current presence."""
614613
return self.__presence
614+
615+
async def _update_presence(self, presence: ClientPresence) -> None:
616+
"""
617+
Sends an ``UPDATE_PRESENCE`` packet to the gateway.
618+
619+
.. note::
620+
There is a ratelimit to using this method (5 per minute).
621+
As there's no gateway ratelimiter yet, breaking this ratelimit
622+
will force your bot to disconnect.
623+
624+
:param presence: The presence to change the bot to on identify.
625+
:type presence: ClientPresence
626+
"""
627+
payload: dict = {"op": OpCodeType.PRESENCE, "d": presence._json}
628+
await self._send_packet(payload)
629+
log.debug(f"UPDATE_PRESENCE: {presence._json}")
630+
self.__presence = presence

interactions/api/gateway.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@ class WebSocketClient:
7979
@property
8080
def presence(self) -> None: ...
8181
async def restart(self): ...
82+
async def _update_presence(self, presence: ClientPresence) -> None: ...

interactions/client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .api.models.flags import Intents
1818
from .api.models.guild import Guild
1919
from .api.models.misc import MISSING, Snowflake
20+
from .api.models.presence import ClientPresence
2021
from .api.models.team import Application
2122
from .base import get_logger
2223
from .decor import command
@@ -335,6 +336,20 @@ def event(self, coro: Coroutine, name: Optional[str] = MISSING) -> Callable[...,
335336
self._websocket._dispatch.register(coro, name if name is not MISSING else coro.__name__)
336337
return coro
337338

339+
async def change_presence(self, presence: ClientPresence) -> None:
340+
"""
341+
A method that changes the current client's presence on runtime.
342+
343+
.. note::
344+
There is a ratelimit to using this method (5 per minute).
345+
As there's no gateway ratelimiter yet, breaking this ratelimit
346+
will force your bot to disconnect.
347+
348+
:param presence: The presence to change the bot to on identify.
349+
:type presence: ClientPresence
350+
"""
351+
await self._websocket._update_presence(presence)
352+
338353
def __check_command(
339354
self,
340355
command: ApplicationCommand,

interactions/client.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Client:
5050
async def _login(self) -> None: ...
5151
async def wait_until_ready(self) -> None: ...
5252
def event(self, coro: Coroutine, name: Optional[str] = None) -> Callable[..., Any]: ...
53+
def change_presence(self, presence: ClientPresence) -> None: ...
5354
def __check_command(
5455
self,
5556
command: ApplicationCommand,

interactions/models/component.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from ..api.error import InteractionException
44
from ..api.models.message import Emoji
5-
from ..api.models.misc import MISSING, DictSerializerMixin
5+
from ..api.models.misc import DictSerializerMixin
66
from ..enums import ButtonStyle, ComponentType, TextStyleType
77

88

0 commit comments

Comments
 (0)