From 8dd58b81c5dfbab5db955a044211e08ea15914eb Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Fri, 23 Jul 2021 12:12:19 -0600 Subject: [PATCH] Get guild ID from the callback URL instead, allow me to not have to create the webhook in order to get the ID and then have to delete it if it isn't allowed to be created. --- src/endpoints/discord/auth/callback.ts | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/endpoints/discord/auth/callback.ts b/src/endpoints/discord/auth/callback.ts index 87ac0bf..eea3ab5 100644 --- a/src/endpoints/discord/auth/callback.ts +++ b/src/endpoints/discord/auth/callback.ts @@ -1,5 +1,5 @@ -import { Request, ResponseToolkit } from "@hapi/hapi"; import { CHANNEL_DATA, DISCORD_API_URI } from "@/constants"; +import { Request, ResponseToolkit } from "@hapi/hapi"; import { config, db } from "@/main"; import boom from "@hapi/boom"; import axios from "axios"; @@ -7,7 +7,8 @@ import axios from "axios"; export default { method: `GET`, path: `/discord/auth/callback`, async handler(request: Request, h: ResponseToolkit) { - let code = request.query.code; + let { code, guild_id: gID } = request.query; + let data = new URLSearchParams(); data.set(`client_id`, config.discord.client_id); @@ -22,20 +23,11 @@ export default { } }); - let { guild_id, id, token } = r.data.webhook; + let { 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; + db[gID] = JSON.parse(JSON.stringify(CHANNEL_DATA)) + db[gID].webhook.token = token; + db[gID].webhook.id = id; return r.data; },