0
0
Fork 0

Add support for per-guild database setup

This commit is contained in:
Oliver-Akins 2021-07-22 12:54:54 -06:00
parent 3b7a44e133
commit cc68fd3d72

View file

@ -1,11 +1,12 @@
import { Request, ResponseToolkit } from "@hapi/hapi";
import { CHANNEL_DATA, DISCORD_API_URI } from "@/constants";
import { config, db } from "@/main";
import boom from "@hapi/boom";
import axios from "axios";
export default {
method: `GET`, path: `/discord/auth/callback`,
async handler(request: Request, h: ResponseToolkit) {
console.log(`Authentication finishing!`)
let code = request.query.code;
let data = new URLSearchParams();
@ -15,14 +16,26 @@ export default {
data.set(`code`, code);
data.set(`redirect_uri`, config.discord.auth_redirect);
let r = await axios.post(`https://discord.com/api/v8/oauth2/token`, data, {
let r = await axios.post(`${DISCORD_API_URI}/oauth2/token`, data, {
headers: {
'Content-Type': `application/x-www-form-urlencoded`
}
});
db.webhook.token = r.data.webhook.token;
db.webhook.id = r.data.webhook.id;
let { guild_id, id, token } = r.data.webhook;
// Assert the guild is allowed to be setup.
if (!config.guilds[guild_id]) {
// Delete the webhook so that it doesn't remain in the server
await axios.delete(r.data.webhook.url);
throw boom.notFound(`Cannot save a webhook for a guild that doesn't have a config set up.`);
};
db[guild_id] = JSON.parse(JSON.stringify(CHANNEL_DATA))
db[guild_id].webhook.token = token;
db[guild_id].webhook.id = id;
return r.data;
},