Skip to content

Commit 4ed7114

Browse files
authored
Updated to use Classes instead
1 parent fa1d4b0 commit 4ed7114

File tree

1 file changed

+63
-74
lines changed

1 file changed

+63
-74
lines changed

discordoauth2.py

Lines changed: 63 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,72 @@
11
import requests
22

3-
client = dict()
3+
class token_instance():
4+
def __init__(self, access_token, bot_token):
5+
self.token = access_token
6+
self.bot_token = bot_token
7+
8+
def identify(self):
9+
try:
10+
headers = {
11+
'Authorization': f'Bearer {self.token}'
12+
}
13+
user_object = requests.get(url="https://discordapp.com/api/users/@me",headers=headers)
14+
return user_object.json()
15+
except(requests.exceptions.HTTPError) as error:
16+
return error
417

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'
18+
def connections(self):
19+
try:
20+
headers = {
21+
'Authorization': f'Bearer {self.token}'
22+
}
23+
user_object = requests.get(url="https://discordapp.com/api/users/@me/connections",headers=headers)
24+
return user_object.json()
25+
except(requests.exceptions.HTTPError) as error:
26+
return error
1427

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()
28+
def guilds(self):
29+
try:
30+
headers = {
31+
'Authorization': f'Bearer {self.token}'
32+
}
33+
user_object = requests.get(url="https://discordapp.com/api/users/@me/guilds",headers=headers)
34+
return user_object.json()
35+
except(requests.exceptions.HTTPError) as error:
36+
return error
2737

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()
38+
def join_guild(self, guild):
39+
try:
40+
headers = {
41+
'Authorization': f'Bot {self.bot_token}',
42+
'Content-Type': 'application/json'
43+
}
44+
data = {
45+
'access_token': self.token
46+
}
47+
user_object = requests.put(url=f"https://discordapp.com/api/guilds/{guild}/members/{self.identify}", json=data, headers=headers)
48+
return user_object.text
49+
except(requests.exceptions.HTTPError) as error:
50+
return error
4051

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}'
52+
class discordOauth2():
53+
endpoint = 'https://discord.com/api/v8'
54+
def __init__(self, client, secret, redirect, token=None):
55+
self.client = client
56+
self.secret = secret
57+
self.redirect = redirect
58+
self.token = token
59+
60+
def exchange_code(self, token):
61+
data = {
62+
'client_id': self.client,
63+
'client_secret': self.secret,
64+
'grant_type': 'authorization_code',
65+
'code': token,
66+
'redirect_uri': self.redirect
6567
}
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:
7368
headers = {
74-
'Authorization': f'Bot {client["token"]}',
75-
'Content-Type': 'application/json'
76-
}
77-
data = {
78-
'access_token': access_token
69+
'Content-Type': 'application/x-www-form-urlencoded'
7970
}
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
71+
data = requests.post(discordOauth2.endpoint+'/oauth2/token', data=data, headers=headers).json()
72+
return token_instance(data['access_token'], self.token)

0 commit comments

Comments
 (0)