From 50ff8711fa0cecb38a76ef638b9f1450ca1a0091 Mon Sep 17 00:00:00 2001 From: JJ-8 <34778827+JJ-8@users.noreply.github.com> Date: Sun, 25 Aug 2024 09:21:34 +0200 Subject: [PATCH] Move database and Discord init to different files --- api/src/discord/database/ctfs.ts | 2 +- api/src/discord/database/tasks.ts | 2 +- api/src/discord/database/users.ts | 2 +- api/src/discord/index.ts | 21 +++++++++++++++++++ api/src/index.ts | 21 ++----------------- .../{discord/database => utils}/database.ts | 2 +- 6 files changed, 27 insertions(+), 23 deletions(-) rename api/src/{discord/database => utils}/database.ts (89%) diff --git a/api/src/discord/database/ctfs.ts b/api/src/discord/database/ctfs.ts index b740d8d43..e74bcb36c 100644 --- a/api/src/discord/database/ctfs.ts +++ b/api/src/discord/database/ctfs.ts @@ -1,4 +1,4 @@ -import { connectToDatabase } from "./database"; +import { connectToDatabase } from "../../utils/database"; import { PoolClient } from "pg"; export interface CTF { diff --git a/api/src/discord/database/tasks.ts b/api/src/discord/database/tasks.ts index fb03ee168..f1a5238dc 100644 --- a/api/src/discord/database/tasks.ts +++ b/api/src/discord/database/tasks.ts @@ -1,4 +1,4 @@ -import { connectToDatabase } from "./database"; +import { connectToDatabase } from "../../utils/database"; import { PoolClient } from "pg"; export interface Task { diff --git a/api/src/discord/database/users.ts b/api/src/discord/database/users.ts index 6a34997ac..86005a4a6 100644 --- a/api/src/discord/database/users.ts +++ b/api/src/discord/database/users.ts @@ -1,4 +1,4 @@ -import { connectToDatabase } from "./database"; +import { connectToDatabase } from "../../utils/database"; import { PoolClient } from "pg"; /* diff --git a/api/src/discord/index.ts b/api/src/discord/index.ts index da4d4f3e0..0c92f750d 100644 --- a/api/src/discord/index.ts +++ b/api/src/discord/index.ts @@ -1,10 +1,31 @@ import { Client, GatewayIntentBits } from "discord.js"; import config from "../config"; import ready from "./listeners/ready"; +import { connectToDatabase } from "../utils/database"; let client: Client | null = null; export let usingDiscordBot = true; +export async function initDiscordBot() { + getDiscordClient(); + + const pgClient = await connectToDatabase(); + + try { + const query = + "UPDATE ctfnote.settings SET discord_integration_enabled = $1"; + const values = [config.discord.use.toLowerCase() !== "false"]; + await pgClient.query(query, values); + } catch (error) { + console.error( + "Failed to set discord_integration_enabled flag in the database:", + error + ); + } finally { + pgClient.release(); + } +} + export function getDiscordClient(): Client | null { if (!usingDiscordBot) return null; if (config.discord.use.toLowerCase() === "false") { diff --git a/api/src/index.ts b/api/src/index.ts index b28461238..8a6389d9f 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -19,10 +19,9 @@ import { icalRoute } from "./routes/ical"; import ConnectionFilterPlugin from "postgraphile-plugin-connection-filter"; import OperationHook from "@graphile/operation-hooks"; import discordHooks from "./plugins/discordHooks"; -import { getDiscordClient } from "./discord"; +import { initDiscordBot } from "./discord"; import PgManyToManyPlugin from "@graphile-contrib/pg-many-to-many"; import ProfileSubscriptionPlugin from "./plugins/ProfileSubscriptionPlugin"; -import { connectToDatabase } from "./discord/database/database"; function getDbUrl(role: "user" | "admin") { const login = config.db[role].login; @@ -153,23 +152,7 @@ async function main() { const postgraphileOptions = createOptions(); const app = createApp(postgraphileOptions); - getDiscordClient(); - - const pgClient = await connectToDatabase(); //? maybe we should not keep this dependency in the discord folder? - - try { - const query = - "UPDATE ctfnote.settings SET discord_integration_enabled = $1"; - const values = [config.discord.use.toLowerCase() !== "false"]; - await pgClient.query(query, values); - } catch (error) { - console.error( - "Failed to set discord_integration_enabled flag in the database:", - error - ); - } finally { - pgClient.release(); - } + await initDiscordBot(); app.listen(config.web.port, () => { //sendMessageToDiscord("CTFNote API started"); diff --git a/api/src/discord/database/database.ts b/api/src/utils/database.ts similarity index 89% rename from api/src/discord/database/database.ts rename to api/src/utils/database.ts index 6d3a23559..8881444d0 100644 --- a/api/src/discord/database/database.ts +++ b/api/src/utils/database.ts @@ -1,5 +1,5 @@ import { Pool } from "pg"; -import config from "../../config"; +import config from "../config"; const pgPool = new Pool({ user: config.db.admin.login,