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/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/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(); -} 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";