/
what.js
69 lines (48 loc) · 1.97 KB
/
what.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
// What.js
// "What is your name?",
// "What is the capital of Spain?"
// -------------------------------------------------- //
var lang = require("../brain/language")
, fileEx = Nodebot.lexicon.file["regular expression"]
;
module.exports = function what (a) {
if (a.owner === a.subject) a.subject = "definition";
var nodebot = this
, owner = a.owner
, subject = a.subject || "definition"
, base = (owner) ? nodebot.lexicon[owner] : nodebot.lexicon
, term = (subject && base) ? base[subject] : undefined
;
// Just some more bullet proofing for the subject
if (owner === subject) subject === "definition"
// If the term is a function, call it to determin the value
if (typeof term === "function") term = term().toString();
// If it is a file, simply return that it is a file
if (fileEx.test(subject)) {
return nodebot.say(owner.cyan + " is a file.").request();
}
// Do we have a definition for ths subject?
// -------------------------------------------------- //
if (term) {
switch (subject) {
case "definition":
nodebot.say(lang.capitalize(owner) + " is " + term.green.bold);
break;
default:
nodebot.say(lang.possessify(lang.capitalize(owner)) + " " + subject + " is " + term.toString().green.bold);
}
return nodebot.request();
}
// no : learn it
nodebot.ask("Hmm... I can't remember. Care to tell me what " + lang.possessify(owner) + " " + subject + " is?", function(text) {
if (text[0].toLowerCase() === "no") {
nodebot.say("Okay, I'll forget you ever asked.");
return nodebot.request();
}
nodebot.lexicon[owner] = nodebot.lexicon[owner] || {};
nodebot.lexicon[owner][subject] = text;
nodebot.say("Great, now I know!");
return nodebot.request();
});
return;
};