This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
277 lines (231 loc) · 6.51 KB
/
index.js
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* Verifies that a value exists before setting it. Throws an error if the
* value doesn't exist.
* @param {Object} value The value to check.
* @param {String} msg The error message to use. (Optional)
* @throws {MissingValueError}
* @return {Object} The value input, if it exists.
*/
function validate(value, msg) {
if(!value) {
throw new MissingValueError(msg);
}
return value;
}
function MissingValueError(msg) {
if(!msg) {msg = 'A required value was missing!';}
this.name = 'MissingValueError';
this.message = msg;
this.code = 'E_MISSINGNO';
Error.captureStackTrace(this, Context);
}
function CustomDataError(msg) {
if(!msg) {msg = 'That key already exists!';}
else {msg = `The key "${msg}" already exists!`;}
this.name = 'CustomDataError';
this.message = msg;
this.code = 'E_EXISTS';
Error.captureStackTrace(this, Context);
}
require('util').inherits(MissingValueError, Error);
require('util').inherits(CustomDataError, Error);
const webURLRegex = require('./URLRegex');
function isCommand(delimiters, text) {
for (var i = 0; i < delimiters.length; i++) {
if(text.startsWith(delimiters[i])) {
return true;
}
}
return false;
}
function getCommand(delimiters, text) {
for (var i = 0; i < delimiters.length; i++) {
if(text.startsWith(delimiters[i])) {
//slice off the delimiter, split on space to separate into words.
var arr = text.slice(delimiters[i].length).split(' ');
var command = arr.shift().trim();
var args = arr.join(' ').trim();
return {
delimiter: delimiters[i],
command: command,
args: args
};
}
}
return {
delimiter: null,
command: null,
args: text
};
}
function parseArgs(text) {
var raw = require('string-argv')(text);
var prepared = require('minimist')(raw);
return {raw: raw, prepared: prepared};
}
function Context(options) {
if(!options) {options = {};}
// Required parameters.
var instance = validate(options.instance, 'No instance specified! (options.instance)');
var instanceType = validate(options.instanceType, 'No instance type specified! (options.instanceType)');
var nick = validate(options.nick, 'No nick specified! (options.nick)');
var text = validate(options.text, 'No text specified! (options.text)');
var to = validate(options.to, 'No channel specified! (options.to)');
var user = validate(options.user, 'No user specified! (options.user)');
// Not so required parameters.
var commandDelimiters = options.commandDelimiters || ['.'];
// In case we're provided with a String.
if(!Array.isArray(commandDelimiters)) {
commandDelimiters = [commandDelimiters];
}
var isAlert = options.isAlert || false;
var customData = {};
var myNick = options.myNick || 'AKP48';
var permissions = options.permissions || [];
var rawMessage = options.message || options.text;
// Generated parameters.
var isCmd = false;
var command = '';
var args = {};
var rawArgs = [];
var argText = '';
// if we don't have a string, we still want to send messages, but just use the
// default values above.
if(typeof text === 'string') {
isCmd = options.isCommand || isCommand(commandDelimiters, text) || false;
var tempCommand = getCommand(commandDelimiters, text);
command = tempCommand.command;
var tempArgs = parseArgs(tempCommand.args);
args = tempArgs.prepared;
rawArgs = tempArgs.raw;
argText = tempCommand.args;
}
this.args = function() {
return args;
};
this.argText = function() {
return argText;
};
this.clone = function() {
return new Context(this.toObject());
};
this.cloneWith = function(opts) {
var obj = this.toObject();
var keys = Object.keys(opts);
// don't be tempted to change this to a for...in. That doesn't work, apparently.
for (var i = 0; i < keys.length; i++) {
var prop = keys[i];
obj[prop] = opts[prop];
}
return new Context(obj);
};
this.command = function() {
return command;
};
this.commandDelimiters = function() {
return commandDelimiters;
};
this.commandDelimiterUsed = function() {
return tempCommand.delimiter;
};
this.getCustomData = function(k) {
if(!k) {
throw new MissingValueError('Context.getCustomData requires a key!');
}
if(!customData[k]) { return null; }
return customData[k];
};
this.getCustomKeys = function() {
return Object.keys(customData);
};
this.hasURL = function () {
return webURLRegex.test(this.text());
};
this.instance = function() {
return instance;
};
this.instanceId = function() {
return instance._id;
};
this.instanceName = function() {
return instance._name || '';
};
this.instanceType = function() {
return instanceType;
};
this.isAlert = function() {
return isAlert;
};
this.isCommand = function() {
return isCmd;
};
// this seems redundant, but I like it.
this.isContext = function() {
return this instanceof Context;
};
this.myNick = function() {
return myNick;
};
this.nick = function() {
return nick;
};
this.permissions = function() {
return permissions;
};
this.rawArgs = function() {
return rawArgs;
};
this.rawMessage = function() {
return rawMessage;
};
this.setCustomData = function(k, v) {
if(customData[k]) {
throw new CustomDataError(k);
} else {
customData[k] = v;
}
return this;
};
this.text = function() {
return text;
};
this.to = function() {
return to;
};
this.toObject = function() {
return {
commandDelimiters: this.commandDelimiters(),
instance: this.instance(),
instanceType: this.instanceType(),
isAlert: this.isAlert(),
isCommand: this.isCommand(),
myNick: this.myNick(),
nick: this.nick(),
permissions: this.permissions(),
rawMessage: this.rawMessage(),
text: this.text(),
to: this.to(),
user: this.user()
};
};
this.user = function() {
return user;
};
this.reply = function(msg) {
// if the message we got is actually a context itself,
// we'll just pull the text from it and reply using it.
if(msg instanceof Context) {
msg.reply(msg.text());
return;
}
// if the message doesn't exist, we'll use the text that this context contains.
if(!msg) {
msg = this.text();
}
instance._AKP48.sendMessage(msg, this);
};
this.answerToTheUltimateQuestionOfLifeTheUniverseAndEverything = function() {
return 42;
};
}
module.exports = Context;