From f7b61dace25ce87d32cd15bfd9146b84732e0347 Mon Sep 17 00:00:00 2001 From: davfsa Date: Tue, 8 Sep 2020 16:02:10 +0200 Subject: [PATCH] Add `update_presence` to bot --- hikari/api/entity_factory.py | 2 +- hikari/impl/bot.py | 16 ++++++++++++ hikari/traits.py | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/hikari/api/entity_factory.py b/hikari/api/entity_factory.py index daec0dabc3..e0a8f93cb4 100644 --- a/hikari/api/entity_factory.py +++ b/hikari/api/entity_factory.py @@ -22,7 +22,7 @@ """Core interface for an object that serializes/deserializes API objects.""" from __future__ import annotations -__all__: typing.Final[typing.List[str]] = ["EntityFactory"] +__all__: typing.Final[typing.List[str]] = ["EntityFactory", "GatewayGuildDefinition"] import abc import typing diff --git a/hikari/impl/bot.py b/hikari/impl/bot.py index c70a788bd3..52ba68d162 100644 --- a/hikari/impl/bot.py +++ b/hikari/impl/bot.py @@ -40,6 +40,7 @@ from hikari import intents as intents_ from hikari import presences from hikari import traits +from hikari import undefined from hikari import users from hikari.api import cache as cache_ from hikari.api import chunker as chunker_ @@ -823,6 +824,21 @@ async def wait_for( ) -> event_dispatcher.EventT_co: return await self._events.wait_for(event_type, timeout=timeout, predicate=predicate) + async def update_presence( + self, + *, + status: undefined.UndefinedOr[presences.Status] = undefined.UNDEFINED, + idle_since: undefined.UndefinedNoneOr[datetime.datetime] = undefined.UNDEFINED, + activity: undefined.UndefinedNoneOr[presences.Activity] = undefined.UNDEFINED, + afk: undefined.UndefinedOr[bool] = undefined.UNDEFINED, + ) -> None: + coros = [ + s.update_presence(status=status, activity=activity, idle_since=idle_since, afk=afk) + for s in self._shards.values() + ] + + await aio.all_of(*coros) + async def _start_one_shard( self, activity: typing.Optional[presences.Activity], diff --git a/hikari/traits.py b/hikari/traits.py index 1fb5e4d9a1..692643016d 100644 --- a/hikari/traits.py +++ b/hikari/traits.py @@ -42,11 +42,15 @@ import typing +from hikari import undefined + if typing.TYPE_CHECKING: import concurrent.futures + import datetime from hikari import config from hikari import intents as intents_ + from hikari import presences from hikari import users from hikari.api import cache as cache_ from hikari.api import chunker as chunker_ @@ -393,6 +397,51 @@ def shard_count(self) -> int: """ raise NotImplementedError + async def update_presence( + self, + *, + status: undefined.UndefinedOr[presences.Status] = undefined.UNDEFINED, + idle_since: undefined.UndefinedNoneOr[datetime.datetime] = undefined.UNDEFINED, + activity: undefined.UndefinedNoneOr[presences.Activity] = undefined.UNDEFINED, + afk: undefined.UndefinedOr[bool] = undefined.UNDEFINED, + ) -> None: + """Update the presence on all shards. + + This call will patch the presence on each shard. This means that + unless you explicitly specify a parameter, the previous value will be + retained. This means you do not have to track the global presence + in your code. + + Parameters + ---------- + idle_since : hikari.undefined.UndefinedNoneOr[datetime.datetime] + The datetime that the user started being idle. If undefined, this + will not be changed. + afk : hikari.undefined.UndefinedOr[builtins.bool] + If `builtins.True`, the user is marked as AFK. If `builtins.False`, + the user is marked as being active. If undefined, this will not be + changed. + activity : hikari.undefined.UndefinedNoneOr[hikari.presences.Activity] + The activity to appear to be playing. If undefined, this will not be + changed. + status : hikari.undefined.UndefinedOr[hikari.presences.Status] + The web status to show. If undefined, this will not be changed. + + !!! note + This will only send the update payloads to shards that are alive. + Any shards that are not alive will cache the new presence for + when they do start. + + !!! note + If you want to set presences per shard, access the shard you wish + to update (e.g. by using `BotApp.shards`), and call + `hikari.api.shard.GatewayShard.update_presence` on that shard. + + This method is simply a facade to make performing this in bulk + simpler. + """ + raise NotImplementedError + @typing.runtime_checkable class BotAware(RESTAware, ShardAware, EventFactoryAware, DispatcherAware, typing.Protocol):