🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

cleverbot-irc

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cleverbot-irc - npm Package Compare versions

Comparing version
0.1.0
to
0.1.1
+36
bot/echo_protection.js
var Levenshtein = require('levenshtein');
var responses = {}; // maintains the list of last sentences said to each user
var ignores = []; // maintains the ignore list
// checks if `user` is ignored
exports.isIgnored = function (user) {
return ignores.indexOf(user) >= 0;
};
// remember the last thing said to `user`
exports.remember = function (user, resp) {
responses[user] = resp;
};
// returns if last thing `user` said is close to the last thing WE said
exports.isTooSimilar = function (user, msg) {
if (!responses[user]) {
return false;
}
var ld = new Levenshtein(responses[user], msg);
return (ld.distance < msg.length/8);
};
// ignores `user` for up to ignoreMax seconds
exports.ignore = function (user, ignoreMax) {
var ignoreTime = Math.ceil(ignoreMax*1000*Math.random());
console.log('ignoring', user, 'for', Math.floor(ignoreTime/1000) + 's');
ignores.push(user);
setTimeout(function () {
console.log('unignoring:', user);
ignores.splice(ignores.indexOf(user), 1);
}, ignoreTime);
return ignoreTime;
};
var zalgo = require('dye').zalgo
, sunCalc = require('suncalc')
, col = require('irc-colors');
/**
* Full Moon Spiceup
* when moon is sufficiently full, clvr speaks with modifications
* the modifications become more intense closer to the full moon
*/
const cutOff = 0.95; // "how full" does a moon have to be to be considered full
var moonIntensity = function (d) {
// (moonFraction - cutOff) is how far into the "full moon" interval we are
// (1 - cutOff) is the length of that interval
// => moon { 0 if outside interval, between 0 and 1 inside interval, 1 at peak}
return Math.max(0, (sunCalc.getMoonFraction(d) - cutOff)/(1 - cutOff));
};
var comp = function (f, g) {
return function (x) {
return f(g(x));
};
};
var colorMap = function (intensity) {
var fnProgress = [
col.aqua, // .1
col.violet, // .3
comp(col.bold, col.olive), // .4
comp(col.bold, col.pink), // .6
col.rainbow, // .7
comp(col.rainbow, col.bold)
];
var l = fnProgress.length;
var idx = Math.min(l - 1, Math.floor(intensity * l)); // map intensity to prog idx
return fnProgress[idx];
};
module.exports = function (resp) {
var d = new Date();
var intensity = moonIntensity(d);
if (intensity > 0) { // full moon
// sometimes colorize
if (d.getMonth() % 2) {
return colorMap(intensity)(resp);
}
// sometimes zalgolize
return zalgo(resp, 0.1, [
Math.ceil(10*intensity),
Math.ceil(6*intensity),
Math.ceil(10*intensity)
]);
}
return resp;
};
+18
-64
var CleverBot = new require('cleverbot-node')
, clever = new CleverBot()
, dye = require('dye')
, Levenshtein = require('levenshtein')
, format = require('util').format
, sunCalc = require('suncalc');
, protection = require('./echo_protection')
, maybeSpiceUp = require('./fullmoon_spiceup');

@@ -11,4 +9,4 @@ /**

* used to notify in channel if we are ignoring a person
* or in pms without the correct password
*/
const ignoreMax = 3600;
var insult = (function () {

@@ -32,68 +30,24 @@ var insults = [

/**
* Full Moon Based Zalgolizer
* when moon is sufficiently full, clvr speaks with the zalgolizer
* the zalgolizer grows more intense the closer to full moon it is
*/
const fullMoonCutoff = 0.95;
var getZalgoIntensities = function () {
var len = 1 - fullMoonCutoff;
var dist = sunCalc.getMoonFraction(Date.now()) - fullMoonCutoff;
if (dist < 0) {
// not a full moon!
return [0, 0, 0];
}
// full moon
var max = dist/len;
console.log(dye.magenta('full moon intensity at ' + max + '%'));
// [0, 0, 0] at [0, fullMoonCutoff), [10, 6, 10] at full moon (=1)
// linear interpolation in the range [fullMoonCutoff, 1]
return [
Math.ceil(10*max),
Math.ceil(6*max),
Math.ceil(10*max)
];
};
var repeats = {}; // maintains last said thing for a user
var ignores = [];
const ignoreMax = 3600;
module.exports = function (gu) {
gu.on(/(.*)/, function (content, from) {
if (ignores.indexOf(from) >= 0) {
return; // ignored person
}
// repeat messengers should not echo back 'basically' what cleverbot said
if (repeats[from]) {
var ld = new Levenshtein(repeats[from], content);
if (ld.distance < content.length/8) {
var ignoreTime = Math.ceil(ignoreMax*1000*Math.random());
console.log(dye.yellow(
'ignoring ' + from + ' for ' + Math.floor(ignoreTime/1000) + 's'
));
ignores.push(from);
gu.say(from + ": " + insult());
setTimeout(function () {
console.log(dye.yellow(format('unignoring', from)));
ignores.splice(ignores.indexOf(from), 1);
}, ignoreTime);
return;
gu.on(/(.*)/, function (message, user) {
if (!protection.isIgnored(user)) {
if (protection.isTooSimilar(user, message)) {
gu.say(user + ': ' + insult());
return protection.ignore(user, ignoreMax);
}
}
// pass data onto cleverbot
clever.write(content, function (data) {
// cache response for user so we can catch if they mime on next message
repeats[from] = data.message;
// pass message on to cleverbot
clever.write(message, function (data) {
var resp = data.message;
// remember the last thing `user` got returned to him
// so we can verify that he doesn't simply echo it back
protection.remember(user, resp);
// get message and zalgolize if close to full moon
var resp = dye.zalgo(data.message, 0.1, getZalgoIntensities());
gu.say(from + ': ' + resp);
});
// do fancy things to the message on full moons
gu.say(user + ': ' + maybeSpiceUp(resp));
});
}
});
};

@@ -5,3 +5,3 @@ {

"description": "IRC bot that defers to Cleverbot",
"version": "0.1.0",
"version": "0.1.1",
"repository": {

@@ -20,5 +20,6 @@ "type": "git",

"dye": "~0.2.0",
"gu": "~0.0.1",
"gu": "~0.0.2",
"cleverbot-node": "~0.1.2",
"suncalc": "~1.2.1",
"irc-colors": "~1.0.3",
"confortable": "~0.2.1"

@@ -25,0 +26,0 @@ },

@@ -24,2 +24,8 @@ # cleverbot-irc

## Quirks
Because spare time.
- `clvr` will go a little crazy close to every full moon
- imitating the responses of `clvr` back to her can get you ignored for some time
## Internal Highlights

@@ -26,0 +32,0 @@