From 78d0cdf5e0e57065a69e2e2b225f167dc7801333 Mon Sep 17 00:00:00 2001 From: danbulant Date: Tue, 1 Sep 2020 21:38:22 +0200 Subject: [PATCH] Implement login --- index.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++---- package.json | 1 + 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index e3592e9..7ecbf5e 100644 --- a/index.js +++ b/index.js @@ -1,28 +1,75 @@ const express = require("express"); +const cookieParser = require('cookie-parser') const discord = require("discord.js"); const Auth = require("discord-oauth2"); const crypto = require('crypto'); const config = require("./config.json"); -const client = new discord.Client(); +const client = new discord.Client({ + ws: { + intents: [ + 'GUILDS' + ] + } +}); const app = express(); const auth = new Auth({ clientId: config.cid, clientSecret: config.secret }); +app.use(cookieParser()); + app.get("/login", (req, res) => { var state = crypto.randomBytes(64).toString('hex'); res.cookie("state", state); res.redirect(auth.generateAuthUrl({ scope: ["connections", "identify"], - state + state, + redirectUri: "http://localhost:8080/callback" })); }); -app.get("/callback", (req, res) => { +app.get("/callback", async (req, res) => { + const { code, state } = req.query; + if(state !== req.cookies.state) return res.status(400).send("Wrong state"); + try { + var token = await auth.tokenRequest({ + code, + scope: [ "connections", "identify" ], + grantType: "authorization_code", + redirectUri: "http://localhost:8080/callback" + }); + } catch(e) { + return res.status(403).send("Invalid code"); + } + var connections = await auth.getUserConnections(token.access_token); + var reddit = connections.filter(val => val.type === "reddit"); + if(!reddit) return res.send("No reddit connection"); + reddit = reddit[0]; + var user = await auth.getUser(token.access_token); + try { + var guild = await client.guilds.fetch(config.guild); + } catch(e) { + return res.status(500).send("Guild not found"); + } + try { + var member = await guild.members.fetch(user.id); + } catch(e) { + return res.status(403).send("Member not found"); + } + try { + await member.setNickname("u/" + reddit.name); + } catch(e) { + console.log(e); + return res.status(500).send("Missing nickname permission"); + } + res.send("Done"); }); +client.login(config.token); -client.login(config.token); \ No newline at end of file +app.listen(8080, () => { + console.log("http://localhost:8080/login"); +}) \ No newline at end of file diff --git a/package.json b/package.json index 3d7f886..b2cf271 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { + "cookie-parser": "^1.4.5", "discord-oauth2": "^2.3.0", "discord.js": "^12.3.1", "express": "^4.17.1"