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: add support for editing our own application information #1237

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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/1237.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add new :meth:`Client.edit_application_info` and :meth:`AppInfo.edit` methods.
101 changes: 99 additions & 2 deletions disnake/appinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from __future__ import annotations

from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING, Any, Dict, List, Optional

from . import utils
from .asset import Asset
from .asset import Asset, AssetBytes
from .flags import ApplicationFlags
from .permissions import Permissions
from .utils import MISSING

if TYPE_CHECKING:
from .guild import Guild
Expand All @@ -20,6 +21,7 @@
)
from .user import User


__all__ = (
"AppInfo",
"PartialAppInfo",
Expand Down Expand Up @@ -64,6 +66,12 @@ def to_url(self) -> str:
"""
return utils.oauth_url(self._app_id, scopes=self.scopes, permissions=self.permissions)

def to_dict(self) -> Dict[str, Any]:
return {
"scopes": self.scopes,
"permissions": self.permissions.value,
}


class AppInfo:
"""Represents the application info for the bot provided by Discord.
Expand Down Expand Up @@ -280,6 +288,95 @@ def summary(self) -> str:
)
return self._summary

async def edit(
self,
*,
description: Optional[str] = MISSING,
flags: ApplicationFlags = MISSING,
icon: Optional[AssetBytes] = MISSING,
cover_image: Optional[AssetBytes] = MISSING,
custom_install_url: Optional[str] = MISSING,
install_params: Optional[InstallParams] = MISSING,
role_connections_verification_url: Optional[str] = MISSING,
interactions_endpoint_url: Optional[str] = MISSING,
tags: Optional[List[str]] = MISSING,
Comment on lines +294 to +302
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figured this is worth mentioning - most of these fields, other than icon and cover_image, aren't marked as nullable in the documentation.
However considering that they are optional in the application object structure itself, clearly only setting them to null would reset the fields. So I assume this is fine?

) -> AppInfo:
"""|coro|

Edit's the application's information.

All parameters are optional.

Returns a new :class:`AppInfo` with the updated information.
Comment on lines +309 to +310
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Returns a new :class:`AppInfo` with the updated information.


.. versionadded:: 2.10

Parameters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the docstring is missing interactions_endpoint_url

----------
description: Optional[:class:`str`]
The application's description.
flags: Optional[:class:`ApplicationFlags`]
The application's public flags.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should note that this is limited to specific flags only

tags: Optional[List[:class:`str`]]
The application's tags.
install_params: Optional[:class:`InstallParams`]
The installation parameters for this application.
custom_install_url: Optional[:class:`str`]
The custom installation url for this application.
role_connections_verification_url: Optional[:class:`str`]
The application's role connection verification entry point,
which when configured will render the app as a verification method
in the guild role verification configuration.
icon: |resource_type|
Update the application's icon asset, if any.
cover_image: |resource_type|
Retrieves the cover image on a store embed, if any.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this description is outdated


Raises
------
HTTPException
Editing the information failed somehow.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Editing the information failed somehow.
Editing the application information failed.


Returns
-------
:class:`.AppInfo`
The bot's new application information.
"""
fields: Dict[str, Any] = {}

if install_params is not MISSING:
fields["install_params"] = None if install_params is None else install_params.to_dict()

if icon is not MISSING:
fields["icon"] = await utils._assetbytes_to_base64_data(icon)

if cover_image is not MISSING:
fields["cover_image"] = await utils._assetbytes_to_base64_data(cover_image)

if flags is not MISSING:
fields["flags"] = flags.value

if description is not MISSING:
fields["description"] = description

if custom_install_url is not MISSING:
fields["custom_install_url"] = custom_install_url

if role_connections_verification_url is not MISSING:
fields["role_connections_verification_url"] = role_connections_verification_url

if interactions_endpoint_url is not MISSING:
fields["interactions_endpoint_url"] = interactions_endpoint_url

if tags is not MISSING:
fields["tags"] = tags

data = await self._state.http.edit_application_info(**fields)

if "rpc_origins" not in data:
data["rpc_origins"] = None
return AppInfo(self._state, data)


class PartialAppInfo:
"""Represents a partial AppInfo given by :func:`~disnake.abc.GuildChannel.create_invite`.
Expand Down
89 changes: 88 additions & 1 deletion disnake/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
ApplicationCommand,
GuildApplicationCommandPermissions,
)
from .appinfo import AppInfo
from .appinfo import AppInfo, InstallParams
from .application_role_connection import ApplicationRoleConnectionMetadata
from .backoff import ExponentialBackoff
from .channel import PartialMessageable, _threaded_channel_factory
Expand Down Expand Up @@ -2372,6 +2372,93 @@ async def application_info(self) -> AppInfo:
data["rpc_origins"] = None
return AppInfo(self._connection, data)

async def edit_application_info(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the same method on Client seems unnecessary (also not very DRY code :<).

self,
*,
description: Optional[str] = MISSING,
flags: ApplicationFlags = MISSING,
icon: Optional[AssetBytes] = MISSING,
cover_image: Optional[AssetBytes] = MISSING,
custom_install_url: Optional[str] = MISSING,
install_params: Optional[InstallParams] = MISSING,
role_connections_verification_url: Optional[str] = MISSING,
interactions_endpoint_url: Optional[str] = MISSING,
tags: Optional[List[str]] = MISSING,
) -> AppInfo:
"""|coro|

Edit's the application's information.

All parameters are optional.

.. versionadded:: 2.10

Parameters
----------
description: Optional[:class:`str`]
The application's description.
flags: Optional[:class:`.ApplicationFlags`]
The application's public flags.
tags: Optional[List[:class:`str`]]
The application's tags.
install_params: Optional[:class:`.InstallParams`]
The installation parameters for this application.
custom_install_url: Optional[:class:`str`]
The custom installation url for this application.
role_connections_verification_url: Optional[:class:`str`]
The application's role connection verification entry point,
which when configured will render the app as a verification method
in the guild role verification configuration.
icon: |resource_type|
Update the application's icon asset, if any.
cover_image: |resource_type|
Retrieves the cover image on a store embed, if any.

Raises
------
HTTPException
Editing the information failed somehow.

Returns
-------
:class:`.AppInfo`
The bot's application information.
"""
fields: Dict[str, Any] = {}

if install_params is not MISSING:
fields["install_params"] = None if install_params is None else install_params.to_dict()

if icon is not MISSING:
fields["icon"] = await utils._assetbytes_to_base64_data(icon)

if cover_image is not MISSING:
fields["cover_image"] = await utils._assetbytes_to_base64_data(cover_image)

if flags is not MISSING:
fields["flags"] = flags.value

if description is not MISSING:
fields["description"] = description

if custom_install_url is not MISSING:
fields["custom_install_url"] = custom_install_url

if role_connections_verification_url is not MISSING:
fields["role_connections_verification_url"] = role_connections_verification_url

if interactions_endpoint_url is not MISSING:
fields["interactions_endpoint_url"] = interactions_endpoint_url

if tags is not MISSING:
fields["tags"] = tags

data = await self.http.edit_application_info(**fields)

if "rpc_origins" not in data:
data["rpc_origins"] = None
return AppInfo(self._connection, data)

async def fetch_user(self, user_id: int, /) -> User:
"""|coro|

Expand Down
3 changes: 3 additions & 0 deletions disnake/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2794,6 +2794,9 @@ def get_voice_regions(self) -> Response[List[voice.VoiceRegion]]:
def application_info(self) -> Response[appinfo.AppInfo]:
return self.request(Route("GET", "/oauth2/applications/@me"))

def edit_application_info(self, **fields: Any) -> Response[appinfo.AppInfo]:
return self.request(Route("PATCH", "/applications/@me"), json=fields)

def get_application_role_connection_metadata_records(
self, application_id: Snowflake
) -> Response[List[application_role_connection.ApplicationRoleConnectionMetadata]]:
Expand Down
Loading