1
1
// netlify/functions/discord-auth.js
2
2
const admin = require ( 'firebase-admin' ) ;
3
- const { Client, GatewayIntentBits, Partials } = require ( "discord.js" ) ;
4
3
5
4
if ( ! admin . apps . length ) {
6
5
admin . initializeApp ( {
@@ -9,19 +8,8 @@ if (!admin.apps.length) {
9
8
}
10
9
const db = admin . firestore ( ) ;
11
10
12
- const client = new Client ( {
13
- intents : [
14
- GatewayIntentBits . DirectMessages ,
15
- GatewayIntentBits . Guilds ,
16
- GatewayIntentBits . GuildMessages ,
17
- GatewayIntentBits . MessageContent ,
18
- GatewayIntentBits . GuildMembers ,
19
- ] ,
20
- partials : [ Partials . Channel ] ,
21
- } ) ;
22
-
23
11
exports . handler = async function ( event ) {
24
- const { access_token, state, token_type } = event . queryStringParameters ;
12
+ const { access_token, state, token_type } = event . queryStringParameters ;
25
13
26
14
if ( ! access_token || ! state || ! token_type ) {
27
15
return {
@@ -31,8 +19,6 @@ exports.handler = async function (event) {
31
19
}
32
20
33
21
try {
34
- client . login ( process . env . DISCORD_TOKEN ) . then ( ) ;
35
-
36
22
const response = await fetch ( 'https://discord.com/api/users/@me' , {
37
23
headers : {
38
24
Authorization : `${ token_type } ${ access_token } ` ,
@@ -42,36 +28,37 @@ exports.handler = async function (event) {
42
28
const discordId = response . id ;
43
29
const userId = decodeURIComponent ( state ) ;
44
30
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 ) ;
31
+ // Create an invitation link
32
+ const inviteResponse = await fetch ( `https://discord.com/api/v10/channels/${ process . env . START_CHANNEL } /invites` , {
33
+ method : 'POST' ,
34
+ headers : {
35
+ 'Authorization' : `Bot ${ process . env . DISCORD_TOKEN } ` ,
36
+ 'Content-Type' : 'application/json' ,
37
+ } ,
38
+ body : JSON . stringify ( {
39
+ max_age : 300 , // 5 minutes
40
+ max_uses : 1 ,
41
+ } ) ,
42
+ } ) . then ( ( res ) => res . json ( ) ) ;
49
43
50
- const invite = await channel . createInvite ( {
51
- maxAge : 300 , // 5 minutes
52
- maxUses : 1 ,
53
- } ) ;
44
+ const inviteUrl = `https://discord.gg/${ inviteResponse . code } ` ;
54
45
55
46
const userDoc = db . collection ( 'users' ) . doc ( userId ) ;
56
47
await userDoc . set ( {
57
- discordId,
58
- discordInvite : {
59
- url : invite . url ,
60
- expiry : Date . now ( ) + 300 * 1000 , // Current timestamp + 5 minutes in milliseconds
61
- }
48
+ discordId
62
49
} , { merge : true } ) ;
63
50
64
51
return {
65
- statusCode : 302 ,
66
- headers : {
67
- Location : `${ process . env . URL } /profile`
68
- }
52
+ statusCode : 200 ,
53
+ body : JSON . stringify ( {
54
+ url : inviteUrl ,
55
+ expiry : Date . now ( ) + 300 * 1000 , // Current timestamp + 5 minutes in milliseconds
56
+ } ) ,
69
57
} ;
70
58
} catch ( error ) {
71
- console . error ( error ) ;
72
59
return {
73
60
statusCode : 500 ,
74
- body : 'Internal Server Error' ,
61
+ body : error . message || error ,
75
62
} ;
76
63
}
77
64
}
0 commit comments