Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(activity): fall back to name for custom activities #1087

Merged
merged 5 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1087.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make :attr:`CustomActivity.state` fall back to the provided :attr:`~CustomActivity.name`, simplifying setting a custom status.
11 changes: 9 additions & 2 deletions disnake/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,8 @@ class CustomActivity(BaseActivity):
The custom activity's name.
emoji: Optional[:class:`PartialEmoji`]
The emoji to pass to the activity, if any.

This currently cannot be set by bots.
"""

__slots__ = ("name", "emoji", "state")
Expand All @@ -810,7 +812,10 @@ def __init__(
) -> None:
super().__init__(**kwargs)
self.name: Optional[str] = name
self.state: Optional[str] = state
# Fall back to `name`, since `state` is the relevant field for custom status (`name` is not shown)
self.state: Optional[str] = state or name

# The official client uses "Custom Status" as the name, the actual name is in `state`
if self.name == "Custom Status":
self.name = self.state

Expand Down Expand Up @@ -904,7 +909,9 @@ def create_activity(

activity: ActivityTypes
game_type = try_enum(ActivityType, data.get("type", -1))
if game_type is ActivityType.playing and not ("application_id" in data or "session_id" in data):
if game_type is ActivityType.playing and not (
"application_id" in data or "session_id" in data or "state" in data
):
activity = Game(**data) # type: ignore # pyright bug(?)
elif game_type is ActivityType.custom and "name" in data:
activity = CustomActivity(**data) # type: ignore
Expand Down
10 changes: 7 additions & 3 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ Where can I find usage examples?
Example code can be found in the `examples folder <https://github.com/DisnakeDev/disnake/tree/master/examples>`_
in the repository.

How do I set the "Playing" status?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How do I set an activity/status?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The ``activity`` keyword argument may be passed in the :class:`Client` constructor or :meth:`Client.change_presence`, given an :class:`Activity` object.

Expand All @@ -100,16 +100,20 @@ The constructor may be used for static activities, while :meth:`Client.change_pr

There is a high chance of disconnecting if presences are changed right after connecting.

The status type (playing, listening, streaming, watching) can be set using the :class:`ActivityType` enum.
The status type (playing, listening, streaming, watching, or custom) can be set using the :class:`ActivityType` enum.
For memory optimisation purposes, some activities are offered in slimmed-down versions:

- :class:`Game`
- :class:`Streaming`
- :class:`CustomActivity`

Putting both of these pieces of info together, you get the following: ::

client = disnake.Client(activity=disnake.Game(name='my game'))

# alternatively, a plain custom status:
client = disnake.Client(activity=disnake.CustomActivity(name='As seen on TV!'))

# or, for watching:
activity = disnake.Activity(name='my activity', type=disnake.ActivityType.watching)
client = disnake.Client(activity=activity)
Expand Down
Loading