@@ -16,34 +16,12 @@ const koa_router_1 = __importDefault(require("koa-router"));
1616 const axios_1 = __importDefault(require("axios"));
1717 const querystring_1 = require("querystring");
1818 const router = new koa_router_1.default({ prefix: "/discord" });
19-router.get("/authorize", (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
20- var _a;
21- if (!ctx.session) {
22- ctx.status = 403;
23- return yield next();
24- }
25- // Front end will send the user here, and then from here into the authorization flow:
26- // https://discord.com/api/oauth2/authorize?
27- // client_id=1064167640148476004&
28- // redirect_uri=http%3A%2F%2Flocalhost%3A3001%2Fdiscord%2Fcallback&
29- // response_type=code&
30- // scope=guilds.join%20guilds.members.read
31- const authorize_url = "https://discord.com/api/oauth2/authorize" +
32- `?client_id=${process.env.DISCORD_CLIENT_ID}` +
33- `&redirect_uri=${encodeURI(process.env.DISCORD_REDIRECT_URL || "")}` +
34- `&response_type=code` +
35- `&scope=identify guilds guilds.join guilds.members.read` +
36- `&state=${(_a = ctx.session) === null || _a === void 0 ? void 0 : _a.user.id}`;
37- ctx.redirect(authorize_url);
38- yield next();
39-}));
4019 router.get("/callback", (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
4120 const code = ctx.request.query.code;
4221 const state = Number(ctx.request.query.state);
4322 if (code) {
44- // Success, we should exchange the code for a token:
45- // https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-access-token-exchange-example
46- const response = yield axios_1.default.post("https://discord.com/api/v10/oauth2/token", (0, querystring_1.stringify)({
23+ const response = yield axios_1.default
24+ .post("https://discord.com/api/v10/oauth2/token", (0, querystring_1.stringify)({
4725 client_id: process.env.DISCORD_CLIENT_ID,
4826 client_secret: process.env.DISCORD_CLIENT_SECRET,
4927 grant_type: "authorization_code",
@@ -51,6 +29,10 @@ router.get("/callback", (ctx, next) => __awaiter(void 0, void 0, void 0, functio
5129 redirect_uri: process.env.DISCORD_REDIRECT_URL,
5230 }), {
5331 headers: { "Content-Type": "application/x-www-form-urlencoded" },
32+ })
33+ .catch((e) => {
34+ console.log(e);
35+ throw e;
5436 });
5537 const discordToken = yield ctx.prisma.discordToken.create({
5638 data: {
@@ -67,39 +49,42 @@ router.get("/callback", (ctx, next) => __awaiter(void 0, void 0, void 0, functio
6749 });
6850 ctx.status = 201;
6951 }
70- ctx.redirect(process.env.CLIENT_URL || "");
52+ ctx.redirect("/discord/join");
7153 yield next();
7254 }));
73-router.post("/join", (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
55+router.get("/join", (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
7456 if (!ctx.session) {
7557 ctx.status = 403;
7658 return yield next();
7759 }
78- // Use the token acquired to join the server
79- const accessToken = ctx.session.user.discordToken.accessToken;
80- const discordUserResponse = yield axios_1.default.get("https://discord.com/api/v10/users/@me", {
81- headers: {
82- Authorization: `Bearer ${ctx.session.user.discordToken.accessToken}`,
83- "Content-Type": "application/x-www-form-urlencoded",
84- },
60+ const user = yield ctx.prisma.user.findUnique({
61+ where: { walletAddress: ctx.session.user.walletAddress },
62+ include: { discordToken: true },
8563 });
86- const discordGuildAddResponse = yield axios_1.default.put(`https://discord.com/api/v10/guilds/${process.env.QUEST_GUILD_ID}/members/${discordUserResponse === null || discordUserResponse === void 0 ? void 0 : discordUserResponse.data.id}`, {
87- access_token: accessToken,
88- }, {
64+ if (!user.discordToken) {
65+ const authorize_url = "https://discord.com/api/oauth2/authorize" +
66+ `?client_id=${process.env.DISCORD_CLIENT_ID}` +
67+ `&redirect_uri=${encodeURI(process.env.DISCORD_REDIRECT_URL || "")}` +
68+ `&response_type=code` +
69+ `&scope=identify bot guilds guilds.members.read` +
70+ `&state=${user.id}`;
71+ ctx.redirect(authorize_url);
72+ return yield next();
73+ }
74+ const inviteCode = yield axios_1.default
75+ .post(`https://discord.com/api/v10/channels/${process.env.QUEST_CHANNEL_ID}/invites`, {}, {
8976 headers: {
9077 Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN}`,
9178 "Content-Type": "application/json",
9279 },
80+ })
81+ .then((response) => response.data.code)
82+ .catch((e) => {
83+ console.log(e);
84+ throw e;
9385 });
94- if ((discordGuildAddResponse === null || discordGuildAddResponse === void 0 ? void 0 : discordGuildAddResponse.status) === 201) {
95- // user added successfully
96- console.log("added");
97- }
98- else if ((discordGuildAddResponse === null || discordGuildAddResponse === void 0 ? void 0 : discordGuildAddResponse.status) === 204) {
99- // user already in guild
100- console.log("existing");
101- }
102- ctx.status = 200;
86+ ctx.session.user = user;
87+ ctx.redirect(`http://discord.gg/${inviteCode}`);
10388 yield next();
10489 }));
10590 router.get("/verify", (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
@@ -107,18 +92,31 @@ router.get("/verify", (ctx, next) => __awaiter(void 0, void 0, void 0, function*
10792 ctx.status = 403;
10893 return yield next();
10994 }
110- // use the discord api to see the list of members and check if user is in the list
111- // Needs to use the token generated in the callback route
11295 const guildId = process.env.QUEST_GUILD_ID;
11396 // Use the token acquired to get the user and it's joined guilds
11497 const authorizationHeader = {
11598 Authorization: `Bearer ${ctx.session.user.discordToken.accessToken}`,
11699 "Content-Type": "application/x-www-form-urlencoded",
117100 };
118- const discordUserResponse = yield axios_1.default.get(`https://discord.com/api/v10/users/@me/guilds/${guildId}/member`, {
101+ yield axios_1.default
102+ .get(`https://discord.com/api/v10/users/@me/guilds/${guildId}/member`, {
119103 headers: authorizationHeader,
104+ })
105+ .catch((e) => {
106+ console.log(e);
107+ throw e;
108+ });
109+ // 10 random characters alphanumeric string
110+ const coupon = Math.random()
111+ .toString(36)
112+ .substring(2, 12)
113+ .toLocaleUpperCase();
114+ console.log(coupon);
115+ ctx.body = ctx.session.user = yield ctx.prisma.user.update({
116+ where: { walletAddress: ctx.session.user.walletAddress },
117+ include: { discordToken: true },
118+ data: { verified: true, coupon },
120119 });
121- ctx.body = discordUserResponse.data;
122120 ctx.status = 200;
123121 yield next();
124122 }));