Skip to content

Commit 1c584d3

Browse files
committed
feat: decouple commands from commandResolver. Closes #64
1 parent b78eac4 commit 1c584d3

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed
Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { promises as fs } from "fs";
2+
import path from "path";
13
import { Context } from "../../types";
24
import UseCaseNotFound from "../exception/useCaseNotFound";
35
import SendMessageToChannelUseCase from "../../application/usecases/sendMessageToChannel/sendMessageToChannelUseCase";
@@ -8,8 +10,6 @@ import ChannelResolver from "./channelResolver";
810
import KataService from "./kataService/kataService";
911
import SendCodewarsLeaderboardToChannelUseCase from "../../application/usecases/sendCodewarsLeaderboardToChannel/sendCodewarsLeaderboardToChannelUseCase";
1012

11-
type CallbackFunctionVariadic = (...args: unknown[]) => void;
12-
1313
export default class CommandUseCaseResolver {
1414
private messageRepository: MessageRepository;
1515

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

2222
private kataService: KataService;
2323

24+
private commandMessages: Record<string, string> = {};
25+
2426
constructor({
2527
messageRepository,
2628
chatService,
@@ -41,7 +43,13 @@ export default class CommandUseCaseResolver {
4143
this.kataService = kataService;
4244
}
4345

44-
resolveByCommand(command: string, context: Context): void {
46+
private async loadCommands(): Promise<void> {
47+
const filePath = path.join(__dirname, "commands.json");
48+
const data = await fs.readFile(filePath, "utf-8");
49+
this.commandMessages = JSON.parse(data);
50+
}
51+
52+
async resolveByCommand(command: string, context: Context): Promise<void> {
4553
this.loggerService.log(`Command received: "${command}"`);
4654

4755
const deps = {
@@ -52,28 +60,25 @@ export default class CommandUseCaseResolver {
5260
kataService: this.kataService,
5361
};
5462

55-
const commandUseCases: Record<string, CallbackFunctionVariadic> = {
56-
"!ja": async () =>
57-
new SendMessageToChannelUseCase(deps).execute({
58-
channelId: context.channelId,
59-
message:
60-
"Olá! Experimenta fazer a pergunta diretamente e contar o que já tentaste! Sabe mais aqui :point_right: https://dontasktoask.com/pt-pt/",
61-
}),
62-
"!oc": async () =>
63-
new SendMessageToChannelUseCase(deps).execute({
64-
channelId: context.channelId,
65-
message: ":warning: Este servidor é APENAS para questões relacionadas com programação! :warning:",
66-
}),
67-
"!cwl": async () =>
68-
new SendCodewarsLeaderboardToChannelUseCase(deps).execute({
69-
channelId: context.channelId,
70-
}),
71-
};
63+
if (Object.keys(this.commandMessages).length === 0) {
64+
await this.loadCommands();
65+
}
66+
67+
if (this.commandMessages[command]) {
68+
new SendMessageToChannelUseCase(deps).execute({
69+
channelId: context.channelId,
70+
message: this.commandMessages[command],
71+
});
72+
return;
73+
}
7274

73-
if (!commandUseCases[command]) {
74-
throw new UseCaseNotFound().byCommand(command);
75+
if (command === "!cwl") {
76+
new SendCodewarsLeaderboardToChannelUseCase(deps).execute({
77+
channelId: context.channelId,
78+
});
79+
return;
7580
}
7681

77-
commandUseCases[command]();
82+
throw new UseCaseNotFound().byCommand(command);
7883
}
7984
}

domain/service/commands.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"!ja": "Olá! Experimenta fazer a pergunta diretamente e contar o que já tentaste! Sabe mais aqui :point_right: https://dontasktoask.com/pt-pt/",
3+
"!oc": ":warning: Este servidor é APENAS para questões relacionadas com programação! :warning:"
4+
}

0 commit comments

Comments
 (0)