Pretty discord joiner

This commit is contained in:
Tarrgon
2025-05-28 11:52:40 -04:00
parent 4a455da42e
commit 93bfbdfa98
2 changed files with 59 additions and 11 deletions
@@ -1,9 +1,10 @@
import express, { Request, Response } from 'express'; import express, { Request, Response } from 'express';
import DiscordOAuth2 from 'discord-oauth2'; import DiscordOAuth2 from 'discord-oauth2';
import { config } from './config'; import { config } from '../config';
import { Database } from './shared/Database'; import { Database } from '../shared/Database';
import crypto from 'crypto'; import crypto from 'crypto';
import session from 'express-session'; import session from 'express-session';
import fs from 'fs';
declare module 'express-session' { declare module 'express-session' {
interface SessionData { interface SessionData {
@@ -16,6 +17,8 @@ declare module 'express-session' {
const DEV_BASE_URL = `http://localhost:${config.PORT}`; const DEV_BASE_URL = `http://localhost:${config.PORT}`;
const PROD_BASE_URL = 'https://discord.e621.net'; const PROD_BASE_URL = 'https://discord.e621.net';
const PAGE_TEMPLATE = fs.readFileSync('./templates/page.html', { encoding: 'utf-8' });
const oauth = new DiscordOAuth2({ const oauth = new DiscordOAuth2({
clientId: config.DISCORD_CLIENT_ID!, clientId: config.DISCORD_CLIENT_ID!,
clientSecret: config.DISCORD_CLIENT_SECRET!, clientSecret: config.DISCORD_CLIENT_SECRET!,
@@ -56,11 +59,11 @@ async function handleInitial(req: Request, res: Response): Promise<any> {
const { username, user_id, time, hash } = req.query; const { username, user_id, time, hash } = req.query;
if (!username || !user_id || !time || !hash) { if (!username || !user_id || !time || !hash) {
return res.sendStatus(400); return sendBadRequest(res);
} }
if (Date.now() / 1000 > Number(time)) { if (Date.now() / 1000 > Number(time)) {
return res.status(403).send('You took too long to authorize the request. Please try again.'); return render(res, 403, 'You took too long to authorize the request. Please try again.');
} }
const authString = `${username} ${user_id} ${time} ${config.LINK_SECRET}`; const authString = `${username} ${user_id} ${time} ${config.LINK_SECRET}`;
@@ -69,7 +72,7 @@ async function handleInitial(req: Request, res: Response): Promise<any> {
if (hash != digest) { if (hash != digest) {
console.error(`Bad auth: ${hash} ${digest}`); console.error(`Bad auth: ${hash} ${digest}`);
return res.sendStatus(403); return sendForbidden(res);
} }
const oauthState = crypto.randomBytes(16).toString('hex'); const oauthState = crypto.randomBytes(16).toString('hex');
@@ -82,7 +85,7 @@ async function handleInitial(req: Request, res: Response): Promise<any> {
if (e) { if (e) {
console.error('Error saving session:'); console.error('Error saving session:');
console.error(e); console.error(e);
return res.sendStatus(500); return sendInteralServerError(res);
} }
res.redirect(oauth.generateAuthUrl({ res.redirect(oauth.generateAuthUrl({
@@ -94,13 +97,13 @@ async function handleInitial(req: Request, res: Response): Promise<any> {
async function handleCallback(req: Request, res: Response): Promise<any> { async function handleCallback(req: Request, res: Response): Promise<any> {
if (!req.session.userId || !req.session.username || !req.session.oauthState) { if (!req.session.userId || !req.session.username || !req.session.oauthState) {
return res.sendStatus(403); return sendForbidden(res);
} }
const state = req.query.state as string; const state = req.query.state as string;
if (state != req.session.oauthState) { if (state != req.session.oauthState) {
return res.sendStatus(403); return sendForbidden(res);
} }
const code = req.query.code as string; const code = req.query.code as string;
@@ -115,14 +118,30 @@ async function handleCallback(req: Request, res: Response): Promise<any> {
try { try {
if (!await joinGuild(code, userId, username)) { if (!await joinGuild(code, userId, username)) {
console.error(`Error joining user: ${username} (${userId})`); console.error(`Error joining user: ${username} (${userId})`);
return res.sendStatus(500); return sendInteralServerError(res);
} }
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return res.sendStatus(500); return sendInteralServerError(res);
} }
res.sendStatus(200); render(res, 200, 'Success', `You have been added to the server. <a href="https://discord.com/channels/${config.DISCORD_GUILD_ID}">See you there.</a>`);
}
function sendInteralServerError(res: Response) {
render(res, 500, 'Internal Server Error');
}
function sendForbidden(res: Response) {
render(res, 403, 'Forbidden');
}
function sendBadRequest(res: Response) {
render(res, 400, 'Bad Request');
}
function render(res: Response, code: number, title: string = '', message: string = '') {
res.status(code).setHeader('Content-Type', 'text/html').send(PAGE_TEMPLATE.replaceAll('{{ title }}', title).replaceAll('{{ message }}', message));
} }
export function initializeDiscordJoiner() { export function initializeDiscordJoiner() {
+29
View File
@@ -0,0 +1,29 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>{{ title }}</title>
<style>
body {
background-color: #012e56;
color: #fff;
font-family: Verdana, sans-serif;
}
a {
color: #b4c7d9;
text-decoration: none;
}
a:hover {
color: #e9f2fa;
}
</style>
</head>
<body>
<h1>{{ title }}</h1>
<p>{{ message }}</p>
</body>
</html>