2021-08-04 15:35:10 +00:00
from files . helpers . wrappers import *
from files . helpers . security import *
from files . helpers . discord import add_role
from files . __main__ import app
2021-07-21 01:12:26 +00:00
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 ( )
2021-08-05 14:43:54 +00:00
COINS_NAME = environ . get ( " COINS_NAME " ) . strip ( )
2021-07-21 01:12:26 +00:00
DISCORD_ENDPOINT = " https://discordapp.com/api/v6 "
WELCOME_CHANNEL = " 846509313941700618 "
2021-07-27 22:31:28 +00:00
@app.get ( " /discord " )
2021-07-21 01:12:26 +00:00
@auth_required
def join_discord ( v ) :
2021-09-07 02:41:21 +00:00
if v . is_suspended != 0 : return " You ' re banned "
2021-08-31 20:46:04 +00:00
2021-08-31 20:32:34 +00:00
if ' rdrama ' in request . host and v . admin_level == 0 and v . patron == 0 and v . coins < 150 : return f " You must earn 150 { COINS_NAME } before entering the Discord server. You earn { COINS_NAME } by making posts/comments and getting upvoted. "
2021-07-21 01:12:26 +00:00
now = int ( time . time ( ) )
state = generate_hash ( f " { now } + { v . id } +discord " )
state = f " { now } . { state } "
2021-08-14 22:45:56 +00:00
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 } " )
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get ( " /discord_redirect " )
2021-07-21 01:12:26 +00:00
@auth_required
def discord_redirect ( v ) :
#validate state
now = int ( time . time ( ) )
2021-09-19 13:11:34 +00:00
state = request . values . get ( ' state ' , ' ' ) . split ( ' . ' )
2021-07-21 01:12:26 +00:00
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 )
#get discord token
2021-09-19 13:11:34 +00:00
code = request . values . get ( " code " , " " )
2021-07-21 01:12:26 +00:00
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 "
x = requests . post ( url , headers = headers , data = data )
x = x . json ( )
try :
token = x [ " access_token " ]
except KeyError :
abort ( 403 )
#get user ID
url = " https://discord.com/api/users/@me "
headers = {
' Authorization ' : f " Bearer { token } "
}
x = requests . get ( url , headers = headers )
x = x . json ( )
#add user to discord
headers = {
' Authorization ' : f " Bot { BOT_TOKEN } " ,
' Content-Type ' : " application/json "
}
#remove existing user if applicable
if v . discord_id and v . discord_id != x [ ' id ' ] :
url = f " https://discord.com/api/guilds/ { SERVER_ID } /members/ { v . discord_id } "
requests . delete ( url , headers = headers )
2021-09-17 08:29:05 +00:00
if g . db . query ( User ) . options ( lazyload ( ' * ' ) ) . filter ( User . id != v . id , User . discord_id == x [ " id " ] ) . first ( ) :
2021-07-21 01:12:26 +00:00
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 )
url = f " https://discord.com/api/guilds/ { SERVER_ID } /members/ { x [ ' id ' ] } "
name = v . username
data = {
" access_token " : token ,
" nick " : name ,
}
x = requests . put ( url , headers = headers , json = data )
if x . status_code in [ 201 , 204 ] :
2021-08-31 18:59:37 +00:00
if v . id == 1 : add_role ( v , " shrigma " )
2021-08-31 19:02:00 +00:00
time . sleep ( 0.1 )
2021-08-31 19:00:54 +00:00
if v . admin_level > 0 : add_role ( v , " admin " )
2021-08-30 13:06:22 +00:00
else : add_role ( v , " newuser " )
2021-07-21 01:12:26 +00:00
time . sleep ( 0.1 )
2021-08-18 15:32:27 +00:00
if v . patron : add_role ( v , str ( v . patron ) )
2021-07-21 01:12:26 +00:00
add_role ( v , " feedback " )
2021-09-21 20:48:05 +00:00
time . sleep ( 0.3 )
2021-07-21 01:12:26 +00:00
2021-08-04 16:00:57 +00:00
if v . coins > 100 : add_role ( v , " linked " )
2021-07-21 01:12:26 +00:00
else : add_role ( v , " norep " )
else :
2021-07-31 05:28:05 +00:00
return x . json ( )
2021-07-21 01:12:26 +00:00
#check on if they are already there
#print(x.status_code)
if x . status_code == 204 :
##if user is already a member, remove old roles and update nick
url = f " https://discord.com/api/guilds/ { SERVER_ID } /members/ { v . discord_id } "
data = {
" nick " : name
}
2021-08-28 12:01:39 +00:00
requests . patch ( url , headers = headers , json = data )
2021-07-21 01:12:26 +00:00
2021-09-16 17:15:07 +00:00
g . db . commit ( )
2021-07-21 01:12:26 +00:00
return redirect ( f " https://discord.com/channels/ { SERVER_ID } / { WELCOME_CHANNEL } " )