From 4eec9f4088bd4c2e5efa72deccd963a71164e7c8 Mon Sep 17 00:00:00 2001 From: TomatoCake <60300461+DEVTomatoCake@users.noreply.github.com> Date: Tue, 31 Oct 2023 21:20:38 +0100 Subject: [PATCH] Add config example & DB schema --- .eslintrc.json | 3 +-- config.example.json | 16 ++++++++++++++++ index.js | 15 ++++++++------- util/oauth.js | 2 +- util/setupDB.js | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 config.example.json create mode 100644 util/setupDB.js diff --git a/.eslintrc.json b/.eslintrc.json index 689aeae..aca01be 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,7 +4,6 @@ "es2022": true }, "plugins": ["unicorn", "sonarjs"], - "ignorePatterns": ["util/temp.js", "public/toasts/toastNotification.js"], "rules": { "array-bracket-spacing": 2, "array-callback-return": 2, @@ -61,7 +60,7 @@ "no-unreachable": 1, "no-unreachable-loop": 2, "no-unsafe-finally": 2, - "no-unused-vars": [2, { "argsIgnorePattern": "args|next", "varsIgnorePattern": "_|updateSlashcommands|updateLinkedroles" }], + "no-unused-vars": [2, { "argsIgnorePattern": "next", "varsIgnorePattern": "_" }], "no-useless-computed-key": 2, "no-useless-rename": 2, "no-useless-escape": 2, diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..bf395ad --- /dev/null +++ b/config.example.json @@ -0,0 +1,16 @@ +{ + "botId": "1168822115810672711", + "botSecret": "", + "botToken": "", + + "port": 25527, + + "userAgent": "DiscordBot (https://disgithook.tomatenkuchen.com, v1.0.0)", + + "db": { + "host": "", + "user": "", + "password": "", + "database": "" + } +} diff --git a/index.js b/index.js index 7b1ba89..5746bd6 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,9 @@ -const { db } = require("../config.json") - -const mysql = require("mysql2") -const mainDB = mysql.createPool(db) -const pool = mainDB.promise() +const { port } = require("./config.json") +const pool = require("./util/setupDB.js") const express = require("express") const app = express() +app.listen(port) // - Dashboard - @@ -23,9 +21,12 @@ app.get("/auth/logout", (req, res) => { // - Hooks - const hookFunc = async (req, res) => { + const [rows] = await pool.query("SELECT * FROM `hook` WHERE `id` = ?", [req.params.id]) + console.log(rows) + + if (rows.length == 0) return res.sendStatus(404) + res.sendStatus(204) } app.get("/hook/:id/:secret", hookFunc) app.post("/hook/:id/:secret", hookFunc) - -app.listen(25527) diff --git a/util/oauth.js b/util/oauth.js index c23a3d9..a137986 100644 --- a/util/oauth.js +++ b/util/oauth.js @@ -37,7 +37,7 @@ module.exports.getUserGuilds = async token => { return json } -module.exports.getAccessToken = async (userId, tokens, bot) => { +module.exports.getAccessToken = async (userId, tokens) => { if (Date.now() > tokens.expires_at) { const body = new URLSearchParams({ client_id: botId, diff --git a/util/setupDB.js b/util/setupDB.js new file mode 100644 index 0000000..e24d38f --- /dev/null +++ b/util/setupDB.js @@ -0,0 +1,32 @@ +const { db } = require("../config.json") + +const mysql = require("mysql2") +const mainDB = mysql.createPool(db) + +const pool = mainDB.promise() +module.exports = pool + +// hook schema: id secret guild webhook? channel? name? avatar? message +pool.query( + "CREATE TABLE IF NOT EXISTS `hook` (" + + "`id` VARCHAR(8) NOT NULL PRIMARY KEY," + + "`secret` VARCHAR(64) NOT NULL," + + "`guild` VARCHAR(21) NOT NULL," + + "`webhook` VARCHAR(128) NOT NULL," + + "`channel` VARCHAR(21) NOT NULL," + + "`name` VARCHAR(32) NOT NULL," + + "`avatar` VARCHAR(512) NOT NULL," + + "`message` VARCHAR(10240) NOT NULL" + + ")" +) + +// user schema: user token access refresh expires +pool.query( + "CREATE TABLE IF NOT EXISTS `user` (" + + "`id` VARCHAR(21) NOT NULL PRIMARY KEY," + + "`token` VARCHAR(64) NOT NULL," + + "`access` VARCHAR(64) NOT NULL," + + "`refresh` VARCHAR(64) NOT NULL," + + "`expires` BIGINT NOT NULL" + + ")" +)