Skip to content

Commit 3714164

Browse files
committed
Added client.generate_uri, from #6
1 parent 5c96783 commit 3714164

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ client.update_linked_roles_metadata([
3737
}
3838
])
3939

40+
@app.route('/')
41+
def main():
42+
return redirect(client.generate_uri(scope=["identify", "connections", "guilds", "role_connections.write"]))
43+
4044
@app.route("/oauth2")
4145
def oauth2():
4246
code = request.args.get("code")
@@ -52,4 +56,4 @@ def oauth2():
5256
return f"""{identify}<br><br>{connections}<br><br>{guilds}"""
5357

5458
app.run("0.0.0.0", 8080)
55-
```
59+
```

discordoauth2/__init__.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import requests
22
from datetime import datetime
3-
from typing import Optional
3+
from typing import Optional, Union, Literal
4+
from urllib import parse
45

56
class PartialAccessToken():
67
def __init__(self, access_token, client) -> None:
@@ -254,6 +255,28 @@ def revoke_token(self, token: str, token_type: str=None):
254255
elif response.status_code == 429: raise exceptions.RateLimited(f"You are being Rate Limited. Retry after: {response.json()['retry_after']}", retry_after=response.json()['retry_after'])
255256
else:
256257
raise exceptions.HTTPException(f"Unexpected HTTP {response.status_code}")
258+
259+
def generate_uri(self, scope: Union[str, list[str]], state: Optional[str]=None, response_type: Literal["code", "token"]="code", guild_id=None, disable_guild_select=None, permissions=None) -> str:
260+
"""Creates an authorization uri with client information prefilled.
261+
262+
scope: a string, or list of strings for the scope
263+
state: optional state parameter. Optional but recommended.
264+
response_type: either code, or token. token means the server can't access it, but the client can use it without converting.
265+
guild_id: the guild ID to add a bot/webhook.
266+
disable_guild_select: wether to allow the authorizing user to change the selected guild
267+
permissions: the permission bitwise integer for the bot being added.
268+
"""
269+
params = {
270+
"client_id": self.id,
271+
"scope": " ".join(scope) if type(scope) == list else scope,
272+
"state": state,
273+
"redirect_uri": self.redirect_url,
274+
"response_type": response_type,
275+
"guild_id": guild_id,
276+
"disable_guild_select": disable_guild_select,
277+
"permissions": permissions
278+
}
279+
return f"https://discord.com/oauth2/authorize?{parse.urlencode({key: value for key, value in params.items() if value is not None})}"
257280

258281
class exceptions():
259282
class BaseException(Exception):

docs/source/client.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,17 @@ Client
8686

8787
.. note::
8888

89-
The bot token is required to update metadata.
89+
The bot token is required to update metadata.
90+
91+
.. method:: generate_uri(metadata)
92+
93+
Creates an authorization uri with client information prefilled.
94+
95+
.. versionadded:: 1.1
96+
97+
:param Union[str, list[str]] scope: A list, or space-seperated string for the authorization scope
98+
:param Optional[str] state: State parameter. It is recommended for security.
99+
:param Optional[Literal["code", "token"]] response_type: either code, or token. token means the server can't access it, but the client can use it without converting.
100+
:param Optional[Union[int, str]] guild_id: the guild ID to add a bot/webhook.
101+
:param Optional[bool] disable_guild_select: wether to allow the authorizing user to change the selected guild
102+
:param Optional[int] permissions: the permission bitwise integer for the bot being added.

example.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131

3232
@app.route('/')
3333
def main():
34-
# Your OAuth2 url, you can make one a https://discord.dev
35-
return redirect("https://discord.com/api/oauth2/authorize")
34+
return redirect(client.generate_uri(scope=["identify", "connections", "guilds", "guilds.member", "guilds.join", "role_connections.write"]))
3635

3736
@app.route('/oauth2')
3837
def oauth():

0 commit comments

Comments
 (0)