Skip to content

Commit

Permalink
feat: decouple commands from commandResolver. Closes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
diomonogatari committed Oct 26, 2024
1 parent b78eac4 commit 1c584d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
51 changes: 28 additions & 23 deletions domain/service/commandUseCaseResolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { promises as fs } from "fs";
import path from "path";
import { Context } from "../../types";
import UseCaseNotFound from "../exception/useCaseNotFound";
import SendMessageToChannelUseCase from "../../application/usecases/sendMessageToChannel/sendMessageToChannelUseCase";
Expand All @@ -8,8 +10,6 @@ import ChannelResolver from "./channelResolver";
import KataService from "./kataService/kataService";
import SendCodewarsLeaderboardToChannelUseCase from "../../application/usecases/sendCodewarsLeaderboardToChannel/sendCodewarsLeaderboardToChannelUseCase";

type CallbackFunctionVariadic = (...args: unknown[]) => void;

export default class CommandUseCaseResolver {
private messageRepository: MessageRepository;

Expand All @@ -21,6 +21,8 @@ export default class CommandUseCaseResolver {

private kataService: KataService;

private commandMessages: Record<string, string> = {};

constructor({
messageRepository,
chatService,
Expand All @@ -41,7 +43,13 @@ export default class CommandUseCaseResolver {
this.kataService = kataService;
}

resolveByCommand(command: string, context: Context): void {
private async loadCommands(): Promise<void> {
const filePath = path.join(__dirname, "commands.json");
const data = await fs.readFile(filePath, "utf-8");
this.commandMessages = JSON.parse(data);
}

async resolveByCommand(command: string, context: Context): Promise<void> {
this.loggerService.log(`Command received: "${command}"`);

const deps = {
Expand All @@ -52,28 +60,25 @@ export default class CommandUseCaseResolver {
kataService: this.kataService,
};

const commandUseCases: Record<string, CallbackFunctionVariadic> = {
"!ja": async () =>
new SendMessageToChannelUseCase(deps).execute({
channelId: context.channelId,
message:
"Olá! Experimenta fazer a pergunta diretamente e contar o que já tentaste! Sabe mais aqui :point_right: https://dontasktoask.com/pt-pt/",
}),
"!oc": async () =>
new SendMessageToChannelUseCase(deps).execute({
channelId: context.channelId,
message: ":warning: Este servidor é APENAS para questões relacionadas com programação! :warning:",
}),
"!cwl": async () =>
new SendCodewarsLeaderboardToChannelUseCase(deps).execute({
channelId: context.channelId,
}),
};
if (Object.keys(this.commandMessages).length === 0) {
await this.loadCommands();
}

if (this.commandMessages[command]) {
new SendMessageToChannelUseCase(deps).execute({
channelId: context.channelId,
message: this.commandMessages[command],
});
return;
}

if (!commandUseCases[command]) {
throw new UseCaseNotFound().byCommand(command);
if (command === "!cwl") {
new SendCodewarsLeaderboardToChannelUseCase(deps).execute({
channelId: context.channelId,
});
return;
}

commandUseCases[command]();
throw new UseCaseNotFound().byCommand(command);
}
}
4 changes: 4 additions & 0 deletions domain/service/commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"!ja": "Olá! Experimenta fazer a pergunta diretamente e contar o que já tentaste! Sabe mais aqui :point_right: https://dontasktoask.com/pt-pt/",
"!oc": ":warning: Este servidor é APENAS para questões relacionadas com programação! :warning:"
}

0 comments on commit 1c584d3

Please sign in to comment.