Skip to content

Commit 89d3011

Browse files
committed
refactor(AppInfo): improve integration_types_config handling and simplify owner assignment
1 parent bde7ec3 commit 89d3011

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

discord/appinfo.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,8 @@ class AppInfo:
141141
142142
.. versionadded:: 2.7
143143
144-
integration_types_config: Optional[Dict[:class:`int`, Optional[Dict[:class:`str`, Any]]]]
145-
Per-installation context configuration. Keys are ``0`` (guild) and ``1`` (user) mapping to an object containing
146-
``oauth2_install_params`` or ``None`` if cleared.
144+
integration_types_config: Optional[:class:`IntegrationTypesConfig`]
145+
Per-installation context configuration for guild (``0``) and user (``1``) contexts.
147146
148147
.. versionadded:: 2.7
149148
@@ -238,10 +237,7 @@ def __init__(self, state: ConnectionState, data: AppInfoPayload):
238237
self.rpc_origins: list[str] | None = data.get("rpc_origins")
239238
self.bot_public: bool = data.get("bot_public", False)
240239
self.bot_require_code_grant: bool = data.get("bot_require_code_grant", False)
241-
owner_data = data.get("owner")
242-
self.owner: User | None = (
243-
state.create_user(owner_data) if owner_data is not None else None
244-
)
240+
self.owner: User | None = data.get("owner") and state.create_user(data["owner"])
245241

246242
team: TeamPayload | None = data.get("team")
247243
self.team: Team | None = Team(state, team) if team else None
@@ -266,8 +262,7 @@ def __init__(self, state: ConnectionState, data: AppInfoPayload):
266262
self.approximate_user_authorization_count: int | None = data.get(
267263
"approximate_user_authorization_count"
268264
)
269-
raw_flags = data.get("flags")
270-
self._flags: int | None = raw_flags if isinstance(raw_flags, int) else None
265+
self._flags: int | None = data.get("flags")
271266
self.redirect_uris: list[str] = data.get("redirect_uris", [])
272267
self.interactions_endpoint_url: str | None = data.get(
273268
"interactions_endpoint_url"
@@ -279,13 +274,12 @@ def __init__(self, state: ConnectionState, data: AppInfoPayload):
279274
self.event_webhooks_status: int | None = data.get("event_webhooks_status")
280275
self.event_webhooks_types: list[str] | None = data.get("event_webhooks_types")
281276

282-
install_params = data.get("install_params")
283-
self.install_params: AppInstallParams | None = (
284-
AppInstallParams(install_params) if install_params else None
277+
self.install_params: AppInstallParams | None = data.get("install_params") and (
278+
AppInstallParams(data["install_params"])
285279
)
286280
self.tags: list[str] = data.get("tags", [])
287281
self.custom_install_url: str | None = data.get("custom_install_url")
288-
self.integration_types_config: dict[int, dict[str, object] | None] | None = (
282+
self.integration_types_config = IntegrationTypesConfig.from_payload(
289283
data.get("integration_types_config")
290284
)
291285

@@ -301,6 +295,8 @@ def flags(self) -> ApplicationFlags | None:
301295
"""The public application flags, if set.
302296
303297
Returns an :class:`ApplicationFlags` instance or ``None`` when not present.
298+
299+
.. versionadded:: 2.7
304300
"""
305301
if self._flags is None:
306302
return None
@@ -329,12 +325,10 @@ async def edit(
329325
330326
Edit the current application's settings.
331327
332-
This method wraps the Edit Current Application endpoint and returns the updated application info.
333-
334328
Parameters
335329
----------
336330
description: Optional[:class:`str`]
337-
The new application description. Pass ``None`` to clear.
331+
The new application description or ``None`` to clear.
338332
icon: Optional[Union[:class:`bytes`, :class:`str`]]
339333
New icon image. If ``bytes`` is given it will be base64 encoded automatically. If a ``str`` is given it is assumed
340334
to be a pre-encoded base64 data URI or hash and sent as-is. Pass ``None`` to clear.
@@ -372,6 +366,8 @@ async def edit(
372366
-------
373367
:class:`.AppInfo`
374368
The updated application information.
369+
370+
.. versionadded:: 2.7
375371
"""
376372
payload: dict[str, object] = {}
377373
if description is not utils.MISSING:
@@ -603,6 +599,29 @@ def __init__(
603599
self.guild = guild
604600
self.user = user
605601

602+
@classmethod
603+
def from_payload(cls, data: dict | None) -> "IntegrationTypesConfig | None":
604+
if data is None:
605+
return None
606+
607+
def _get_ctx(raw: dict, key: int):
608+
if key in raw:
609+
return raw[key]
610+
skey = str(key)
611+
return raw.get(skey)
612+
613+
def _decode_ctx(value: dict | None) -> AppInstallParams | None:
614+
if value is None:
615+
return None
616+
params = value.get("oauth2_install_params")
617+
if not params:
618+
return None
619+
return AppInstallParams(params)
620+
621+
guild_ctx = _decode_ctx(_get_ctx(data, 0))
622+
user_ctx = _decode_ctx(_get_ctx(data, 1))
623+
return cls(guild=guild_ctx, user=user_ctx)
624+
606625
def _encode_install_params(
607626
self, value: AppInstallParams | None
608627
) -> dict[str, object] | None:

0 commit comments

Comments
 (0)