Skip to content

Commit

Permalink
Merge pull request #2 from edgedb/lazy-bot-refactor
Browse files Browse the repository at this point in the history
Refactor getBot
  • Loading branch information
diksipav authored Aug 1, 2024
2 parents 652fd88 + 72db027 commit 3610d8e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 40 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 {
}
);
}
}
}
2 changes: 1 addition & 1 deletion app/lib/discord/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Interaction = APIApplicationCommandInteractionData> {
Expand Down
23 changes: 6 additions & 17 deletions app/lib/discord/commands/helpChannels.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
APIApplicationCommand,
APIBaseInteraction,
InteractionType,
APIApplicationCommandInteractionData,
RESTPostAPIApplicationCommandsJSONBody,
PermissionFlagsBits,
ApplicationCommandType,
Expand All @@ -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<APIChatInputApplicationCommandInteractionData>
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion app/lib/discord/commands/helpful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
15 changes: 0 additions & 15 deletions app/lib/discord/getBot.ts

This file was deleted.

2 changes: 1 addition & 1 deletion app/lib/discord/utils/downloadThread.ts
Original file line number Diff line number Diff line change
@@ -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<string>();

Expand Down
2 changes: 1 addition & 1 deletion app/lib/discord/utils/reviewCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down

0 comments on commit 3610d8e

Please sign in to comment.