-
Notifications
You must be signed in to change notification settings - Fork 6
/
troller.js
57 lines (46 loc) · 1.59 KB
/
troller.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
const TROLL_PROBABILITY = 0.001 // 0.1% chance of being randomly trolled by the bot
, MIN_STARTS = 5
, MAX_LENGTH = 250
, WISDOM_PROBABILITY = 0.3 // 30% chance of random wisdom from the bot rather than your own words back
const EchoMunge = require('echomunge')
, echomungeWeb = require('echomunge-web')
, seriousNerds = require('./serious_nerds').map(function (nerd) {
return nerd[0] == '/' ? new RegExp(nerd) : nerd
})
var databases = {}
function dbForUser (nick) {
return databases['$' + nick] || (databases['$' + nick] = new EchoMunge())
}
function logForUser (nick, msg) {
dbForUser(nick).recordSentence(msg)
}
function enoughForUser (nick) {
return dbForUser(nick).starts.length > MIN_STARTS
}
function trollForUser (nick, callback) {
var ret = function (err, db, urls) {
if (urls)
console.log('fetched from web:',urls)
callback(null, db.makeText({ maxLength: MAX_LENGTH, terminate: true }))
}
if (enoughForUser(nick) && Math.random() > WISDOM_PROBABILITY)
ret(null, dbForUser(nick))
else
echomungeWeb(null, ret)
}
function onKeyword (options, message) {
if (message.user == options.nick || seriousNerds.indexOf(message.user) > -1)
return
trollForUser(message.user, function (err, msg) {
message.say(message.user + ': ' + msg)
})
}
function channelMessage (options, message) {
if (message.user == options.nick)
return
if (Math.random() < TROLL_PROBABILITY)
onKeyword(options, message)
logForUser(message.user, message.text[0])
}
module.exports = channelMessage
module.exports.onKeyword = onKeyword