-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chat.js
97 lines (70 loc) · 2.59 KB
/
Chat.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
module.exports = function(bot, mongoose, db, constants, privates) {
// the bot (so we can actually, y'know, CHAT. Which is what this module does.)
this.bot = bot;
// Our constants.
this.privates = privates;
this.constants = constants;
this.mongoose = mongoose;
/*
------------------ EXAMPLE ENTRY.
foo = {
identifier: "crank_success",
fields: 1,
text: "No prob %1, I went ahead and originated a call for you to %2"
};
*/
// Go ahead and define our ORM schema etc.
this.chatSchema = this.mongoose.Schema({
identifier: String,
fields: Number,
text: String
}, { collection: 'chat' });
this.Chat = this.mongoose.model('Chat', this.chatSchema);
// Now, let's create a method to "say" something to a channel.
this.say = function(identifier,parameters) {
// Ok, so let's count the parameters.
// console.log(parameters.length);
// Now that we know it's length, let's get an appropriate item from mongo.
this.Chat.find({ identifier: identifier, fields: parameters.length},function (err, chat) {
if (err) {
console.log('that didnt work',err);
} else {
// Ok, how many results do we have?
if (chat.length) {
// Pick a random line out of that.
line = chat[this.randomId(chat.length)];
// Now go and replace it's symbols with our parameters..
output = line.text;
for (var i=0; i<parameters.length; i++) {
// console.log("the eye",i);
symbolid = i+1;
re = new RegExp("%" + symbolid);
// console.log(re);
// console.log(parameters[i]);
output = output.replace(re,parameters[i]);
}
// Finally, say it through IRC.
this.ircSay(output);
} else {
this.ircSay("That's an error. I'm saying a generic thing, because I can't find what I'm supposed to specifically say. I'll slap myself about with a trout. [identifier: " + identifier + "]");
}
}
}.bind(this));
};
// Give me a random number based on the number elements in an array.
this.randomId = function(max) {
// Account for the fact it's zero based.
max--;
// Our minimum is always zero (first array element)
min = 0;
// Now just go and get a random number in that range.
return Math.floor(Math.random() * (max - min + 1)) + min;
};
this.ircSay = function(message) {
if (this.privates.IRC_ENABLED) {
this.bot.say(this.privates.IRC_CHANNEL, message);
} else {
console.log('bot would have said:',message);
}
};
};