From 9f978f7df43960af3bf8daca911270ea938a6ba3 Mon Sep 17 00:00:00 2001 From: Scott Trinh Date: Thu, 1 Aug 2024 11:32:57 -0400 Subject: [PATCH] Refactor `getBot` - No need for a separate module - Return the newly created `bot` if it's not set --- app/api/interactions/route.ts | 4 ++-- app/lib/discord/bot.ts | 16 ++++++++++++++-- app/lib/discord/getBot.ts | 15 --------------- 3 files changed, 16 insertions(+), 19 deletions(-) delete mode 100644 app/lib/discord/getBot.ts diff --git a/app/api/interactions/route.ts b/app/api/interactions/route.ts index 1c98784..cfc9d2a 100644 --- a/app/api/interactions/route.ts +++ b/app/api/interactions/route.ts @@ -2,7 +2,7 @@ import { InteractionResponseType, InteractionType, } from "discord-api-types/v10"; -import { getBot } from "@/app/lib/discord/getBot"; +import { getBot } from "@/app/lib/discord/bot"; import { getEnvironment } from "../../../envs"; import { verifyInteractionRequest } from "@/app/lib/discord/verify-interaction-request"; @@ -26,7 +26,7 @@ export async function POST(req: Request) { try { const bot = await getBot(); - const result = await bot?.processInteraction(interaction); + const result = await bot.processInteraction(interaction); return Response.json(result); } catch (err: any) { diff --git a/app/lib/discord/bot.ts b/app/lib/discord/bot.ts index ce189a3..9e35c39 100644 --- a/app/lib/discord/bot.ts +++ b/app/lib/discord/bot.ts @@ -11,11 +11,23 @@ import { } from "discord-api-types/v10"; import { Command, loadCommands } from "./command"; import { REST, RESTOptions } from "@discordjs/rest"; -import { Client } from "edgedb"; +import { Client, createClient } from "edgedb"; import { InteractionPromise } from "./interactionPromise"; import { getHelpChannels } from "./queries/getHelpChannels.query"; import { getEnvironment } from "../../../envs"; +let bot: Bot | null = null; + +export async function getBot(): Promise { + if (bot) { + return bot; + } + + bot = new Bot(createClient(), getEnvironment().discordToken); + await bot.initialize(); + return bot; +} + export class Bot extends REST { public readonly edgedb: Client; public ["help-channels"]: Set = new Set(); @@ -136,4 +148,4 @@ export class Bot extends REST { } ); } -} +} \ No newline at end of file diff --git a/app/lib/discord/getBot.ts b/app/lib/discord/getBot.ts deleted file mode 100644 index da8573a..0000000 --- a/app/lib/discord/getBot.ts +++ /dev/null @@ -1,15 +0,0 @@ -import createClient from "edgedb"; - -import { Bot } from "./bot"; -import { getEnvironment } from "../../../envs"; - -let bot: Bot | null = null; - -export async function getBot() { - if (bot) { - return bot; - } - - bot = new Bot(createClient(), getEnvironment().discordToken); - await bot.initialize(); -}