Add support for per-guild database setup
This commit is contained in:
parent
3b7a44e133
commit
cc68fd3d72
1 changed files with 17 additions and 4 deletions
|
|
@ -1,11 +1,12 @@
|
||||||
import { Request, ResponseToolkit } from "@hapi/hapi";
|
import { Request, ResponseToolkit } from "@hapi/hapi";
|
||||||
|
import { CHANNEL_DATA, DISCORD_API_URI } from "@/constants";
|
||||||
import { config, db } from "@/main";
|
import { config, db } from "@/main";
|
||||||
|
import boom from "@hapi/boom";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
method: `GET`, path: `/discord/auth/callback`,
|
method: `GET`, path: `/discord/auth/callback`,
|
||||||
async handler(request: Request, h: ResponseToolkit) {
|
async handler(request: Request, h: ResponseToolkit) {
|
||||||
console.log(`Authentication finishing!`)
|
|
||||||
let code = request.query.code;
|
let code = request.query.code;
|
||||||
|
|
||||||
let data = new URLSearchParams();
|
let data = new URLSearchParams();
|
||||||
|
|
@ -15,14 +16,26 @@ export default {
|
||||||
data.set(`code`, code);
|
data.set(`code`, code);
|
||||||
data.set(`redirect_uri`, config.discord.auth_redirect);
|
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: {
|
headers: {
|
||||||
'Content-Type': `application/x-www-form-urlencoded`
|
'Content-Type': `application/x-www-form-urlencoded`
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
db.webhook.token = r.data.webhook.token;
|
let { guild_id, id, token } = r.data.webhook;
|
||||||
db.webhook.id = r.data.webhook.id;
|
|
||||||
|
// 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;
|
return r.data;
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue