-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapplication.py
256 lines (229 loc) · 7.67 KB
/
application.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import requests
import configparser
import os
from flask import Flask, request, redirect, url_for, render_template
config = configparser.ConfigParser()
config.read('database.ini')
application = Flask(__name__)
API_ENDPOINT = "https://discord.com/api/v9"
CLIENT_ID = config['apiinfo']['CLIENT_ID']
CLIENT_SECRET = config['apiinfo']['CLIENT_SECRET']
CLIENT_TOKEN = config['botinfo']['bottoken']
DOMAIN = config['apiinfo']['DOMAIN']
exchangepass = config['apiinfo']['exchangepass']
SCOPE = "identify guilds guilds.join"
REDIRECT_URI = f"{DOMAIN}/discordauth"
welcomechannel = str(config['botinfo']['welcome_channel'])
memberrole = str(config['botinfo']['memberrole'])
restorekey = str(config['botinfo']['therestorekey'])
guildid = config['info']['guildid']
def cls():
os.system('cls' if os.name == 'nt' else 'clear')
@application.route('/working', methods=['GET', 'POST'])
def working():
return 'true'
@application.route('/discordauth', methods=['GET', 'POST'])
def discord():
print("In discordauth")
code = request.args.get('code')
data = exchange_code(code)
state = request.args.get('state')
access_token = data.get("access_token")
refresh_token = data.get("refresh_token")
data2 = getid(access_token)
userid = str(data2.get("id"))
username = data2.get("username")
country = data2.get("locale")
if userid in config['useridsincheck']:
config['users'][userid] = 'NA'
config[userid] = {}
config[userid]['refresh_tokens'] = refresh_token
config[userid]['refresh'] = 'true'
config[userid]['country'] = country
with open('database.ini', 'w') as configfile:
config.write(configfile)
if request.method == 'POST':
return 'success'
if request.method == 'GET':
return render_template('Authcomplete.html')
elif userid in config['users']:
if request.method == 'POST':
return 'success'
if request.method == 'GET':
return render_template('Authcomplete.html')
else:
return 'fail'
@application.route('/restore', methods=['GET', 'POST'])
def restore():
password = request.json['code']
if password == exchangepass:
restoreserver()
return 'succsess'
else:
print("Invalid password" + password)
return 'wrong password'
@application.route('/', methods=['GET', 'POST'])
def testbuild():
return render_template('index.html')
def getid(info):
url = "https://discord.com/api/v9/users/@me"
payload={}
accsestokentoget = info
headers = {
'Authorization': 'Bearer ' + accsestokentoget,
}
response = requests.request("GET", url, headers=headers, data=payload)
response.raise_for_status()
return response.json()
#error to fix in here
@application.route('/requestid', methods=['GET', 'POST'])
def requestid():
print("Part requestid")
key = request.json['key']
id = str(request.json['id'])
print(id)
print(key)
if key == exchangepass:
if id in config['users']:
return 'succsess'
else:
print("key was correct")
#check if the category is in the config
config['useridsincheck'] = {}
config['useridsincheck'][id] = 'waiting'
with open('database.ini', 'w') as configfile:
config.write(configfile)
return 'succsess'
else:
print("key was wrong")
return 'wrong key'
@application.route('/data', methods=['GET', 'POST'])
def data():
dataset = request.json['dataset']
print("part data")
if config['apiinfo']['botsetupcomplete'] == 'no':
if dataset == 'CLIENT_ID':
return CLIENT_ID
if dataset == 'guildid':
config['apiinfo']['botsetupcomplete'] = 'yes'
with open('database.ini', 'w') as configfile:
config.write(configfile)
return guildid
if dataset == 'CLIENT_SECRET':
return CLIENT_SECRET
if dataset == 'bottoken':
return CLIENT_TOKEN
if dataset == 'exchangepass':
return exchangepass
if dataset == 'welcomechannel':
return welcomechannel
if dataset == 'verifiedrole':
return memberrole
if dataset == 'restorekey':
return restorekey
else:
return 'fail datasetval needed'
else:
return 'fail setup done alr'
@application.route('/checkifverifydone', methods=['GET', 'POST'])
def checkifverifydone():
print("Part checkifverifydone")
key = request.json['key']
id = str(request.json['id'])
print(id)
print(key)
if key == exchangepass:
print("key was correct")
if id in config['users']:
config['useridsincheck'][id] = 'verified'
with open('database.ini', 'w') as configfile:
config.write(configfile)
print("corect")
return 'true'
else:
print("id was not found")
return 'false'
else:
return 'false'
def exchange_code(code):
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
data = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': REDIRECT_URI,
'scope': SCOPE
}
r = requests.post(
f"{API_ENDPOINT}/oauth2/token",
data=data,
headers=headers
)
r.raise_for_status()
return r.json()
def get_new_token(old_token): # gets new refresh_token
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
data = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': 'refresh_token',
'refresh_token': old_token
}
r = requests.post(
f"{API_ENDPOINT}/oauth2/token",
data=data,
headers=headers
)
r.raise_for_status()
return r.json()
def add_to_guild(access_token, user_id, guild_id):
headers = {
"Authorization" : f"Bot {CLIENT_TOKEN}",
'Content-Type': 'application/json'
}
data = {
"access_token" : access_token
}
response = requests.put(
url=f"{API_ENDPOINT}/guilds/{guild_id}/members/{user_id}",
headers=headers,
json=data
)
def restoreserver():
userids = config['users']
guildid = config['info']['guildid']
for idsinlist in userids:
print(idsinlist)
code = config[idsinlist]['refresh_tokens']
if config[idsinlist]['refresh'] == "false":
try:
data = exchange_code(code)
access_token = data.get("access_token")
add_to_guild(access_token, idsinlist, guildid)
config[idsinlist]['refresh_tokens'] = data.get("refresh_token")
config[idsinlist]['refresh'] = 'true'
with open('database.ini', 'w') as configfile:
config.write(configfile)
except:
print("error")
if config[idsinlist]['refresh'] == "true":
try:
data = get_new_token(code)
access_token = data.get("access_token")
add_to_guild(access_token, idsinlist, guildid)
config[idsinlist]['refresh_tokens'] = data.get("refresh_token")
with open('database.ini', 'w') as configfile:
config.write(configfile)
except:
print("error")
else:
print("Refresh status is invalid")
print(code)
if __name__ == '__main__':
cls()
application.run(host='0.0.0.0', port=80) #change to your port default port is 80