|
1 | 1 | import requests |
2 | 2 | from datetime import datetime |
3 | | -from typing import Optional |
| 3 | +from typing import Optional, Union, Literal |
| 4 | +from urllib import parse |
4 | 5 |
|
5 | 6 | class PartialAccessToken(): |
6 | 7 | def __init__(self, access_token, client) -> None: |
@@ -254,6 +255,28 @@ def revoke_token(self, token: str, token_type: str=None): |
254 | 255 | 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']) |
255 | 256 | else: |
256 | 257 | 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})}" |
257 | 280 |
|
258 | 281 | class exceptions(): |
259 | 282 | class BaseException(Exception): |
|
0 commit comments