3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-25 04:02:39 +01:00

link handlers callback result instead of event.reply it[Close #536]

This commit is contained in:
reality 2013-07-25 21:56:44 +00:00
parent ac2d010a57
commit 4ae10a2b26
7 changed files with 44 additions and 33 deletions

View File

@ -33,7 +33,7 @@ var dent = function(dbot) {
} }
}; };
this.lookup = function(event, id, service) { this.lookup = function(id, service, callback) {
request({ request({
url: this.StatusAPI[service], url: this.StatusAPI[service],
qs: {"id": id}, qs: {"id": id},
@ -41,7 +41,7 @@ var dent = function(dbot) {
}, function(error, response, body) { }, function(error, response, body) {
if (!error && response.statusCode == 200) { if (!error && response.statusCode == 200) {
if (_.has(body, 'text')) { if (_.has(body, 'text')) {
event.reply(service + " [" + body.user.screen_name + '] ' + body.text); callback(service + " [" + body.user.screen_name + '] ' + body.text);
} }
} }
}); });
@ -65,8 +65,8 @@ var dent = function(dbot) {
} }
for(s in this.StatusRegex) { for(s in this.StatusRegex) {
dbot.api.link.addHandler(s, this.StatusRegex[s], function(event, matches, name) { dbot.api.link.addHandler(s, this.StatusRegex[s], function(matches, name, callback) {
this.lookup(event, matches[1], name); this.lookup(matches[1], name, callback);
}.bind(this)); }.bind(this));
} }
}.bind(this); }.bind(this);

View File

@ -163,7 +163,7 @@ var imgur = function(dbot) {
} }
this.onLoad = function() { this.onLoad = function() {
var imgurHandler = function(event, matches, name) { var imgurHandler = function(matches, name, callback) {
if(matches[1]) { if(matches[1]) {
var dataCallback = function(data) { var dataCallback = function(data) {
var info; var info;
@ -175,7 +175,7 @@ var imgur = function(dbot) {
info = this.internalAPI.galleryInfoString(data); info = this.internalAPI.galleryInfoString(data);
} }
if(info) event.reply(dbot.t('imgurinfo', { 'info': info })); if(info) callback(dbot.t('imgurinfo', { 'info': info }));
}.bind(this); }.bind(this);
if(name == 'imgurimage') { if(name == 'imgurimage') {

View File

@ -57,6 +57,27 @@ var link = function(dbot) {
} catch(err) { callback(false); } } catch(err) { callback(false); }
}); });
},
'parseLink': function(link, callback) {
var handler = false;
for(var i=0;i<this.handlers.length;i++) {
var matches = this.handlers[i].regex.exec(link);
if(matches) {
handler = this.handlers[i];
break;
}
}
if(handler) {
this.handlers[i].callback(matches, this.handlers[i].name, function(parsed) {
callback(parsed);
});
} else {
this.api.getTitle(link, function(title) {
callback(dbot.t('link', { 'link': title } ));
});
}
} }
}; };
@ -127,21 +148,11 @@ var link = function(dbot) {
if(urlMatches !== null) { if(urlMatches !== null) {
this.links[event.channel.name] = urlMatches[0]; this.links[event.channel.name] = urlMatches[0];
if(this.config.autoTitle == true) { if(this.config.autoTitle == true) {
var handlerFound = false; this.api.parseLink(urlMatches[0], function(result) {
for(var i=0;i<this.handlers.length;i++) { event.reply(result);
var matches = this.handlers[i].regex.exec(urlMatches[0]);
if(matches) {
this.handlers[i].callback(event, matches, this.handlers[i].name);
handlerFound = true; break;
}
}
if(!handlerFound) {
this.api.getTitle(urlMatches[0], function(title) {
event.reply(dbot.t('link', { 'link': title } ));
}); });
} }
} }
}
}.bind(this); }.bind(this);
this.on = 'PRIVMSG'; this.on = 'PRIVMSG';
}; };

View File

@ -57,7 +57,7 @@ var reddit = function(dbot) {
}; };
this.onLoad = function() { this.onLoad = function() {
var rHandler = function(event, matches, name) { var rHandler = function(matches, name, callback) {
if(matches[6]) { // It's a comment if(matches[6]) { // It's a comment
this.api.getCommentInfo(matches[4], matches[6], function(info) { this.api.getCommentInfo(matches[4], matches[6], function(info) {
if(info) { if(info) {
@ -70,7 +70,7 @@ var reddit = function(dbot) {
'down': info.downs 'down': info.downs
}); });
if(info.over_18) infoString += " " + dbot.t("nsfw"); if(info.over_18) infoString += " " + dbot.t("nsfw");
event.reply(infoString); callback(infoString);
} }
}); });
} else if(matches[4]) { // It's a post } else if(matches[4]) { // It's a post
@ -86,7 +86,7 @@ var reddit = function(dbot) {
'url': this.ApiRoot + matches[4] 'url': this.ApiRoot + matches[4]
}); });
if(info.over_18) infoString += " " + dbot.t("nsfw"); if(info.over_18) infoString += " " + dbot.t("nsfw");
event.reply(infoString); callback(infoString);
} }
}.bind(this)); }.bind(this));
} else if(matches[2]) { // It's a subreddit } else if(matches[2]) { // It's a subreddit
@ -98,7 +98,7 @@ var reddit = function(dbot) {
'active': info.accounts_active 'active': info.accounts_active
}); });
if(info.over18) infoString += dbot.t("nsfw"); if(info.over18) infoString += dbot.t("nsfw");
event.reply(infoString); callback(infoString);
} }
}); });
} }

View File

@ -39,7 +39,7 @@ var soundcloud = function(dbot) {
this.onLoad = function() { this.onLoad = function() {
dbot.api.link.addHandler(this.name, dbot.api.link.addHandler(this.name,
/https?:\/\/(www\.)?soundcloud\.com\//, /https?:\/\/(www\.)?soundcloud\.com\//,
function(event, match, name) { function(match, name, callback) {
var url = match.input; var url = match.input;
request.get(this.ApiRoot + '/resolve.json', { request.get(this.ApiRoot + '/resolve.json', {
'qs': { 'qs': {
@ -51,7 +51,7 @@ var soundcloud = function(dbot) {
if(response.statusCode == 200) { if(response.statusCode == 200) {
if(body.kind == 'track') { if(body.kind == 'track') {
if(!body.genre) body.genre = ''; if(!body.genre) body.genre = '';
event.reply(dbot.t('sc_track', { callback(dbot.t('sc_track', {
'title': body.title, 'title': body.title,
'artist': body.user.username, 'artist': body.user.username,
'genre': body.genre.trim(), 'genre': body.genre.trim(),

View File

@ -20,7 +20,7 @@ var spotify = function(dbot) {
this.youtubeRegex = /^http:\/\/(?:www\.)?youtube.com\/watch\?v=\w+(&\S*)?$/ this.youtubeRegex = /^http:\/\/(?:www\.)?youtube.com\/watch\?v=\w+(&\S*)?$/
this.spotifyText = '\u00039spotify\u000f'; this.spotifyText = '\u00039spotify\u000f';
this.lookup = function(event, link) { this.lookup = function(link, callback) {
request({ request({
'url': this.spotifyLookup, 'url': this.spotifyLookup,
'qs': { 'uri': link }, 'qs': { 'uri': link },
@ -28,19 +28,19 @@ var spotify = function(dbot) {
}, function(error, response, body) { }, function(error, response, body) {
if(!error && response.statusCode == 200) { if(!error && response.statusCode == 200) {
if(_.has(body, 'track')) { if(_.has(body, 'track')) {
event.reply(dbot.t('track', { callback(dbot.t('track', {
'artist': _.map(body.track.artists, 'artist': _.map(body.track.artists,
function(a) { return a.name }).join(', '), function(a) { return a.name }).join(', '),
'album': body.track.album.name, 'album': body.track.album.name,
'track': body.track.name 'track': body.track.name
})); }));
} else if(_.has(body, 'album')) { } else if(_.has(body, 'album')) {
event.reply(dbot.t('album', { callback(dbot.t('album', {
'artist': body.album.artist, 'artist': body.album.artist,
'album': body.album.name 'album': body.album.name
})); }));
} else if(_.has(body, 'artist')) { } else if(_.has(body, 'artist')) {
event.reply(dbot.t('artist', { callback(dbot.t('artist', {
'artist': body.artist.name 'artist': body.artist.name
})); }));
} }
@ -119,8 +119,8 @@ var spotify = function(dbot) {
this.commands = commands; this.commands = commands;
this.onLoad = function() { this.onLoad = function() {
dbot.api.link.addHandler(this.name, this.spotifyRegex, function(event, matches, name) { dbot.api.link.addHandler(this.name, this.spotifyRegex, function(matches, name, callback) {
this.lookup(event, matches[0]); this.lookup(matches[0], callback);
}.bind(this)); }.bind(this));
}.bind(this); }.bind(this);
}; };

View File

@ -52,7 +52,7 @@ var youtube = function(dbot) {
this.onLoad = function() { this.onLoad = function() {
dbot.api.link.addHandler(this.name, this.LinkRegex, dbot.api.link.addHandler(this.name, this.LinkRegex,
function(event, match, name) { function(match, name, callback) {
request.get(this.ApiRoot + '/videos/' + match[2], { request.get(this.ApiRoot + '/videos/' + match[2], {
'qs': this.params, 'qs': this.params,
'json': true 'json': true
@ -65,7 +65,7 @@ var youtube = function(dbot) {
'numDislikes': 0 'numDislikes': 0
}; };
} }
event.reply(dbot.t('yt_video', { callback(dbot.t('yt_video', {
'title': v.title['$t'], 'title': v.title['$t'],
'plays': v['yt$statistics'].viewCount, 'plays': v['yt$statistics'].viewCount,
'author': v.author[0].name['$t'], 'author': v.author[0].name['$t'],