Skip to content

Commit 8145f8e

Browse files
committed
direct join
1 parent 1e815ae commit 8145f8e

File tree

7 files changed

+38
-48
lines changed

7 files changed

+38
-48
lines changed

netlify/functions/discord-auth.js

+21-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// netlify/functions/discord-auth.js
22
const admin = require('firebase-admin');
3-
const { Client, GatewayIntentBits, Partials } = require("discord.js");
43

54
if (!admin.apps.length) {
65
admin.initializeApp({
@@ -9,19 +8,8 @@ if (!admin.apps.length) {
98
}
109
const db = admin.firestore();
1110

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-
2311
exports.handler = async function (event) {
24-
const { access_token, state, token_type } = event.queryStringParameters;
12+
const { access_token, state, token_type } = event.queryStringParameters;
2513

2614
if (!access_token || !state || !token_type) {
2715
return {
@@ -31,8 +19,6 @@ exports.handler = async function (event) {
3119
}
3220

3321
try {
34-
client.login(process.env.DISCORD_TOKEN).then();
35-
3622
const response = await fetch('https://discord.com/api/users/@me', {
3723
headers: {
3824
Authorization: `${token_type} ${access_token}`,
@@ -42,36 +28,37 @@ exports.handler = async function (event) {
4228
const discordId = response.id;
4329
const userId = decodeURIComponent(state);
4430

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());
4943

50-
const invite = await channel.createInvite({
51-
maxAge: 300, // 5 minutes
52-
maxUses: 1,
53-
});
44+
const inviteUrl = `https://discord.gg/${inviteResponse.code}`;
5445

5546
const userDoc = db.collection('users').doc(userId);
5647
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
6249
}, { merge: true });
6350

6451
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+
}),
6957
};
7058
} catch (error) {
71-
console.error(error);
7259
return {
7360
statusCode: 500,
74-
body: 'Internal Server Error',
61+
body: error.message || error,
7562
};
7663
}
7764
}

src/pages/404.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { useRouter } from 'next/router';
77
const Error = () => {
88
const router = useRouter();
99

10+
const error = router.query.error || "Looks Like You're Lost";
11+
1012
return (
1113
<Flex
1214
flexDirection='column'
@@ -18,7 +20,7 @@ const Error = () => {
1820
<TopBar />
1921
<Image src='/404.svg' height='400' width='300' alt='' />
2022
<Heading textAlign='center' color='#407BFF' size='2xl'>
21-
Looks Like You&apos;re Lost
23+
{error}
2224
</Heading>
2325
<Heading textAlign='center' size='md' fontWeight='300' mt='20px'>
2426
Here&apos;s is your way back to home

src/pages/auth/discord.tsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ const DiscordAuth = () => {
77
const router = useRouter();
88

99
useEffect(() => {
10-
if(typeof window === 'undefined')
10+
if (typeof window === 'undefined')
1111
return;
1212

1313
const query = window.location.hash.slice(1);
14-
router.push(`/.netlify/functions/discord-auth?${query}`).then();
14+
fetch(`/.netlify/functions/discord-auth?${query}`).then(async (res) => {
15+
if(res.status !== 200)
16+
return router.push(`/404?error=${await res.text()}`);
17+
18+
const data = await res.json();
19+
20+
if (!data.url)
21+
return router.push("/404?error=Unable to Create Invite");
22+
23+
return router.push(data.url);
24+
})
1525
}, [router]);
1626

1727
return (

src/pages/profile.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ const Index = () => {
111111
const skillsArr = val?.skills?.map((el: any) => el.value);
112112
const dbData: Form = {
113113
discordId: user?.discordId || null,
114-
discordInvite: user?.discordInvite || null,
115114
team: user?.team || null,
116115
accept: user?.accept || false,
117116
college: college || user?.college || null,

src/pages/wizard.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ const Wizard = () => {
6161
const skillsArr = val.skills?.map((el: { value: string }) => el.value);
6262
const dbData: Form = {
6363
discordId: null,
64-
discordInvite: null,
6564
team: null,
6665
accept: null,
6766
college: college || null,

src/types/index.ts

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ export interface Errors {
1010

1111
export interface Form {
1212
discordId: string | null;
13-
discordInvite: {
14-
url: string;
15-
expiry: number;
16-
} | null;
1713
team: string | null;
1814
name: string;
1915
passYear: number | null;

src/views/profile/components/ProfileBar.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {BiArrowBack} from 'react-icons/bi';
44
import {FiEdit} from 'react-icons/fi';
55
import {AiOutlineSave} from 'react-icons/ai';
66
import {useRouter} from 'next/router';
7-
import {CopyText} from "@/components/copy";
87
import {Form} from "@/types";
98

109
interface BarProp {
@@ -29,8 +28,6 @@ export const ProfileBar = ({
2928
return router.push(`https://discord.com/api/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=token&scope=identify&state=${state}`);
3029
};
3130

32-
const valid = user.discordInvite?.expiry && user.discordInvite.expiry > Date.now();
33-
3431
return (
3532
<Box
3633
display="flex"
@@ -60,9 +57,9 @@ export const ProfileBar = ({
6057
color: 'white',
6158
borderColor: 'transparent',
6259
}}
63-
onClick={valid ? () => router.push( user.discordInvite?.url || '') : handleConnectDiscord}
60+
onClick={handleConnectDiscord}
6461
>
65-
{valid ? "Join Discord" : "Connect Discord"}
62+
Join Discord
6663
</Button>
6764
{isEdit && (
6865
<Button

0 commit comments

Comments
 (0)