-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrain.ts
208 lines (185 loc) · 5.4 KB
/
brain.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {EventEmitter} from "events";
import * as _ from "lodash";
import Robot from "./robot";
import User from "./user";
export default class Brain extends EventEmitter {
robot: Robot;
data: any;
saveInterval: any;
autoSave: boolean;
constructor(robot) {
super();
this.robot = robot;
this.data = {
users: {}, // id: User
_private: {},
};
this.autoSave = true;
this.robot.on("running", () => {
this.resetSaveInterval(5);
});
}
public set(key, value) {
let pair: any;
if (key === Object(key)) {
pair = key;
} else {
pair = {};
pair[key] = value;
}
_.extend(this.data._private, pair);
this.emit("loaded", this.data);
return this;
}
public get(key) {
return this.data._private[key] != null ? this.data._private[key] : null;
}
// If the plugin supports data per user rather than all users having the same data, use this
// instead of set()
public setForUser(key: string, value: any, userId: string): any {
if (!userId) {
throw new Error("userId cannot be undefined");
}
let data = this.get(key);
if (!data) {
data = {};
}
data[userId] = value;
this.set(key, data);
}
// If the plugin supports data per user rather than all users having the same data, use this
// instead of get()
public getForUser(key: string, userId: string): any {
if (!userId) {
throw new Error("userId cannot be undefined");
}
let data = this.get(key);
if (!data) {
return null;
}
return data[userId];
}
public remove(key) {
if (this.data._private[key] != null) {
return delete this.data._private[key];
} else {
return null;
}
}
public mergeData(data) {
if (data) {
_.extend(this.data, data);
}
this.robot.logger.info(`[brain] Merged data, current keys: ${Object.keys(this.data._private)}`);
this.emit("loaded", this.data);
}
public save() {
this.emit("save", this.data);
}
public close() {
clearInterval(this.saveInterval);
this.save();
this.emit("close");
}
public setAutoSave(enabled) {
this.autoSave = enabled;
}
public resetSaveInterval(seconds) {
if (this.saveInterval) {
clearInterval(this.saveInterval);
}
return (this.saveInterval = setInterval(() => {
if (this.autoSave) {
return this.save();
}
}, seconds * 1000));
}
private getUsers(): User[] {
let users = this.get("users");
if (!users) {
return [];
}
return Object.values(users).map((u) => new User(u));
}
public userForId(id): User {
if (!id) {
this.robot.logger.warn("[brain] userForId cannot search for undefined id");
return undefined;
}
// Ask each user object if the id is contained in thir user object
let user: User;
let users = this.getUsers();
for (let u of users) {
if (u.containsId(id)) {
user = u;
break;
}
}
if (!user) {
return undefined;
} else {
return user;
}
}
// Update or create new user
public updateUser(user: User) {
if (!user || !user.id) {
this.robot.logger.warn(`[brain] Cannot update undefined user: ${user}`);
return;
}
let users = this.get("users");
if (!users) {
users = {};
}
// Should merge the user here rather than just setting it. This just clobbers them.
users[user.id] = user;
this.set("users", users);
}
// Categories
//
// This is a simple way to manage a series of grouped items for the bot, dynamically.
// A good example is a bot that replies to a certain phrase with a random gif. If the list of gifs
// is hard coded in the plugin, it will get boring eventually. This lets the plugin register a
// list of default gifs, then a user can add more gifs to the category, and when a phrase triggers
// the plugin, it fetchs a random item. The items are stored as strings, but could be response
// phrases, image links, or serialized objects.
CATEGORY_KEY = "categories";
public listCategories(): string[] {
let allItems = this.get(this.CATEGORY_KEY) || {};
return Object.keys(allItems);
}
public addItemToCategory(category: string, item: string) {
let allItems = this.get(this.CATEGORY_KEY) || {};
if (!allItems[category]) {
allItems[category] = [];
}
allItems[category].push(item);
this.set(this.CATEGORY_KEY, allItems);
}
public removeItemAtIndexInCategory(category: string, index: number) {
let allItems = this.get(this.CATEGORY_KEY) || {};
let items = allItems[category] || [];
items.splice(index, 1);
allItems[category] = items;
this.set(this.CATEGORY_KEY, allItems);
}
public getRandomItemFromCategory(category: string): string {
let combined = this.listItemsInCategory(category);
return combined[Math.floor(Math.random() * combined.length)];
}
public listItemsInCategory(category: string): string[] {
let allItems = this.get(this.CATEGORY_KEY) || {};
let items = allItems[category] || [];
return items;
}
public registerDefaultsForCateogry(category: string, items: string[]) {
let allItems = this.get(this.CATEGORY_KEY) || {};
let existingItems = allItems[category] || [];
if (existingItems.length > 0) {
return;
}
this.robot.logger.debug(`[brain] Loading defaults for category ${category}`);
allItems[category] = items;
this.set(this.CATEGORY_KEY, allItems);
}
}