2022-05-04 23:09:46 +00:00
|
|
|
from files.helpers.wrappers import *
|
|
|
|
from files.helpers.security import *
|
|
|
|
from files.helpers.discord import add_role
|
|
|
|
from files.__main__ import app
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/discord")
|
|
|
|
@is_not_permabanned
|
|
|
|
def join_discord(v):
|
|
|
|
|
|
|
|
if v.shadowbanned: return {"error": "Internal server error"}
|
|
|
|
|
|
|
|
now=int(time.time())
|
|
|
|
|
|
|
|
state=generate_hash(f"{now}+{v.id}+discord")
|
|
|
|
|
|
|
|
state=f"{now}.{state}"
|
|
|
|
|
2022-07-08 16:21:13 +00:00
|
|
|
return redirect(f"https://discord.com/api/oauth2/authorize?client_id={DISCORD_CLIENT_ID}&redirect_uri=https%3A%2F%2F{SITE}%2Fdiscord_redirect&response_type=code&scope=identify%20guilds.join&state={state}")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/discord_redirect")
|
|
|
|
@auth_required
|
|
|
|
def discord_redirect(v):
|
|
|
|
|
|
|
|
|
|
|
|
now=int(time.time())
|
|
|
|
state=request.values.get('state','').split('.')
|
|
|
|
|
|
|
|
timestamp=state[0]
|
|
|
|
|
|
|
|
state=state[1]
|
|
|
|
|
|
|
|
if int(timestamp) < now-600:
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
if not validate_hash(f"{timestamp}+{v.id}+discord", state):
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
code = request.values.get("code","")
|
|
|
|
if not code:
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
data={
|
2022-07-08 16:21:13 +00:00
|
|
|
"client_id": DISCORD_CLIENT_ID,
|
|
|
|
'client_secret': DISCORD_CLIENT_SECRET,
|
2022-05-04 23:09:46 +00:00
|
|
|
'grant_type': 'authorization_code',
|
|
|
|
'code': code,
|
2022-06-24 16:15:37 +00:00
|
|
|
'redirect_uri': f"https://{SITE}/discord_redirect",
|
2022-05-04 23:09:46 +00:00
|
|
|
'scope': 'identify guilds.join'
|
|
|
|
}
|
|
|
|
headers={
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
|
|
|
url="https://discord.com/api/oauth2/token"
|
|
|
|
|
|
|
|
x=requests.post(url, headers=headers, data=data, timeout=5)
|
|
|
|
|
|
|
|
x=x.json()
|
|
|
|
|
|
|
|
|
|
|
|
token=x["access_token"]
|
|
|
|
|
|
|
|
|
|
|
|
url="https://discord.com/api/users/@me"
|
|
|
|
headers={
|
|
|
|
'Authorization': f"Bearer {token}"
|
|
|
|
}
|
|
|
|
x=requests.get(url, headers=headers, timeout=5)
|
|
|
|
|
|
|
|
x=x.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
headers={
|
2022-07-08 16:21:13 +00:00
|
|
|
'Authorization': f"Bot {DISCORD_BOT_TOKEN}",
|
2022-05-04 23:09:46 +00:00
|
|
|
'Content-Type': "application/json"
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.discord_id and v.discord_id != x['id']:
|
2022-07-08 16:21:13 +00:00
|
|
|
url=f"https://discord.com/api/guilds/{DISCORD_SERVER_ID}/members/{v.discord_id}"
|
2022-05-04 23:09:46 +00:00
|
|
|
requests.delete(url, headers=headers, timeout=5)
|
|
|
|
|
|
|
|
if g.db.query(User).filter(User.id!=v.id, User.discord_id==x["id"]).one_or_none():
|
|
|
|
return render_template("message.html", title="Discord account already linked.", error="That Discord account is already in use by another user.", v=v)
|
|
|
|
|
|
|
|
v.discord_id=x["id"]
|
|
|
|
g.db.add(v)
|
|
|
|
|
2022-07-08 16:21:13 +00:00
|
|
|
url=f"https://discord.com/api/guilds/{DISCORD_SERVER_ID}/members/{x['id']}"
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
name=v.username
|
|
|
|
|
|
|
|
data={
|
|
|
|
"access_token":token,
|
|
|
|
"nick":name,
|
|
|
|
}
|
|
|
|
|
|
|
|
x=requests.put(url, headers=headers, json=data, timeout=5)
|
|
|
|
|
|
|
|
if x.status_code in {201, 204}:
|
2022-08-18 16:52:26 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
add_role(v, "linked")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-08-18 16:52:26 +00:00
|
|
|
if v.patron:
|
2022-05-04 23:09:46 +00:00
|
|
|
time.sleep(0.1)
|
2022-08-18 16:52:26 +00:00
|
|
|
add_role(v, str(v.patron))
|
2022-06-05 15:05:43 +00:00
|
|
|
|
2022-08-18 16:52:26 +00:00
|
|
|
if SITE == 'rdrama.net' and v.id == AEVANN_ID:
|
|
|
|
time.sleep(0.1)
|
|
|
|
add_role(v, "admin")
|
2022-06-07 10:55:50 +00:00
|
|
|
|
2022-08-18 16:52:26 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
requests.put("https://discord.com/api/guilds/913091440035389520/members/788152118669606932", headers=headers, json={"access_token":token,"roles":[915260962540511292]}, timeout=5)
|
2022-05-04 23:09:46 +00:00
|
|
|
else:
|
|
|
|
return x.json()
|
|
|
|
|
|
|
|
|
|
|
|
if x.status_code==204:
|
|
|
|
|
2022-07-08 16:21:13 +00:00
|
|
|
url=f"https://discord.com/api/guilds/{DISCORD_SERVER_ID}/members/{v.discord_id}"
|
2022-05-04 23:09:46 +00:00
|
|
|
data={
|
|
|
|
"nick": name
|
|
|
|
}
|
|
|
|
|
|
|
|
requests.patch(url, headers=headers, json=data, timeout=5)
|
|
|
|
|
|
|
|
|
2022-07-08 16:21:13 +00:00
|
|
|
return redirect(f"https://discord.com/channels/{DISCORD_SERVER_ID}/{DISCORD_WELCOME_CHANNEL}")
|