Skip to content

Commit 3610d8e

Browse files
authored
Merge pull request #2 from edgedb/lazy-bot-refactor
Refactor getBot
2 parents 652fd88 + 72db027 commit 3610d8e

File tree

8 files changed

+26
-40
lines changed

8 files changed

+26
-40
lines changed

app/api/interactions/route.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
InteractionResponseType,
33
InteractionType,
44
} from "discord-api-types/v10";
5-
import { getBot } from "@/app/lib/discord/getBot";
5+
import { getBot } from "@/app/lib/discord/bot";
66
import { getEnvironment } from "../../../envs";
77
import { verifyInteractionRequest } from "@/app/lib/discord/verify-interaction-request";
88

@@ -26,7 +26,7 @@ export async function POST(req: Request) {
2626

2727
try {
2828
const bot = await getBot();
29-
const result = await bot?.processInteraction(interaction);
29+
const result = await bot.processInteraction(interaction);
3030

3131
return Response.json(result);
3232
} catch (err: any) {

app/lib/discord/bot.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,23 @@ import {
1111
} from "discord-api-types/v10";
1212
import { Command, loadCommands } from "./command";
1313
import { REST, RESTOptions } from "@discordjs/rest";
14-
import { Client } from "edgedb";
14+
import { Client, createClient } from "edgedb";
1515
import { InteractionPromise } from "./interactionPromise";
1616
import { getHelpChannels } from "./queries/getHelpChannels.query";
1717
import { getEnvironment } from "../../../envs";
1818

19+
let bot: Bot | null = null;
20+
21+
export async function getBot(): Promise<Bot> {
22+
if (bot) {
23+
return bot;
24+
}
25+
26+
bot = new Bot(createClient(), getEnvironment().discordToken);
27+
await bot.initialize();
28+
return bot;
29+
}
30+
1931
export class Bot extends REST {
2032
public readonly edgedb: Client;
2133
public ["help-channels"]: Set<string> = new Set();
@@ -136,4 +148,4 @@ export class Bot extends REST {
136148
}
137149
);
138150
}
139-
}
151+
}

app/lib/discord/command.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
InteractionType,
66
RESTPostAPIApplicationCommandsJSONBody,
77
} from "discord-api-types/v10";
8-
import {Bot} from "./bot";
8+
import type { Bot } from "./bot";
99
import commands from "./commands";
1010

1111
export interface Command<Interaction = APIApplicationCommandInteractionData> {

app/lib/discord/commands/helpChannels.ts

+6-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import {
2-
APIApplicationCommand,
32
APIBaseInteraction,
43
InteractionType,
5-
APIApplicationCommandInteractionData,
64
RESTPostAPIApplicationCommandsJSONBody,
75
PermissionFlagsBits,
86
ApplicationCommandType,
@@ -16,11 +14,10 @@ import {
1614
MessageFlags,
1715
InteractionResponseType,
1816
} from "discord-api-types/v10";
19-
import {Bot} from "../bot";
20-
import {Command} from "../command";
21-
import {getHelpChannels} from "../queries/getHelpChannels.query";
22-
import {addHelpChannel} from "../queries/addHelpChannel.query";
23-
import {removeHelpChannel} from "../queries/removeHelpChannel.query";
17+
import type { Bot } from "../bot";
18+
import { Command } from "../command";
19+
import { addHelpChannel } from "../queries/addHelpChannel.query";
20+
import { removeHelpChannel } from "../queries/removeHelpChannel.query";
2421

2522
export default class HelpChannelCommands
2623
implements Command<APIChatInputApplicationCommandInteractionData>
@@ -140,11 +137,7 @@ export default class HelpChannelCommands
140137
});
141138
break;
142139
case "add": {
143-
const channel = await this.resolveChannel(
144-
bot,
145-
interaction,
146-
subCommand
147-
);
140+
const channel = await this.resolveChannel(bot, interaction, subCommand);
148141

149142
if (bot["help-channels"].has(channel.id)) {
150143
respond({
@@ -172,11 +165,7 @@ export default class HelpChannelCommands
172165
break;
173166
}
174167
case "remove": {
175-
const channel = await this.resolveChannel(
176-
bot,
177-
interaction,
178-
subCommand
179-
);
168+
const channel = await this.resolveChannel(bot, interaction, subCommand);
180169

181170
if (!bot["help-channels"].has(channel.id)) {
182171
respond({

app/lib/discord/commands/helpful.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
RESTPostAPIApplicationCommandsJSONBody,
1212
} from "discord-api-types/v10";
1313
import { Command } from "../command";
14-
import { Bot } from "../bot";
14+
import type { Bot } from "../bot";
1515
import { isHelpfulThread } from "../queries/isHelpfulThread.query";
1616
import downloadThreadMessages from "../utils/downloadThread";
1717
import { suggestThread } from "../queries/suggestThread.query";

app/lib/discord/getBot.ts

-15
This file was deleted.

app/lib/discord/utils/downloadThread.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { APIMessage, APIThreadChannel, Routes } from "discord-api-types/v10";
2-
import { Bot } from "../bot";
2+
import type { Bot } from "../bot";
33

44
const downloadingInProgress = new Set<string>();
55

app/lib/discord/utils/reviewCard.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
APIThreadChannel,
55
Routes,
66
} from "discord-api-types/v10";
7-
import { Bot } from "../bot";
7+
import type { Bot } from "../bot";
88
import { SuggestThreadReturns } from "../queries/suggestThread.query";
99
import { getEnvironment } from "../../../../envs";
1010

0 commit comments

Comments
 (0)