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,6 +19,15 @@ var kill_namespam = function(dbot) {
var matchedPattern = _.find(this.config.cliconn_patterns,
function(p) { try { return event.message.match(p); } catch(e) {}; }); // ok.jpg
if(matchedPattern) {
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);
@ -32,6 +41,8 @@ var kill_namespam = function(dbot) {
dbot.api.report.notify('autokill', event.server, event.rUser,
dbot.config.servers[event.server].admin_channel, msg, ip, ip);
}
}, true);
}
}
}

View File

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

View File

@ -97,7 +97,8 @@ var link = function(dbot) {
},
'~xkcd': function(event) {
var comicId = event.params[1] || "";
//var comicId = event.params[1] || "";
var comicId = event.params.slice(1).join(' ');
if(comicId == "*") {
request("http://xkcd.com/info.0.json", function(error, response, body){
@ -109,6 +110,65 @@ var link = function(dbot) {
}
} 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 + "/";
@ -126,7 +186,7 @@ var link = function(dbot) {
} catch(err) { };
});
}
}
},
'~ud': function(event) {

View File

@ -26,12 +26,15 @@ var nickserv = function(dbot) {
}.bind(this), 6000);
},
'getUserHost': function(server, nick, callback) {
'getUserHost': function(server, nick, callback, skipFallback) {
if(!_.has(this.userStack, server)) this.userStack[server] = {};
this.userStack[server][nick] = callback;
dbot.instance.connections[server].send('USERHOST ' + nick);
setTimeout(function() {
if(_.has(this.userStack[server], nick)) {
if (skipFallback) {
callback(false);
} else {
dbot.instance.connections[server].send('WHOWAS ' + nick + ' 1');
setTimeout(function() {
if(_.has(this.userStack[server], nick)) {
@ -39,6 +42,7 @@ var nickserv = function(dbot) {
}
}.bind(this), 2000);
}
}
}.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];
this.commands = commands;

View File

@ -30,13 +30,14 @@ var youtube = function(dbot) {
this.internalAPI = {
'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[1]) {
var seconds =((time[2]%60 < 10) ? "0"+time[2]%60 : time[2]%60),
minutes = time[1];
} 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;
}
} else {