-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.ts
42 lines (37 loc) · 1.4 KB
/
commands.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export default class CommandHandler {
commands = {
"login": () => 'Welcome to the Terminal. Type "help" for a list of commands.',
"help": () => 'Available Commands: ' + Object.keys(this.commands).join(", ")
}
// Commands that aren't listed when executing help
secretcommands = {
"secret": () => "Woo hoo, you found a secret command!",
}
// Commands that aren't pushed to github
topsecretcommands = {}
setup() {
Deno.readFile("./secrets.json").then(data => {
const decoder = new TextDecoder("utf-8");
const secrets = JSON.parse(decoder.decode(data));
console.log(secrets);
Object.entries(secrets).forEach(([key, value]) => {
(this.topsecretcommands as any)[key] = () => eval(value as string);
});
}, _error => {})
}
handleCommand(command: string): Promise<Answer> {
return new Promise((resolve) => {
const allcommands = {...this.commands, ...this.secretcommands, ...this.topsecretcommands};
Object.entries(allcommands).forEach(([key, value]) => {
if (command.startsWith(key)) {
resolve({ type: 'text', content: value() })
}
});
resolve({ type: 'text', content: 'Command not found.' })
});
}
}
type Answer = {
type: 'text';
content: string;
}