22
33const { resolve } = require ( 'node:path' ) ;
44
5- const { EmbedBuilder } = require ( 'discord.js' ) ;
5+ // eslint-disable-next-line no-unused-vars
6+ const { EmbedBuilder, GuildMember } = require ( 'discord.js' ) ;
67
78const qs = require ( 'qs' ) ;
89// eslint-disable-next-line no-unused-vars
@@ -43,7 +44,7 @@ server.use(
4344 resave : true ,
4445 proxy : true ,
4546 saveUninitialized : true ,
46- cookie : { maxAge : 60000 * 60 * 12 , secure : true } ,
47+ cookie : { maxAge : 60000 * 60 * 12 , secure : "auto" } ,
4748 } ) ,
4849) ;
4950
@@ -67,80 +68,115 @@ server.get('/verify', async (req, res) => {
6768 } ) ,
6869 } ;
6970
70- const response = await axios ( oauthApiOptions ) ;
71+ try {
72+ const response = await axios ( oauthApiOptions ) ;
7173
74+ const oauth_parsed = response . data ;
7275
73- const oauth_parsed = response . data ;
76+ /** @type {Axios.AxiosRequestConfig } */
77+ const apiUserOptions = {
78+ method : 'GET' ,
79+ url : 'https://discord.com/api/users/@me' ,
80+ headers : { authorization : `Bearer ${ oauth_parsed . access_token } ` } ,
81+ } ;
7482
75- /** @type {Axios.AxiosRequestConfig } */
76- const apiUserOptions = {
77- method : 'GET' ,
78- url : 'https://discord.com/api/users/@me' ,
79- headers : { authorization : `Bearer ${ oauth_parsed . access_token } ` } ,
80- } ;
83+ const response2 = await axios ( apiUserOptions ) ;
8184
82- const response2 = await axios ( apiUserOptions ) ;
85+ const parsed = response2 . data ;
8386
84- const parsed = response2 . data ;
87+ let fetchedGuild = client . guilds . cache . get ( process . env . SERVER_ID ) ;
8588
86- const fetchedGuild = client . guilds . cache . get ( process . env . SERVER_ID ) ;
89+ if ( ! fetchedGuild ) {
90+ fetchedGuild = await client . guilds . fetch ( process . env . SERVER_ID ) ;
91+ }
8792
88- const userfetch = await client . users . fetch ( parsed . id ) ;
93+ /** @type {GuildMember } */
94+ let userfetch ;
8995
90- await fetchedGuild ?. members . add ( userfetch , {
91- accessToken : oauth_parsed . access_token ,
92- } ) ;
96+ try {
97+ userfetch = await fetchedGuild . members . fetch ( parsed . id ) ;
98+ } catch {
99+ const userObject = await client . users . fetch ( parsed . id ) ;
100+ userfetch = await fetchedGuild . members . add ( userObject , {
101+ accessToken : oauth_parsed . access_token ,
102+ } ) ;
103+ }
93104
94- const userguild = await fetchedGuild ?. members . fetch ( userfetch ) ;
105+ if ( userfetch . roles . cache . has ( process . env . VERIFIED_ROLE_ID ) ) {
106+ res . render ( resolve ( './html/success.html' ) , {
107+ messageText : 'You already verified!' ,
108+ } ) ;
95109
96- if ( ! userguild ) {
97- res . render ( resolve ( './html/error.html' ) , {
98- messageText : 'You already verified!' ,
99- } ) ;
110+ return ;
111+ }
100112
101- return ;
113+ req . session . verify_userid = userfetch . id ;
114+
115+ if ( process . env . REQUIRE_VERIFIED_EMAIL === 'true' || parsed . verified ) {
116+ req . session . verify_status = 'waiting_captcha' ;
117+
118+ let sitekey = process . env . RECAPTCHA_SITEKEY ;
119+ let captcha_provider_script = 'https://www.google.com/recaptcha/api.js' ;
120+
121+ if ( process . env . CAPTCHA_PROVIDER === 'hcaptcha' ) {
122+ sitekey = process . env . HCAPTCHA_SITEKEY ;
123+ captcha_provider_script = 'https://js.hcaptcha.com/1/api.js' ;
124+ }
125+
126+ if ( process . env . CAPTCHA_PROVIDER === 'turnstile' ) {
127+ sitekey = process . env . TURNSTILE_SITEKEY ;
128+ captcha_provider_script = 'https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha' ;
129+ }
130+
131+ res . render ( resolve ( './html/captcha.html' ) , {
132+ captcha_provider : process . env . CAPTCHA_PROVIDER ,
133+ captcha_provider_script,
134+ captcha_sitekey : sitekey ,
135+ } ) ;
136+ } else {
137+ req . session . destroy ( ( ) => undefined ) ;
138+ res . render ( resolve ( './html/error.html' ) , {
139+ messageText : 'Please verify your email!' ,
140+ } ) ;
141+ }
142+ } catch ( error ) {
143+ console . log ( error ) ;
144+ res . redirect (
145+ `https://discord.com/oauth2/authorize?client_id=${ client . application ?. id } &redirect_uri=${ process . env . CALLBACK_URL } &response_type=code&scope=guilds.join%20email%20identify` ,
146+ ) ;
102147 }
148+ } ) ;
103149
104- if ( userguild . roles . cache . has ( process . env . VERIFIED_ROLE_ID ) ) {
105- res . render ( resolve ( './html/success.html' ) , {
106- messageText : 'You already verified!' ,
107- } ) ;
150+ server . post ( '/verify/solve' , async ( req , res ) => {
151+ let response_body_field = 'g-recaptcha-response' ;
152+ let captha_server = 'https://www.google.com/recaptcha/api/siteverify' ;
153+ let captcha_secret = process . env . RECAPTCHA_SECRET ;
108154
109- return ;
155+ if ( process . env . CAPTCHA_PROVIDER === 'hcaptcha' ) {
156+ response_body_field = 'h-captcha-response' ;
157+ captha_server = 'https://hcaptcha.com/siteverify' ;
158+ captcha_secret = process . env . HCAPTCHA_SECRET ;
110159 }
111160
112- req . session . verify_userid = parsed . id ;
113-
114- if ( process . env . REQUIRE_VERIFIED_EMAIL === 'true' || parsed . verified ) {
115- req . session . verify_status = 'waiting_recaptcha' ;
116- res . render ( resolve ( './html/captcha.html' ) , {
117- recaptcha_sitekey : process . env . RECAPTCHA_SITEKEY ,
118- } ) ;
119- } else {
120- req . session . destroy ( ( ) => undefined ) ;
121- res . render ( resolve ( './html/error.html' ) , {
122- messageText : 'Please verify your email!' ,
123- } ) ;
161+ if ( process . env . CAPTCHA_PROVIDER === 'turnstile' ) {
162+ response_body_field = 'g-recaptcha-response' ;
163+ captha_server = 'https://challenges.cloudflare.com/turnstile/v0/siteverify' ;
164+ captcha_secret = process . env . TURNSTILE_SECRET ;
124165 }
125- } ) ;
126166
127- server . post ( '/verify/solve/' , async ( req , res ) => {
128- console . log ( req . session . verify_userid ) ;
129- if ( ! req . session . verify_userid || ! req . body [ 'g-recaptcha-response' ] ) {
167+ console . log ( req . session . verify_userid , req . body [ response_body_field ] ) ;
168+
169+ if ( ! req . session . verify_userid || ! req . body [ response_body_field ] ) {
130170 return res . redirect ( '/verify' ) ;
131171 }
132172
133173 /** @type {Axios.AxiosRequestConfig } */
134174 const options = {
135175 method : 'POST' ,
136- url : 'https://www.google.com/recaptcha/api/siteverify' ,
137- headers : {
138- 'content-type' :
139- 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' ,
140- } ,
176+ url : captha_server ,
141177 data : qs . stringify ( {
142- secret : process . env . RECAPTCHA_SECRET ,
143- response : req . body [ 'g-recaptcha-response' ] ,
178+ secret : captcha_secret ,
179+ response : req . body [ response_body_field ] ,
144180 } ) ,
145181 } ;
146182
0 commit comments