Skip to content

Commit

Permalink
Move database and Discord init to different files
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ-8 committed Aug 25, 2024
1 parent a41f44b commit 50ff871
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion api/src/discord/database/ctfs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connectToDatabase } from "./database";
import { connectToDatabase } from "../../utils/database";
import { PoolClient } from "pg";

export interface CTF {
Expand Down
2 changes: 1 addition & 1 deletion api/src/discord/database/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connectToDatabase } from "./database";
import { connectToDatabase } from "../../utils/database";
import { PoolClient } from "pg";

export interface Task {
Expand Down
2 changes: 1 addition & 1 deletion api/src/discord/database/users.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connectToDatabase } from "./database";
import { connectToDatabase } from "../../utils/database";
import { PoolClient } from "pg";

/*
Expand Down
21 changes: 21 additions & 0 deletions api/src/discord/index.ts
Original file line number Diff line number Diff line change
@@ -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") {
Expand Down
21 changes: 2 additions & 19 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 50ff871

Please sign in to comment.