From 40f12c8a00a22c869d3747227338b41d0c77fd3c Mon Sep 17 00:00:00 2001 From: Miku Chan <34207400+Dhruvacube@users.noreply.github.com> Date: Mon, 13 Jun 2022 12:47:43 +0530 Subject: [PATCH] Add state parameter to utils.oauth_url Co-authored-by: Danny --- discord/utils.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/discord/utils.py b/discord/utils.py index a473e5ff2..cf62b0185 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -288,13 +288,14 @@ def oauth_url( redirect_uri: str = MISSING, scopes: Iterable[str] = MISSING, disable_guild_select: bool = False, + state: str = MISSING, ) -> str: """A helper function that returns the OAuth2 URL for inviting the bot into guilds. .. versionchanged:: 2.0 - ``permissions``, ``guild``, ``redirect_uri``, and ``scopes`` parameters + ``permissions``, ``guild``, ``redirect_uri``, ``scopes`` and ``state`` parameters are now keyword-only. Parameters @@ -315,6 +316,10 @@ def oauth_url( disable_guild_select: :class:`bool` Whether to disallow the user from changing the guild dropdown. + .. versionadded:: 2.0 + state: :class:`str` + The state to return after the authorization. + .. versionadded:: 2.0 Returns @@ -328,12 +333,15 @@ def oauth_url( url += f'&permissions={permissions.value}' if guild is not MISSING: url += f'&guild_id={guild.id}' - if redirect_uri is not MISSING: - from urllib.parse import urlencode - - url += '&response_type=code&' + urlencode({'redirect_uri': redirect_uri}) if disable_guild_select: url += '&disable_guild_select=true' + if redirect_uri is not MISSING or state is not MISSING: + from urllib.parse import urlencode + + if redirect_uri is not MISSING: + url += '&response_type=code&' + urlencode({'redirect_uri': redirect_uri}) + if state is not MISSING: + url += f'&{urlencode({"state": state})}' return url