-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathABResponseInteraction.ts
78 lines (70 loc) · 1.65 KB
/
ABResponseInteraction.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import Interaction from '../ChatServer/Interaction';
import DashBot from '../DashBot';
import { EventForEmitter } from '../Events';
import Tracery from '../tracery/Tracery';
type Trigger = string | RegExp | Array<string | RegExp>;
type Response = string | string[];
/**
* Simple a -> b lookup.
*
* ```
* new ABMessageAction(this, [
* ['a', 'b']
* ])
* ```
*
* Eg. If the message is "a" it will respond with "b"
*/
export default class ABResponseInteraction implements Interaction {
constructor(protected aBResponses: [Trigger, Response][]) {}
register(bot: DashBot) {
bot.on('message', this.onMessage.bind(this));
}
async onMessage(event: EventForEmitter<DashBot, 'message'>) {
const message = event.data;
const content = message.textContent;
for (const response of this.aBResponses) {
const triggers =
response[0] instanceof Array ? response[0] : [response[0]];
for (const trigger of triggers) {
if (typeof trigger === 'string') {
if (trigger === content) {
message.channel.sendText(
Tracery.generate(
{
origin: response[1],
author: {
username: message.author.username,
},
},
'origin'
)
);
event.cancel();
return;
}
} else {
const match = trigger.exec(content);
if (match) {
message.channel.sendText(
Tracery.generate(
{
origin: response[1],
target: {
username: message.author.username,
},
match: {
...match.groups,
},
},
'origin'
)
);
event.cancel();
return;
}
}
}
}
}
}