3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-24 11:42:36 +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({
url: this.StatusAPI[service],
qs: {"id": id},
@ -41,7 +41,7 @@ var dent = function(dbot) {
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
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) {
dbot.api.link.addHandler(s, this.StatusRegex[s], function(event, matches, name) {
this.lookup(event, matches[1], name);
dbot.api.link.addHandler(s, this.StatusRegex[s], function(matches, name, callback) {
this.lookup(matches[1], name, callback);
}.bind(this));
}
}.bind(this);

View File

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

View File

@ -57,6 +57,27 @@ var link = function(dbot) {
} 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) {
this.links[event.channel.name] = urlMatches[0];
if(this.config.autoTitle == true) {
var handlerFound = false;
for(var i=0;i<this.handlers.length;i++) {
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 } ));
this.api.parseLink(urlMatches[0], function(result) {
event.reply(result);
});
}
}
}
}.bind(this);
this.on = 'PRIVMSG';
};

View File

@ -57,7 +57,7 @@ var reddit = function(dbot) {
};
this.onLoad = function() {
var rHandler = function(event, matches, name) {
var rHandler = function(matches, name, callback) {
if(matches[6]) { // It's a comment
this.api.getCommentInfo(matches[4], matches[6], function(info) {
if(info) {
@ -70,7 +70,7 @@ var reddit = function(dbot) {
'down': info.downs
});
if(info.over_18) infoString += " " + dbot.t("nsfw");
event.reply(infoString);
callback(infoString);
}
});
} else if(matches[4]) { // It's a post
@ -86,7 +86,7 @@ var reddit = function(dbot) {
'url': this.ApiRoot + matches[4]
});
if(info.over_18) infoString += " " + dbot.t("nsfw");
event.reply(infoString);
callback(infoString);
}
}.bind(this));
} else if(matches[2]) { // It's a subreddit
@ -98,7 +98,7 @@ var reddit = function(dbot) {
'active': info.accounts_active
});
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() {
dbot.api.link.addHandler(this.name,
/https?:\/\/(www\.)?soundcloud\.com\//,
function(event, match, name) {
function(match, name, callback) {
var url = match.input;
request.get(this.ApiRoot + '/resolve.json', {
'qs': {
@ -51,7 +51,7 @@ var soundcloud = function(dbot) {
if(response.statusCode == 200) {
if(body.kind == 'track') {
if(!body.genre) body.genre = '';
event.reply(dbot.t('sc_track', {
callback(dbot.t('sc_track', {
'title': body.title,
'artist': body.user.username,
'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.spotifyText = '\u00039spotify\u000f';
this.lookup = function(event, link) {
this.lookup = function(link, callback) {
request({
'url': this.spotifyLookup,
'qs': { 'uri': link },
@ -28,19 +28,19 @@ var spotify = function(dbot) {
}, function(error, response, body) {
if(!error && response.statusCode == 200) {
if(_.has(body, 'track')) {
event.reply(dbot.t('track', {
callback(dbot.t('track', {
'artist': _.map(body.track.artists,
function(a) { return a.name }).join(', '),
'album': body.track.album.name,
'track': body.track.name
}));
} else if(_.has(body, 'album')) {
event.reply(dbot.t('album', {
callback(dbot.t('album', {
'artist': body.album.artist,
'album': body.album.name
}));
} else if(_.has(body, 'artist')) {
event.reply(dbot.t('artist', {
callback(dbot.t('artist', {
'artist': body.artist.name
}));
}
@ -119,8 +119,8 @@ var spotify = function(dbot) {
this.commands = commands;
this.onLoad = function() {
dbot.api.link.addHandler(this.name, this.spotifyRegex, function(event, matches, name) {
this.lookup(event, matches[0]);
dbot.api.link.addHandler(this.name, this.spotifyRegex, function(matches, name, callback) {
this.lookup(matches[0], callback);
}.bind(this));
}.bind(this);
};

View File

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