Skip to content

Commit

Permalink
Merge pull request #141 from davfsa/task/bot-update-presence
Browse files Browse the repository at this point in the history
Add `update_presence` to bot
  • Loading branch information
Nekokatt authored Sep 8, 2020
2 parents d9a7283 + f7b61da commit 70396cd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hikari/api/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions hikari/impl/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down Expand Up @@ -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],
Expand Down
49 changes: 49 additions & 0 deletions hikari/traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 70396cd

Please sign in to comment.