@@ -19,9 +19,6 @@ const client = new Client({
19
19
] ,
20
20
partials : [ Partials . Channel ] ,
21
21
} ) ;
22
- client . login ( process . env . DISCORD_TOKEN ) . then ( ) ;
23
-
24
- const ready = new Promise ( ( resolve ) => client . once ( 'ready' , resolve ) ) ;
25
22
26
23
exports . handler = async function ( event ) {
27
24
const { access_token, state, token_type } = event . queryStringParameters ;
@@ -34,20 +31,21 @@ exports.handler = async function (event) {
34
31
}
35
32
36
33
try {
34
+ client . login ( process . env . DISCORD_TOKEN ) . then ( ) ;
35
+
37
36
const response = await fetch ( 'https://discord.com/api/users/@me' , {
38
37
headers : {
39
38
Authorization : `${ token_type } ${ access_token } ` ,
40
39
} ,
41
40
} ) . then ( ( res ) => res . json ( ) ) ;
42
41
43
- console . log ( response ) ;
44
-
45
42
const discordId = response . id ;
46
43
const userId = decodeURIComponent ( state ) ;
47
44
48
- await ready
49
- const server = client . guilds . cache . get ( process . env . guildID ) ;
50
- const channel = await server . channels . fetch ( process . env . START_CHANNEL ) ;
45
+ await new Promise ( async ( resolve ) => client . once ( 'ready' , resolve ) ) ;
46
+
47
+ const server = client . guilds . cache . get ( process . env . guildID ) || await client . guilds . fetch ( process . env . guildID ) ;
48
+ const channel = server . channels . cache . get ( process . env . START_CHANNEL ) || await server . channels . fetch ( process . env . START_CHANNEL ) ;
51
49
52
50
const invite = await channel . createInvite ( {
53
51
maxAge : 300 , // 5 minutes
@@ -63,15 +61,14 @@ exports.handler = async function (event) {
63
61
}
64
62
} , { merge : true } ) ;
65
63
66
- client . destroy ( ) . then ( ) ;
67
-
68
64
return {
69
65
statusCode : 302 ,
70
66
headers : {
71
67
Location : `${ process . env . URL } /profile`
72
68
}
73
69
} ;
74
70
} catch ( error ) {
71
+ console . error ( error ) ;
75
72
return {
76
73
statusCode : 500 ,
77
74
body : 'Internal Server Error' ,
0 commit comments