1
+ import { promises as fs } from "fs" ;
2
+ import path from "path" ;
1
3
import { Context } from "../../types" ;
2
4
import UseCaseNotFound from "../exception/useCaseNotFound" ;
3
5
import SendMessageToChannelUseCase from "../../application/usecases/sendMessageToChannel/sendMessageToChannelUseCase" ;
@@ -8,8 +10,6 @@ import ChannelResolver from "./channelResolver";
8
10
import KataService from "./kataService/kataService" ;
9
11
import SendCodewarsLeaderboardToChannelUseCase from "../../application/usecases/sendCodewarsLeaderboardToChannel/sendCodewarsLeaderboardToChannelUseCase" ;
10
12
11
- type CallbackFunctionVariadic = ( ...args : unknown [ ] ) => void ;
12
-
13
13
export default class CommandUseCaseResolver {
14
14
private messageRepository : MessageRepository ;
15
15
@@ -21,6 +21,8 @@ export default class CommandUseCaseResolver {
21
21
22
22
private kataService : KataService ;
23
23
24
+ private commandMessages : Record < string , string > = { } ;
25
+
24
26
constructor ( {
25
27
messageRepository,
26
28
chatService,
@@ -41,7 +43,13 @@ export default class CommandUseCaseResolver {
41
43
this . kataService = kataService ;
42
44
}
43
45
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 > {
45
53
this . loggerService . log ( `Command received: "${ command } "` ) ;
46
54
47
55
const deps = {
@@ -52,28 +60,25 @@ export default class CommandUseCaseResolver {
52
60
kataService : this . kataService ,
53
61
} ;
54
62
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
+ }
72
74
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 ;
75
80
}
76
81
77
- commandUseCases [ command ] ( ) ;
82
+ throw new UseCaseNotFound ( ) . byCommand ( command ) ;
78
83
}
79
84
}
0 commit comments