diff --git a/index.js b/index.js index c931c58..2a85b11 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,19 @@ 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_ID, + CLIENT_SECRET, + TOKEN, + GUILD_ID, + + PROTOCOL, + HOSTNAME, + LOGIN_PATH, + CALLBACK_PATH, + PORT +} = require("./config.json"); const client = new discord.Client({ ws: { @@ -14,61 +26,77 @@ const client = new discord.Client({ }); const app = express(); const auth = new Auth({ - clientId: config.cid, - clientSecret: config.secret + clientId: CLIENT_ID, + clientSecret: CLIENT_SECRET }); +/* + * Make sure to add a this URL to your Discord Application like show on this page: + * https://discordjs.guide/oauth2/#getting-an-oauth2-url + */ +const redirectUri = `${PROTOCOL}://${HOSTNAME}:${PORT}${CALLBACK_PATH}`; + app.use(cookieParser()); -app.get("/login", (req, res) => { - var state = crypto.randomBytes(64).toString('hex'); +app.get(LOGIN_PATH, (req, res) => { + const state = crypto.randomBytes(64).toString('hex'); res.cookie("state", state); res.redirect(auth.generateAuthUrl({ scope: ["connections", "identify"], state, - redirectUri: "http://localhost:8080/callback" + redirectUri })); }); -app.get("/callback", async (req, res) => { +app.get(CALLBACK_PATH, async (req, res) => { const { code, state } = req.query; + // Alternatively, use res.redirect() to use a fancier error page. Same thing goes for all the other error handlers. 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" + redirectUri }); } 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"); + const connections = await auth.getUserConnections(token.access_token); + + let reddit = connections.filter(val => val.type === "reddit"); + if(!reddit[0]) return res.send("No reddit connection found"); reddit = reddit[0]; - var user = await auth.getUser(token.access_token); + const user = await auth.getUser(token.access_token); try { - var guild = await client.guilds.fetch(config.guild); + var guild = await client.guilds.fetch(GUILD_ID); } catch(e) { - return res.status(500).send("Guild not found"); + return res.status(500).send("Discord guild not found"); } try { var member = await guild.members.fetch(user.id); } catch(e) { - return res.status(403).send("Member not found"); + return res.status(403).send("Discord member not found"); } try { + /* + Add whatever discord logic you want here. You could for example add a "Verfied" role: + await member.roles.add("roleId") + or DM the user: (make sure to do error handling if bot can't DM user) + await member.send("Succesfully verified you as reddit user u/" + reddit.name + " in guild " + guild.name) + + If you're gonna change the user's nickname like this, remember to limit their "Change nickname" perms - otherwise they can just change it back. + */ await member.setNickname("u/" + reddit.name); } catch(e) { - return res.status(500).send("Missing nickname permission"); + return res.status(500).send("Couldn't change nickname."); } - res.send("Done"); + res.send("Success!"); }); -client.login(config.token); +client.login(TOKEN); -app.listen(8080, () => { - console.log("http://localhost:8080/login"); -}) \ No newline at end of file +app.listen(PORT, () => { + console.log("Listening on port", PORT); +});