|
| 1 | +import requests |
| 2 | + |
| 3 | +client = dict() |
| 4 | + |
| 5 | +def client(payload): |
| 6 | + global client |
| 7 | + client = { |
| 8 | + 'id': str(payload['client_id']), |
| 9 | + 'secret': payload['client_secret'], |
| 10 | + 'redirect_uri': payload['redirect_uri'], |
| 11 | + 'token': payload['bot_token'] |
| 12 | +} |
| 13 | +endpoint = 'https://discord.com/api/v8' |
| 14 | + |
| 15 | +def exchange_code(token): |
| 16 | + data = { |
| 17 | + 'client_id': client['id'], |
| 18 | + 'client_secret': client['secret'], |
| 19 | + 'grant_type': 'authorization_code', |
| 20 | + 'code': token, |
| 21 | + 'redirect_uri': client['redirect_uri'] |
| 22 | + } |
| 23 | + headers = { |
| 24 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 25 | + } |
| 26 | + return requests.post(endpoint+'/oauth2/token', data=data, headers=headers).json() |
| 27 | + |
| 28 | +def refresh_token(token): |
| 29 | + data = { |
| 30 | + 'client_id': client['id'], |
| 31 | + 'client_secret': client['secret'], |
| 32 | + 'grant_type': 'authorization_code', |
| 33 | + 'code': token |
| 34 | + } |
| 35 | + headers = { |
| 36 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 37 | + } |
| 38 | + r = requests.post(endpoint+'/oauth2/token', data=data, headers=headers) |
| 39 | + return r.json() |
| 40 | + |
| 41 | +def get_identify(access_token): |
| 42 | + try: |
| 43 | + headers = { |
| 44 | + 'Authorization': f'Bearer {access_token}' |
| 45 | + } |
| 46 | + user_object = requests.get(url="https://discordapp.com/api/users/@me",headers=headers) |
| 47 | + return user_object.json() |
| 48 | + except(requests.exceptions.HTTPError) as error: |
| 49 | + return error |
| 50 | + |
| 51 | +def get_connections(access_token): |
| 52 | + try: |
| 53 | + headers = { |
| 54 | + 'Authorization': f'Bearer {access_token}' |
| 55 | + } |
| 56 | + user_object = requests.get(url="https://discordapp.com/api/users/@me/connections",headers=headers) |
| 57 | + return user_object.json() |
| 58 | + except(requests.exceptions.HTTPError) as error: |
| 59 | + return error |
| 60 | + |
| 61 | +def get_guilds(access_token): |
| 62 | + try: |
| 63 | + headers = { |
| 64 | + 'Authorization': f'Bearer {access_token}' |
| 65 | + } |
| 66 | + user_object = requests.get(url="https://discordapp.com/api/users/@me/guilds",headers=headers) |
| 67 | + return user_object.json() |
| 68 | + except(requests.exceptions.HTTPError) as error: |
| 69 | + return error |
| 70 | + |
| 71 | +def join_guild(access_token, guild): |
| 72 | + try: |
| 73 | + headers = { |
| 74 | + 'Authorization': f'Bot {client["token"]}', |
| 75 | + 'Content-Type': 'application/json' |
| 76 | + } |
| 77 | + data = { |
| 78 | + 'access_token': access_token |
| 79 | + } |
| 80 | + user_object = requests.put(url=f"https://discordapp.com/api/guilds/{guild}/members/{get_identify(access_token)['id']}", json=data, headers=headers) |
| 81 | + return user_object.text |
| 82 | + except(requests.exceptions.HTTPError) as error: |
| 83 | + return error |
0 commit comments