2021-10-15 14:08:27 +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
SERVER_ID = environ . get ( " DISCORD_SERVER_ID " , ' ' ) . strip ( )
CLIENT_ID = environ . get ( " DISCORD_CLIENT_ID " , ' ' ) . strip ( )
CLIENT_SECRET = environ . get ( " DISCORD_CLIENT_SECRET " , ' ' ) . strip ( )
BOT_TOKEN = environ . get ( " DISCORD_BOT_TOKEN " ) . strip ( )
COINS_NAME = environ . get ( " COINS_NAME " ) . strip ( )
DISCORD_ENDPOINT = " https://discordapp.com/api/v6 "
WELCOME_CHANNEL = " 846509313941700618 "
2021-10-21 17:01:25 +00:00
SITE_NAME = environ . get ( " SITE_NAME " , " " ) . strip ( )
2021-10-15 14:08:27 +00:00
@app.get ( " /discord " )
@auth_required
def join_discord ( v ) :
2021-10-18 18:39:49 +00:00
if v . is_suspended != 0 and v . admin_level == 0 : return " Banned users cannot join the discord server! "
2021-10-15 14:08:27 +00:00
2021-12-04 20:48:10 +00:00
if SITE_NAME == ' Drama ' and v . admin_level == 0 and v . patron == 0 and v . truecoins < 150 : return f " You must receive 150 upvotes/downvotes from other users before being able to join the Discord server. "
2021-10-15 14:08:27 +00:00
if v . shadowbanned or v . agendaposter : return " "
now = int ( time . time ( ) )
state = generate_hash ( f " { now } + { v . id } +discord " )
state = f " { now } . { state } "
return redirect ( f " https://discord.com/api/oauth2/authorize?client_id= { CLIENT_ID } &redirect_uri=https%3A%2F%2F { app . config [ ' SERVER_NAME ' ] } %2Fdiscord_redirect&response_type=code&scope=identify%20guilds.join&state= { state } " )
@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 = {
" client_id " : CLIENT_ID ,
' client_secret ' : CLIENT_SECRET ,
' grant_type ' : ' authorization_code ' ,
' code ' : code ,
' redirect_uri ' : f " https:// { app . config [ ' SERVER_NAME ' ] } /discord_redirect " ,
' scope ' : ' identify guilds.join '
}
headers = {
' Content-Type ' : ' application/x-www-form-urlencoded '
}
url = " https://discord.com/api/oauth2/token "
2021-11-14 01:19:32 +00:00
x = requests . post ( url , headers = headers , data = data , timeout = 5 )
2021-10-15 14:08:27 +00:00
x = x . json ( )
try :
token = x [ " access_token " ]
except KeyError :
abort ( 403 )
url = " https://discord.com/api/users/@me "
headers = {
' Authorization ' : f " Bearer { token } "
}
2021-11-14 01:19:32 +00:00
x = requests . get ( url , headers = headers , timeout = 5 )
2021-10-15 14:08:27 +00:00
x = x . json ( )
headers = {
' Authorization ' : f " Bot { BOT_TOKEN } " ,
' Content-Type ' : " application/json "
}
if v . discord_id and v . discord_id != x [ ' id ' ] :
url = f " https://discord.com/api/guilds/ { SERVER_ID } /members/ { v . discord_id } "
2021-11-14 01:19:32 +00:00
requests . delete ( url , headers = headers , timeout = 5 )
2021-10-15 14:08:27 +00:00
2021-11-06 15:52:48 +00:00
if g . db . query ( User ) . filter ( User . id != v . id , User . discord_id == x [ " id " ] ) . first ( ) :
2021-12-19 13:01:28 +00:00
if v and v . oldsite : template = ' '
else : template = ' CHRISTMAS/ '
return render_template ( f " { template } message.html " , title = " Discord account already linked. " , error = " That Discord account is already in use by another user. " , v = v )
2021-10-15 14:08:27 +00:00
v . discord_id = x [ " id " ]
g . db . add ( v )
url = f " https://discord.com/api/guilds/ { SERVER_ID } /members/ { x [ ' id ' ] } "
name = v . username
data = {
" access_token " : token ,
" nick " : name ,
}
2021-11-14 01:19:32 +00:00
x = requests . put ( url , headers = headers , json = data , timeout = 5 )
2021-10-15 14:08:27 +00:00
if x . status_code in [ 201 , 204 ] :
2021-11-15 22:24:34 +00:00
if v . admin_level > 2 :
2021-10-29 04:44:49 +00:00
add_role ( v , " owner " )
2021-10-15 14:08:27 +00:00
time . sleep ( 0.1 )
if v . admin_level > 0 : add_role ( v , " admin " )
time . sleep ( 0.1 )
add_role ( v , " linked " )
if v . patron :
time . sleep ( 0.1 )
add_role ( v , str ( v . patron ) )
else :
return x . json ( )
if x . status_code == 204 :
url = f " https://discord.com/api/guilds/ { SERVER_ID } /members/ { v . discord_id } "
data = {
" nick " : name
}
2021-11-14 01:19:32 +00:00
requests . patch ( url , headers = headers , json = data , timeout = 5 )
2021-10-15 14:08:27 +00:00
g . db . commit ( )
2021-07-21 01:12:26 +00:00
return redirect ( f " https://discord.com/channels/ { SERVER_ID } / { WELCOME_CHANNEL } " )