forked from topliceanu/IC3-dec-id
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_discord.py
67 lines (58 loc) · 1.76 KB
/
query_discord.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
import os
from dotenv import load_dotenv
import json
import sys
load_dotenv()
API_ENDPOINT = 'https://discord.com/api/v10'
CLIENT_ID = os.getenv('DISCORD_APPLICATION_ID')
CLIENT_SECRET = os.getenv('DISCORD_CLIENT_SECRET')
REDIRECT_URI = os.getenv('REDIRECT_URI')
def exchange_code(code):
data = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': REDIRECT_URI
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.post('%s/oauth2/token' % API_ENDPOINT, data=data, headers=headers)
r.raise_for_status()
return r.json()
def get_token():
data = {
'grant_type': 'client_credentials',
'scope': 'identify guilds connections guilds.members.read'
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.post('%s/oauth2/token' % API_ENDPOINT, data=data, headers=headers, auth=(CLIENT_ID, CLIENT_SECRET))
r.raise_for_status()
return r.json()
def get_user_server_list(access_token):
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': f'Bearer {access_token}'
}
print("headers", headers)
r = requests.get('%s/users/@me/guilds' % API_ENDPOINT, headers=headers)
r.raise_for_status()
return r.json()
auth_obj = exchange_code(sys.argv[1])
print("auth obj", auth_obj)
# print("access token", auth_obj['access_token'])
# auth_obj = get_token()
# server_list = get_user_server_list(auth_obj['access_token'])
#
# found = False
# for server in server_list:
# if server['id'] == DISCORD_SERVER_ID:
# print('user is a part of dec-id')
# found = True
#
# if not found:
# print('user is NOT a part of dec-id')