From cc152c0892def39d652ca9515ccd389a7455c7a8 Mon Sep 17 00:00:00 2001 From: dolfies Date: Mon, 21 Aug 2023 01:16:29 +0300 Subject: [PATCH] Implement embedded activity platform configs --- discord/application.py | 129 ++++++++++++++++++++++++++++------- discord/enums.py | 20 ++++++ discord/http.py | 5 ++ discord/types/application.py | 7 +- docs/api.rst | 49 +++++++++++++ 5 files changed, 181 insertions(+), 29 deletions(-) diff --git a/discord/application.py b/discord/application.py index 0155a08ad..0d09574a1 100644 --- a/discord/application.py +++ b/discord/application.py @@ -25,19 +25,7 @@ DEALINGS IN THE SOFTWARE. from __future__ import annotations from datetime import datetime -from typing import ( - TYPE_CHECKING, - Any, - AsyncIterator, - Collection, - List, - Mapping, - Optional, - Sequence, - Tuple, - Union, - overload, -) +from typing import TYPE_CHECKING, Any, AsyncIterator, Collection, List, Mapping, Optional, Sequence, Tuple, Union, overload from urllib.parse import quote from . import utils @@ -51,8 +39,10 @@ from .enums import ( ApplicationType, ApplicationVerificationState, Distributor, + EmbeddedActivityLabelType, EmbeddedActivityOrientation, EmbeddedActivityPlatform, + EmbeddedActivityReleasePhase, Locale, OperatingSystem, RPCApplicationState, @@ -72,6 +62,8 @@ from .utils import _bytes_to_base64_data, _parse_localizations if TYPE_CHECKING: from datetime import date + from typing_extensions import Self + from .abc import Snowflake, SnowflakeTime from .enums import SKUAccessLevel, SKUFeature, SKUGenre, SKUType from .file import File @@ -92,6 +84,8 @@ if TYPE_CHECKING: Build as BuildPayload, Company as CompanyPayload, EmbeddedActivityConfig as EmbeddedActivityConfigPayload, + EmbeddedActivityPlatform as EmbeddedActivityPlatformValues, + EmbeddedActivityPlatformConfig as EmbeddedActivityPlatformConfigPayload, GlobalActivityStatistics as GlobalActivityStatisticsPayload, Manifest as ManifestPayload, ManifestLabel as ManifestLabelPayload, @@ -107,6 +101,7 @@ __all__ = ( 'EULA', 'Achievement', 'ThirdPartySKU', + 'EmbeddedActivityPlatformConfig', 'EmbeddedActivityConfig', 'ApplicationBot', 'ApplicationExecutable', @@ -436,6 +431,61 @@ class ThirdPartySKU: return f'' +class EmbeddedActivityPlatformConfig: + """Represents an application's embedded activity configuration for a specific platform. + + .. versionadded:: 2.1 + + Attributes + ----------- + platform: :class:`EmbeddedActivityPlatform` + The platform that the configuration is for. + label_type: :class:`EmbeddedActivityLabelType` + The current label shown on the activity. + label_until: Optional[:class:`datetime.datetime`] + When the current label expires. + release_phase: :class:`EmbeddedActivityReleasePhase` + The current release phase of the activity. + """ + + __slots__ = ('platform', 'label_type', 'label_until', 'release_phase') + + def __init__( + self, + platform: EmbeddedActivityPlatform, + *, + label_type: EmbeddedActivityLabelType = EmbeddedActivityLabelType.none, + label_until: Optional[datetime] = None, + release_phase: EmbeddedActivityReleasePhase = EmbeddedActivityReleasePhase.global_launch, + ): + self.platform = platform + self.label_type = label_type + self.label_until = label_until + self.release_phase = release_phase + + @classmethod + def from_data(cls, *, data: EmbeddedActivityPlatformConfigPayload, platform: EmbeddedActivityPlatformValues) -> Self: + return cls( + try_enum(EmbeddedActivityPlatform, platform), + label_type=try_enum(EmbeddedActivityLabelType, data.get('label_type', 0)), + label_until=utils.parse_time(data.get('label_until')), + release_phase=try_enum(EmbeddedActivityReleasePhase, data.get('release_phase', 'global_launch')), + ) + + def __repr__(self) -> str: + return ( + f'' + ) + + def to_dict(self) -> EmbeddedActivityPlatformConfigPayload: + return { + 'label_type': self.label_type.value, + 'label_until': self.label_until.isoformat() if self.label_until else None, + 'release_phase': self.release_phase.value, + } + + class EmbeddedActivityConfig: """Represents an application's embedded activity configuration. @@ -447,6 +497,10 @@ class EmbeddedActivityConfig: The application that the configuration is for. supported_platforms: List[:class:`EmbeddedActivityPlatform`] A list of platforms that the activity supports. + platform_configs: List[:class:`EmbeddedActivityPlatformConfig`] + A list of configurations for each supported activity platform. + + .. versionadded:: 2.1 orientation_lock_state: :class:`EmbeddedActivityOrientation` The mobile orientation lock state of the activity. tablet_orientation_lock_state: :class:`EmbeddedActivityOrientation` @@ -466,6 +520,7 @@ class EmbeddedActivityConfig: __slots__ = ( 'application', 'supported_platforms', + 'platform_configs', 'orientation_lock_state', 'tablet_orientation_lock_state', 'premium_tier_requirement', @@ -487,6 +542,10 @@ class EmbeddedActivityConfig: self.supported_platforms: List[EmbeddedActivityPlatform] = [ try_enum(EmbeddedActivityPlatform, platform) for platform in data.get('supported_platforms', []) ] + self.platform_configs: List[EmbeddedActivityPlatformConfig] = [ + EmbeddedActivityPlatformConfig.from_data(platform=platform, data=config) + for platform, config in data.get('client_platform_config', {}).items() + ] self.orientation_lock_state: EmbeddedActivityOrientation = try_enum( EmbeddedActivityOrientation, data.get('default_orientation_lock_state', 0) ) @@ -511,6 +570,7 @@ class EmbeddedActivityConfig: self, *, supported_platforms: Collection[EmbeddedActivityPlatform] = MISSING, + platform_configs: Collection[EmbeddedActivityPlatformConfig] = MISSING, orientation_lock_state: EmbeddedActivityOrientation = MISSING, tablet_orientation_lock_state: EmbeddedActivityOrientation = MISSING, requires_age_gate: bool = MISSING, @@ -527,6 +587,10 @@ class EmbeddedActivityConfig: ----------- supported_platforms: List[:class:`EmbeddedActivityPlatform`] A list of platforms that the activity supports. + platform_configs: List[:class:`EmbeddedActivityPlatformConfig`] + A list of configurations for each supported activity platform. + + .. versionadded:: 2.1 orientation_lock_state: :class:`EmbeddedActivityOrientation` The mobile orientation lock state of the activity. tablet_orientation_lock_state: :class:`EmbeddedActivityOrientation` @@ -551,16 +615,21 @@ class EmbeddedActivityConfig: """ data = await self.application._state.http.edit_embedded_activity_config( self.application.id, - supported_platforms=[str(x) for x in (supported_platforms or [])], - orientation_lock_state=int(orientation_lock_state), - tablet_orientation_lock_state=int(tablet_orientation_lock_state), - requires_age_gate=requires_age_gate, - shelf_rank=shelf_rank, + supported_platforms=[str(x) for x in (supported_platforms)] if supported_platforms is not MISSING else None, + platform_config={c.platform.value: c.to_dict() for c in (platform_configs)} + if platform_configs is not MISSING + else None, + orientation_lock_state=int(orientation_lock_state) if orientation_lock_state is not MISSING else None, + tablet_orientation_lock_state=int(tablet_orientation_lock_state) + if tablet_orientation_lock_state is not MISSING + else None, + requires_age_gate=requires_age_gate if requires_age_gate is not MISSING else None, + shelf_rank=shelf_rank if shelf_rank is not MISSING else None, free_period_starts_at=free_period_starts_at.isoformat() if free_period_starts_at else None, free_period_ends_at=free_period_ends_at.isoformat() if free_period_ends_at else None, preview_video_asset_id=(preview_video_asset.id if preview_video_asset else None) if preview_video_asset is not MISSING - else None, + else MISSING, ) self._update(data) @@ -3387,6 +3456,7 @@ class Application(PartialApplication): self, *, supported_platforms: Collection[EmbeddedActivityPlatform] = MISSING, + platform_configs: Collection[EmbeddedActivityPlatformConfig] = MISSING, orientation_lock_state: EmbeddedActivityOrientation = MISSING, tablet_orientation_lock_state: EmbeddedActivityOrientation = MISSING, requires_age_gate: bool = MISSING, @@ -3403,6 +3473,10 @@ class Application(PartialApplication): ----------- supported_platforms: List[:class:`EmbeddedActivityPlatform`] A list of platforms that the activity supports. + platform_configs: List[:class:`EmbeddedActivityPlatformConfig`] + A list of configurations for each supported activity platform. + + .. versionadded:: 2.1 orientation_lock_state: :class:`EmbeddedActivityOrientation` The mobile orientation lock state of the activity. tablet_orientation_lock_state: :class:`EmbeddedActivityOrientation` @@ -3432,16 +3506,21 @@ class Application(PartialApplication): """ data = await self._state.http.edit_embedded_activity_config( self.id, - supported_platforms=[str(x) for x in (supported_platforms or [])], - orientation_lock_state=int(orientation_lock_state), - tablet_orientation_lock_state=int(tablet_orientation_lock_state), - requires_age_gate=requires_age_gate, - shelf_rank=shelf_rank, + supported_platforms=[str(x) for x in (supported_platforms)] if supported_platforms is not MISSING else None, + platform_config={c.platform.value: c.to_dict() for c in (platform_configs)} + if platform_configs is not MISSING + else None, + orientation_lock_state=int(orientation_lock_state) if orientation_lock_state is not MISSING else None, + tablet_orientation_lock_state=int(tablet_orientation_lock_state) + if tablet_orientation_lock_state is not MISSING + else None, + requires_age_gate=requires_age_gate if requires_age_gate is not MISSING else None, + shelf_rank=shelf_rank if shelf_rank is not MISSING else None, free_period_starts_at=free_period_starts_at.isoformat() if free_period_starts_at else None, free_period_ends_at=free_period_ends_at.isoformat() if free_period_ends_at else None, preview_video_asset_id=(preview_video_asset.id if preview_video_asset else None) if preview_video_asset is not MISSING - else None, + else MISSING, ) if self.embedded_activity_config is not None: self.embedded_activity_config._update(data) diff --git a/discord/enums.py b/discord/enums.py index 8dc2d70f9..e31deb361 100644 --- a/discord/enums.py +++ b/discord/enums.py @@ -1009,6 +1009,26 @@ class EmbeddedActivityOrientation(Enum): return self.value +class EmbeddedActivityLabelType(Enum): + none = 0 + new = 1 + updated = 2 + + def __int__(self) -> int: + return self.value + + +class EmbeddedActivityReleasePhase(Enum): + in_development = 'in_development' + activities_team = 'activities_team' + employee_release = 'employee_release' + soft_launch = 'soft_launch' + global_launch = 'global_launch' + + def __str__(self) -> str: + return self.value + + T = TypeVar('T') diff --git a/discord/http.py b/discord/http.py index 0fe516785..f86f53252 100644 --- a/discord/http.py +++ b/discord/http.py @@ -3155,6 +3155,9 @@ class HTTPClient: app_id: Snowflake, *, supported_platforms: Optional[List[str]] = None, + platform_config: Optional[ + Dict[application.EmbeddedActivityPlatform, application.EmbeddedActivityPlatformConfig] + ] = None, orientation_lock_state: Optional[int] = None, tablet_orientation_lock_state: Optional[int] = None, requires_age_gate: Optional[bool] = None, @@ -3166,6 +3169,8 @@ class HTTPClient: payload = {} if supported_platforms is not None: payload['supported_platforms'] = supported_platforms + if platform_config is not None: + payload['client_platform_config'] = platform_config if orientation_lock_state is not None: payload['default_orientation_lock_state'] = orientation_lock_state if tablet_orientation_lock_state is not None: diff --git a/discord/types/application.py b/discord/types/application.py index 4ac9c61cc..204576ef1 100644 --- a/discord/types/application.py +++ b/discord/types/application.py @@ -249,16 +249,15 @@ class GlobalActivityStatistics(TypedDict): EmbeddedActivityPlatform = Literal['web', 'android', 'ios'] -EmbeddedActivityPlatformLabelType = Literal[0, 1, 2] -EmbedddedActivityPlatformReleasePhase = Literal[ +EmbeddedActivityPlatformReleasePhase = Literal[ 'in_development', 'activities_team', 'employee_release', 'soft_launch', 'global_launch' ] class EmbeddedActivityPlatformConfig(TypedDict): - label_type: EmbeddedActivityPlatformLabelType + label_type: Literal[0, 1, 2] label_until: Optional[str] - release_phase: EmbedddedActivityPlatformReleasePhase + release_phase: EmbeddedActivityPlatformReleasePhase class EmbeddedActivityConfig(TypedDict): diff --git a/docs/api.rst b/docs/api.rst index 323e1106f..57066bb60 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -3457,6 +3457,50 @@ of :class:`enum.Enum`. The activity is locked to landscape. +.. class:: EmbeddedActivityLabelType + + Represents the label shown by an embedded activity. + + .. versionadded:: 2.1 + + .. attribute:: none + + No special label. + + .. attribute:: new + + The activity is new. + + .. attribute:: updated + + The activity has been recently updated. + +.. class:: EmbeddedActivityReleasePhase + + Represents the release phase of an embedded activity for a specific :class:`EmbeddedActivityPlatform`. + + .. versionadded:: 2.1 + + .. attribute:: in_development + + The activity is still in development. + + .. attribute:: activities_team + + The activity is available to guilds with the `ACTIVITIES_INTERNAL_DEV` guild feature. + + .. attribute:: employee_release + + The activity is available to guilds with the `ACTIVITIES_EMPLOYEE` guild feature. + + .. attribute:: soft_launch + + The activity is available to guilds with the `ACTIVITIES_ALPHA` guild feature. + + .. attribute:: global_launch + + The activity is available to all guilds. + .. class:: PayoutAccountStatus Represents the status of a team payout account. @@ -6765,6 +6809,11 @@ Application .. autoclass:: EmbeddedActivityConfig() :members: +.. attributetable:: EmbeddedActivityPlatformConfig + +.. autoclass:: EmbeddedActivityPlatformConfig() + :members: + .. attributetable:: UnverifiedApplication .. autoclass:: UnverifiedApplication()