feat: settings v2

This commit is contained in:
Isaac 2023-03-10 23:47:43 +00:00
parent cd71843bb0
commit 96b5c92743
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
7 changed files with 106 additions and 505 deletions

View File

@ -1,6 +1,6 @@
{
"name": "discord-tickets",
"version": "4.0.0-beta.6",
"version": "4.0.0-beta.7",
"private": "true",
"description": "An open-source Discord bot for ticket management",
"main": "src/",
@ -40,13 +40,11 @@
"node": ">=18"
},
"dependencies": {
"@discord-tickets/settings": "^1.5.0",
"@discord-tickets/settings": "^2.0.0",
"@eartharoid/dbf": "^0.3.3",
"@eartharoid/dtf": "^2.0.1",
"@eartharoid/i18n": "^1.2.1",
"@fastify/cookie": "^6.0.0",
"@fastify/cors": "^8.2.0",
"@fastify/http-proxy": "^8.4.0",
"@fastify/jwt": "^5.0.1",
"@fastify/oauth2": "^5.1.0",
"@prisma/client": "^4.11.0",
@ -54,7 +52,6 @@
"cryptr": "^6.2.0",
"discord.js": "^14.7.1",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"fastify": "^4.14.1",
"figlet": "^1.5.2",
"fs-extra": "^10.1.0",
@ -76,10 +73,10 @@
"@commitlint/config-conventional": "^17.4.4",
"all-contributors-cli": "^6.24.0",
"conventional-changelog-cli": "^2.2.2",
"eslint": "^8.35.0",
"eslint": "^8.36.0",
"eslint-plugin-unused-imports": "^2.0.0",
"husky": "^8.0.3",
"lint-staged": "^13.1.2",
"lint-staged": "^13.2.0",
"nodemon": "^2.0.21"
},
"optionalDependencies": {

File diff suppressed because it is too large Load Diff

View File

@ -18,16 +18,14 @@ const env = {
DISCORD_SECRET: '',
DISCORD_TOKEN: '',
ENCRYPTION_KEY: randomBytes(24).toString('hex'),
HTTP_EXTERNAL: 'http://127.0.0.1:8080',
HTTP_EXTERNAL: 'http://127.0.0.1:8169',
HTTP_HOST: '0.0.0.0',
HTTP_PORT: 8080,
HTTP_PORT: 8169,
HTTP_TRUST_PROXY: false,
NODE_ENV: 'production', // not bot-specific
OVERRIDE_ARCHIVE: '',
PUBLIC_BOT: false,
PUBLISH_COMMANDS: false,
SETTINGS_HOST: '127.0.0.1',
SETTINGS_PORT: 8169,
SUPER: '319467558166069248',
};

View File

@ -36,12 +36,6 @@ const env = {
OVERRIDE_ARCHIVE: () => true, // optional
PUBLIC_BOT: () => true, // optional
PUBLISH_COMMANDS: () => true, // optional
SETTINGS_HOST: v =>
(!!v && !v.startsWith('http')) ||
new Error('is required and must be an address, not a URL'),
SETTINGS_PORT: v =>
!!v ||
new Error('is required'),
SUPER: () => true, // optional
};

View File

@ -6,16 +6,8 @@ const { join } = require('path');
const { files } = require('node-dir');
const { PermissionsBitField } = require('discord.js');
process.env.PUBLIC_HOST = process.env.HTTP_EXTERNAL; // the SvelteKit app expects `PUBLIC_HOST`
module.exports = async client => {
// cors plugin
fastify.register(require('@fastify/cors'), {
credentials: true,
methods: ['DELETE', 'GET', 'PATCH', 'PUT', 'POST'],
origin: true,
});
// oauth2 plugin
fastify.states = new Map();
fastify.register(oauth, {
@ -56,20 +48,6 @@ module.exports = async client => {
secret: process.env.ENCRYPTION_KEY,
});
// proxy `/settings` to express
fastify.register(require('@fastify/http-proxy'), {
http2: false,
prefix: '/settings',
replyOptions: {
rewriteRequestHeaders: (req, headers) => ({
...headers,
'host': domain,
}),
},
rewritePrefix: '/settings',
upstream: `http://${process.env.SETTINGS_HOST}:${process.env.SETTINGS_PORT}`,
});
// auth
fastify.decorate('authenticate', async (req, res) => {
try {
@ -176,24 +154,10 @@ module.exports = async client => {
})); // register route
});
// express server for settings
const express = require('express')();
const { handler } = await import('@discord-tickets/settings/build/handler.js');
process.on('sveltekit:error', ({
error,
errorId,
}) => {
client.log.error.http(`Express ${errorId} ${error}`);
});
express.set('trust proxy', true);
express.use((req, res, next) => {
next();
client.log.verbose.http(short(`Express ${req.ip} ${req.method} ${req.route?.path ?? req.path}`));
});
express.use(handler); // let SvelteKit handle everything
express.listen(process.env.SETTINGS_PORT, process.env.SETTINGS_HOST, () => { // start the express server
client.log.verbose.http(`Express listening on port ${process.env.SETTINGS_PORT}`);
});
// https://stackoverflow.com/questions/72317071/how-to-set-up-fastify-correctly-so-that-sveltekit-works-fine
fastify.all('/*', {}, (req, res) => handler(req.raw, res.raw, () => { }));
// start the fastify server
fastify.listen({
@ -203,4 +167,11 @@ module.exports = async client => {
if (err) client.log.error.http(err);
else client.log.success.http(`Listening at ${addr}`);
});
process.on('sveltekit:error', ({
error,
errorId,
}) => {
client.log.error.http(`SvelteKit ${errorId} ${error}`);
});
};

View File

@ -3,7 +3,7 @@ const { PermissionsBitField } = require('discord.js');
module.exports.get = fastify => ({
handler: async (req, res) => {
const { client } = res.context.config;
const guilds = await (await fetch('https://discordapp.com/api/users/@me/guilds', { headers: { 'Authorization': `Bearer ${req.user.payload.access_token}` } })).json();
const guilds = await (await fetch('https://discordapp.com/api/users/@me/guilds', { headers: { 'Authorization': `Bearer ${req.user.payload.accessToken}` } })).json();
res.send(
guilds
.filter(guild => guild.owner || new PermissionsBitField(guild.permissions.toString()).has(PermissionsBitField.Flags.ManageGuild))

View File

@ -1,7 +0,0 @@
module.exports.get = () => ({
handler: (req, res) => {
const { client } = res.context.config;
return `Hello, I am ${client.user.username}!`;
// res.redirect(process.env.SETTINGS_EXTERNAL);
},
});