From 9f978f7df43960af3bf8daca911270ea938a6ba3 Mon Sep 17 00:00:00 2001 From: Scott Trinh Date: Thu, 1 Aug 2024 11:32:57 -0400 Subject: [PATCH 1/2] 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(); -} From 72db027b6e220e4e564cabc5ec77609b9a51e87f Mon Sep 17 00:00:00 2001 From: Scott Trinh Date: Thu, 1 Aug 2024 11:39:50 -0400 Subject: [PATCH 2/2] Ensure `Bot` is only imported as a type Just a quick safety check that we only actually use the `Bot` class through `getBot` instead of instantiating it. --- app/lib/discord/command.ts | 2 +- app/lib/discord/commands/helpChannels.ts | 23 ++++++----------------- app/lib/discord/commands/helpful.ts | 2 +- app/lib/discord/utils/downloadThread.ts | 2 +- app/lib/discord/utils/reviewCard.ts | 2 +- 5 files changed, 10 insertions(+), 21 deletions(-) diff --git a/app/lib/discord/command.ts b/app/lib/discord/command.ts index d463902..97a79d2 100644 --- a/app/lib/discord/command.ts +++ b/app/lib/discord/command.ts @@ -5,7 +5,7 @@ import { InteractionType, RESTPostAPIApplicationCommandsJSONBody, } from "discord-api-types/v10"; -import {Bot} from "./bot"; +import type { Bot } from "./bot"; import commands from "./commands"; export interface Command { diff --git a/app/lib/discord/commands/helpChannels.ts b/app/lib/discord/commands/helpChannels.ts index 9684058..9dba916 100644 --- a/app/lib/discord/commands/helpChannels.ts +++ b/app/lib/discord/commands/helpChannels.ts @@ -1,8 +1,6 @@ import { - APIApplicationCommand, APIBaseInteraction, InteractionType, - APIApplicationCommandInteractionData, RESTPostAPIApplicationCommandsJSONBody, PermissionFlagsBits, ApplicationCommandType, @@ -16,11 +14,10 @@ import { MessageFlags, InteractionResponseType, } from "discord-api-types/v10"; -import {Bot} from "../bot"; -import {Command} from "../command"; -import {getHelpChannels} from "../queries/getHelpChannels.query"; -import {addHelpChannel} from "../queries/addHelpChannel.query"; -import {removeHelpChannel} from "../queries/removeHelpChannel.query"; +import type { Bot } from "../bot"; +import { Command } from "../command"; +import { addHelpChannel } from "../queries/addHelpChannel.query"; +import { removeHelpChannel } from "../queries/removeHelpChannel.query"; export default class HelpChannelCommands implements Command @@ -140,11 +137,7 @@ export default class HelpChannelCommands }); break; case "add": { - const channel = await this.resolveChannel( - bot, - interaction, - subCommand - ); + const channel = await this.resolveChannel(bot, interaction, subCommand); if (bot["help-channels"].has(channel.id)) { respond({ @@ -172,11 +165,7 @@ export default class HelpChannelCommands break; } case "remove": { - const channel = await this.resolveChannel( - bot, - interaction, - subCommand - ); + const channel = await this.resolveChannel(bot, interaction, subCommand); if (!bot["help-channels"].has(channel.id)) { respond({ diff --git a/app/lib/discord/commands/helpful.ts b/app/lib/discord/commands/helpful.ts index a7c31a8..105672b 100644 --- a/app/lib/discord/commands/helpful.ts +++ b/app/lib/discord/commands/helpful.ts @@ -11,7 +11,7 @@ import { RESTPostAPIApplicationCommandsJSONBody, } from "discord-api-types/v10"; import { Command } from "../command"; -import { Bot } from "../bot"; +import type { Bot } from "../bot"; import { isHelpfulThread } from "../queries/isHelpfulThread.query"; import downloadThreadMessages from "../utils/downloadThread"; import { suggestThread } from "../queries/suggestThread.query"; diff --git a/app/lib/discord/utils/downloadThread.ts b/app/lib/discord/utils/downloadThread.ts index ef8d718..7e09d6a 100644 --- a/app/lib/discord/utils/downloadThread.ts +++ b/app/lib/discord/utils/downloadThread.ts @@ -1,5 +1,5 @@ import { APIMessage, APIThreadChannel, Routes } from "discord-api-types/v10"; -import { Bot } from "../bot"; +import type { Bot } from "../bot"; const downloadingInProgress = new Set(); diff --git a/app/lib/discord/utils/reviewCard.ts b/app/lib/discord/utils/reviewCard.ts index 1ae5288..434ed43 100644 --- a/app/lib/discord/utils/reviewCard.ts +++ b/app/lib/discord/utils/reviewCard.ts @@ -4,7 +4,7 @@ import { APIThreadChannel, Routes, } from "discord-api-types/v10"; -import { Bot } from "../bot"; +import type { Bot } from "../bot"; import { SuggestThreadReturns } from "../queries/suggestThread.query"; import { getEnvironment } from "../../../../envs";