Skip to content

Commit

Permalink
Refactor getBot
Browse files Browse the repository at this point in the history
- No need for a separate module
- Return the newly created `bot` if it's not set
  • Loading branch information
scotttrinh committed Aug 1, 2024
1 parent 236548f commit 9f978f7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/api/interactions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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) {
Expand Down
16 changes: 14 additions & 2 deletions app/lib/discord/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bot> {
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<string> = new Set();
Expand Down Expand Up @@ -136,4 +148,4 @@ export class Bot extends REST {
}
);
}
}
}
15 changes: 0 additions & 15 deletions app/lib/discord/getBot.ts

This file was deleted.

0 comments on commit 9f978f7

Please sign in to comment.