3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-23 12:29:26 +01:00

Merge pull request #676 from Scritches/master

Various module fixes / improvements
This commit is contained in:
Luke Slater 2018-05-05 16:34:04 +01:00 committed by GitHub
commit 814a84ee8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 236 additions and 56 deletions

View File

@ -19,18 +19,29 @@ var kill_namespam = function(dbot) {
var matchedPattern = _.find(this.config.cliconn_patterns, var matchedPattern = _.find(this.config.cliconn_patterns,
function(p) { try { return event.message.match(p); } catch(e) {}; }); // ok.jpg function(p) { try { return event.message.match(p); } catch(e) {}; }); // ok.jpg
if(matchedPattern) { if(matchedPattern) {
var ip = event.message.split(' ')[1] var nick = event.message.split(' ')[2];
dbot.api.nickserv.getUserHost(event.server, nick, function(host) {
var userIsAuthenticated = host && host.startsWith('tripsit/');
if (userIsAuthenticated) {
event.reply(dbot.t('clikill_spared', {
'user': nick,
'pattern': matchedPattern
}));
} else {
var ip = event.message.split(' ')[1]
// Alternatively you can just do dbot.api.kick.kill(event.server, event.user, message); // Alternatively you can just do dbot.api.kick.kill(event.server, event.user, message);
dbot.say(event.server, 'operserv', 'akill add *@'+ ip +' !P Naughty Nelly Auto-kill v6.2. Matched pattern: /'+ matchedPattern +'/'); dbot.say(event.server, 'operserv', 'akill add *@'+ ip +' !P Naughty Nelly Auto-kill v6.2. Matched pattern: /'+ matchedPattern +'/');
var msg = dbot.t('clikill_act', { var msg = dbot.t('clikill_act', {
'ip': ip, 'ip': ip,
'pattern': matchedPattern 'pattern': matchedPattern
}); });
event.reply(msg); event.reply(msg);
dbot.api.report.notify('autokill', event.server, event.rUser, dbot.api.report.notify('autokill', event.server, event.rUser,
dbot.config.servers[event.server].admin_channel, msg, ip, ip); dbot.config.servers[event.server].admin_channel, msg, ip, ip);
}
}, true);
} }
} }
} }

View File

@ -7,5 +7,8 @@
}, },
"clikill_act": { "clikill_act": {
"en": "Added K-Line for {ip}, due to matching pattern: /{pattern}/" "en": "Added K-Line for {ip}, due to matching pattern: /{pattern}/"
},
"clikill_spared": {
"en": "{user} spared from clikill matched pattern: /{pattern}/"
} }
} }

View File

@ -9,15 +9,15 @@ var request = require('request'),
var link = function(dbot) { var link = function(dbot) {
this.urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; this.urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
this.links = {}; this.links = {};
this.handlers = []; this.handlers = [];
this.api = { this.api = {
'addHandler': function(name, regex, handler) { 'addHandler': function(name, regex, handler) {
this.handlers.push({ this.handlers.push({
'name': name, 'name': name,
'regex': regex, 'regex': regex,
'callback': handler 'callback': handler
}); });
}, },
@ -43,8 +43,8 @@ var link = function(dbot) {
}, },
'udLookup': function(query, callback) { 'udLookup': function(query, callback) {
var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' + var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' +
encodeURI(query); encodeURI(query);
request(reqUrl, function(error, response, body) { request(reqUrl, function(error, response, body) {
try { try {
@ -62,14 +62,14 @@ var link = function(dbot) {
'parseLink': function(link, callback) { 'parseLink': function(link, callback) {
var handler = false; var handler = false;
for(var i=0;i<this.handlers.length;i++) { for(var i=0;i<this.handlers.length;i++) {
var matches = this.handlers[i].regex.exec(link); var matches = this.handlers[i].regex.exec(link);
if(matches) { if(matches) {
console.log(this.handlers[i].name); console.log(this.handlers[i].name);
handler = this.handlers[i]; handler = this.handlers[i];
break; break;
} }
} }
if(handler) { if(handler) {
this.handlers[i].callback(matches, this.handlers[i].name, function(parsed) { this.handlers[i].callback(matches, this.handlers[i].name, function(parsed) {
callback(parsed); callback(parsed);
@ -81,7 +81,7 @@ var link = function(dbot) {
} }
} }
}; };
var commands = { var commands = {
'~title': function(event) { '~title': function(event) {
var link = this.links[event.channel.name]; var link = this.links[event.channel.name];
@ -95,9 +95,10 @@ var link = function(dbot) {
event.reply(dbot.t('link', { 'link': title} )); event.reply(dbot.t('link', { 'link': title} ));
}); });
}, },
'~xkcd': function(event) { '~xkcd': function(event) {
var comicId = event.params[1] || ""; //var comicId = event.params[1] || "";
var comicId = event.params.slice(1).join(' ');
if(comicId == "*") { if(comicId == "*") {
request("http://xkcd.com/info.0.json", function(error, response, body){ request("http://xkcd.com/info.0.json", function(error, response, body){
@ -108,27 +109,86 @@ var link = function(dbot) {
dbot.commands['~xkcd'](event); dbot.commands['~xkcd'](event);
} }
} catch(err) { }; } catch(err) { };
});
} else {
if(comicId !== "") {
comicId = comicId + "/";
}
var link = "http://xkcd.com/"+comicId+"info.0.json";
request(link, function(error, response, body) {
try {
if (response.statusCode == "200") {
data = JSON.parse(body);
event.reply(dbot.t("xkcd", data));
} else {
event.reply(dbot.t("no-hits"));
}
} catch(err) { };
}); });
} else {
if (isNaN(parseInt(comicId))) {
request({
url: 'http://www.explainxkcd.com/wiki/api.php',
qs: {
action: 'query',
format: 'json',
generator: 'search',
gsrwhat: 'text',
gsrsearch: comicId,
prop: 'info|categories',
gsrlimit: 50
},
json: true
}, function(err, res, body) {
if(!body) {
event.reply(dbot.t("no-hits"));
return;
}
var pages = _.values(body.query.pages);
// page titles must be of the format "####: $$$$$$"
pages = _.filter(pages, p => p.title.indexOf(':') > 0);
if (pages.length > 0) {
// See if any of these matches are exact title matches
var match = false;
_.each(pages, function(p) {
var title = p.title.slice(p.title.indexOf(':')+2).trim();
if(title.toLowerCase() == comicId.toLowerCase()) {
match = p;
}
});
if (match) {
// We got a match! Get the ID and let's get tf out of here.
comicId = match.title.slice(0, match.title.indexOf(':'));
} else {
comicId = pages[0].title.slice(0, pages[0].title.indexOf(':'));
}
var link = "http://xkcd.com/"+comicId+"/info.0.json";
request(link, function(error, response, body) {
try {
if (response.statusCode == "200") {
data = JSON.parse(body);
event.reply(dbot.t("xkcd", data));
} else {
event.reply(dbot.t("no-hits"));
}
} catch(err) { };
});
} else {
event.reply(dbot.t("no-hits"));
}
});
} else {
if(comicId !== "") {
comicId = comicId + "/";
}
var link = "http://xkcd.com/"+comicId+"info.0.json";
request(link, function(error, response, body) {
try {
if (response.statusCode == "200") {
data = JSON.parse(body);
event.reply(dbot.t("xkcd", data));
} else {
event.reply(dbot.t("no-hits"));
}
} catch(err) { };
});
}
} }
}, },
'~ud': function(event) { '~ud': function(event) {
var query = event.input[1]; var query = event.input[1];
@ -151,7 +211,7 @@ var link = function(dbot) {
console.log('DEBUG: got a link'); console.log('DEBUG: got a link');
if(this.config.autoTitle == true) { if(this.config.autoTitle == true) {
this.api.parseLink(urlMatches[0], function(result) { this.api.parseLink(urlMatches[0], function(result) {
event.reply(result); event.reply(result);
}); });
} }
} }

View File

@ -26,18 +26,22 @@ var nickserv = function(dbot) {
}.bind(this), 6000); }.bind(this), 6000);
}, },
'getUserHost': function(server, nick, callback) { 'getUserHost': function(server, nick, callback, skipFallback) {
if(!_.has(this.userStack, server)) this.userStack[server] = {}; if(!_.has(this.userStack, server)) this.userStack[server] = {};
this.userStack[server][nick] = callback; this.userStack[server][nick] = callback;
dbot.instance.connections[server].send('USERHOST ' + nick); dbot.instance.connections[server].send('USERHOST ' + nick);
setTimeout(function() { setTimeout(function() {
if(_.has(this.userStack[server], nick)) { if(_.has(this.userStack[server], nick)) {
dbot.instance.connections[server].send('WHOWAS ' + nick + ' 1'); if (skipFallback) {
setTimeout(function() { callback(false);
if(_.has(this.userStack[server], nick)) { } else {
callback(false); dbot.instance.connections[server].send('WHOWAS ' + nick + ' 1');
} setTimeout(function() {
}.bind(this), 2000); if(_.has(this.userStack[server], nick)) {
callback(false);
}
}.bind(this), 2000);
}
} }
}.bind(this), 4000); }.bind(this), 4000);
} }

6
modules/omdb/config.json Normal file
View File

@ -0,0 +1,6 @@
{
"dependencies": [ ],
"ignorable": true,
"outputPrefix": "omdb",
"api_key": "insert api key here - get from http://www.omdbapi.com/apikey.aspx"
}

83
modules/omdb/omdb.js Normal file
View File

@ -0,0 +1,83 @@
/**
* Module Name: omdb
* Description: Interacts with the Open Movie Database to provide movie summary
* and review information.
*/
var rp = require('request-promise-native'),
_ = require('underscore')._;
var OMDB = function(dbot) {
this.apiRoot = 'http://www.omdbapi.com';
this.imdbLinkPrefix = 'https://www.imdb.com/title/';
this.internalAPI = {
formatLink: r => {
var aRating = parseFloat(r.imdbRating) * 10;
var cRating = parseFloat(r.Metascore);
if (isNaN(aRating)) {
aRating = " N/A";
} else {
var aColour = (aRating <= 5) ? '\u00033 ' : '\u00034 ';
aRating = aColour + String(aRating) + '%\u000f';
}
if (isNaN(cRating)) {
cRating = " N/A";
} else {
var cColour = (cRating <= 5) ? '\u00033 ' : '\u00034 ';
cRating = cColour + String(cRating) + '%\u000f';
}
var mString = dbot.t('omdb_film', {
'title': r.Title,
'year': r.Year,
'aRating': aRating,
'cRating': cRating
});
if (_.has(r, 'Director') && r.Director != "N/A") mString += ' [Director: ' + r.Director + ']';
if (_.has(r, 'Genre') && r.Genre != "N/A") mString += ' [Genre: ' + r.Genre + ']';
if (_.has(r, 'Plot') && r.Plot != "N/A") {
if (r.Plot.length > 140) r.Plot = r.Plot.substring(0, 140) + '...';
mString += ' [Plot: ' + r.Plot + ']';
}
mString += ' - ' + this.imdbLinkPrefix + r.imdbID;
return mString;
}
};
this.commands = {
'~movie': async event => {
try {
var r = await rp({
url: this.apiRoot,
qs: {
apikey: this.config.api_key,
t: event.input[1],
plot: 'short',
r: 'json'
},
json: true
});
if (r.Response === 'True') {
event.reply(this.internalAPI.formatLink(r));
} else {
event.reply(dbot.t('omdb_noresults'));
}
}
catch (e) {
console.log(e);
}
}
};
this.commands['~movie'].regex = [/^movie (.+)$/, 2];
}
exports.fetch = dbot => new OMDB(dbot);

10
modules/omdb/strings.json Normal file
View File

@ -0,0 +1,10 @@
{
"omdb_film": {
"en": "[{title} - Audience:{aRating} - Critic:{cRating} - {year}]",
"de": "[{title} - {aRating} - {year}]"
},
"omdb_noresults": {
"en": "No films found.",
"de": "Kein Film gefunden."
}
}

View File

@ -113,6 +113,8 @@ var spotify = function(dbot) {
} }
} }
}; };
commands['~sp'] = commands['~spotify'].bind(this);
commands['~sp'].regex = [/^sp (.*)/, 2];
commands['~spotify'].regex = [/^spotify (.*)/, 2]; commands['~spotify'].regex = [/^spotify (.*)/, 2];
this.commands = commands; this.commands = commands;

View File

@ -23,20 +23,21 @@ var youtube = function(dbot) {
}, },
'json': true 'json': true
}, function(error, response, body) { }, function(error, response, body) {
callback(body); callback(body);
}.bind(this)); }.bind(this));
} }
}; };
this.internalAPI = { this.internalAPI = {
'formatLink': function(v) { 'formatLink': function(v) {
var time = v.contentDetails.duration.match(/^PT(\d+)?M?(\d+)S$/); var time = v.contentDetails.duration.match(/^PT(?:(\d+)M)?(\d+)S$/);
if(time) { if(time) {
if(time[1]) { if(time[1]) {
var seconds =((time[2]%60 < 10) ? "0"+time[2]%60 : time[2]%60), var seconds =((time[2]%60 < 10) ? "0"+time[2]%60 : time[2]%60),
minutes = time[1]; minutes = time[1];
} else { } else {
var seconds =((time[1]%60 < 10) ? "0"+time[1]%60 : time[1]%60), var seconds =((time[2]%60 < 10) ? "0"+time[2]%60 : time[2]%60),
minutes = 0; minutes = 0;
} }
} else { } else {
@ -61,18 +62,18 @@ var youtube = function(dbot) {
return res; return res;
}.bind(this), }.bind(this),
'formatPlaylistLink': function(v) { 'formatPlaylistLink': function(v) {
var res = dbot.t('yt_playlist', { var res = dbot.t('yt_playlist', {
'title': v.snippet.title, 'title': v.snippet.title,
'author': v.snippet.channelTitle, 'author': v.snippet.channelTitle,
'videos': v.contentDetails.itemCount 'videos': v.contentDetails.itemCount
}); });
if (v.id) { if (v.id) {
res += " - https://www.youtube.com/playlist?list=" + v.id; res += " - https://www.youtube.com/playlist?list=" + v.id;
} }
return res; return res;
} }
}; };
@ -101,7 +102,7 @@ var youtube = function(dbot) {
} }
}.bind(this), "video"); }.bind(this), "video");
}, },
// search for a youtube playlist // search for a youtube playlist
'~ytpl': function(event) { '~ytpl': function(event) {
this.api.search(event.input[1], function(body) { this.api.search(event.input[1], function(body) {