-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
56 lines (51 loc) · 2.39 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { ChatCommandManager } from './src/commands/ChatCommandManager';
import * as express from 'express';
import { AddressInfo } from 'net';
import * as path from 'path';
import { Message, TextChannel, User } from 'discord.js';
import * as TypeMoq from 'typemoq';
import { IActionN } from 'typemoq/_all';
import { ChatterService, OutputType, ChatterServiceOptions } from './src/commands/ChatterService';
import { EchoHelpService } from './src/commands/EchoHelpService';
const chatterYAML = new ChatterServiceOptions().mergeWith({ splitMessages: false, outputType: OutputType.YAML });
const chatterService = new ChatterService(chatterYAML);
const echoHelpService = new EchoHelpService(new ChatterService(chatterYAML));
const chatCommandManager = new ChatCommandManager(chatterService, echoHelpService);
const app = express();
app.use(express.urlencoded());
app.use(express.static('assets'));
app.use('/bootstrap-dark', express.static('node_modules\\@forevolve\\bootstrap-dark\\dist\\css'));
const listener = app.listen(8889, function() {
console.log('Listening on port ' + (listener.address() as AddressInfo).port);
});
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.post('/command', async function(req, res) {
const chatCommand = req.body.chatCommand;
let output = '';
const messageMock = MakeMessage(chatCommand, chat => (output += chat));
await chatCommandManager.Handle(messageMock.object);
output = cleanMarkdownCodeBreak(output, OutputType.JSON);
output = cleanMarkdownCodeBreak(output, OutputType.YAML);
res.send(output);
});
function cleanMarkdownCodeBreak(output: string, type: OutputType) {
const spacerString = '\n``````' + type.toLowerCase() + '\n';
do {
output = output.replace(spacerString, '');
} while (output.indexOf(spacerString) > -1);
return output;
}
function MakeMessage(chatCommand: string, callback: IActionN<string>): TypeMoq.IMock<Message> {
const messageMock: TypeMoq.IMock<Message> = TypeMoq.Mock.ofType(Message);
const userMock: TypeMoq.IMock<User> = TypeMoq.Mock.ofType(User);
messageMock
.setup(message => message.reply(TypeMoq.It.isAnyString()))
.callback(callback)
.returns(message => Promise.resolve(message));
messageMock.object.author = userMock.object;
messageMock.object.content = chatCommand;
userMock.object.bot = false;
return messageMock;
}