pokemon-showdown/chat-plugins/tcg.js
Rhythms f4ca4b8a8b TCG: fix spacing
wasn't really needed, but it's 'more correct.'
2015-04-04 11:58:45 -06:00

50 lines
1.5 KiB
JavaScript

/**
* TCG: Yugioh wiki plugin
* This is a command that allows users to search the yugioh wiki for cards. It will display the closest match with a given query, or a separate message if there isn't anything found.
* By bumbadadabum with help from ascriptmaster, codelegend and the PS development team.
*/
var http = require("http");
var tcgsearch = function (target, room, user, cmd, self) {
if (room.id !== 'tcg') return self.sendReply("This command can only be used in the TCG room.");
if (!self.canBroadcast()) return;
var query = Tools.escapeHTML(target);
var host = (cmd === 'yugioh' || cmd === 'ygo') ? 'yugioh.wikia.com' : 'mtg.wikia.com';
var Search = http.get('http://' + host + '/api/v1/Search/List/?query=' + query + '&limit=1', function (a) {
var response = '';
a.on('data', function (data) {
response += data;
});
a.on('end', function () {
var result;
try {
result = JSON.parse(response);
} catch (e) {
return self.sendReply("ERROR: Could not parse query:" + e);
}
if (result.exception) {
self.sendReply("No articles matching your query were found.");
} else {
self.sendReplyBox("<strong>Best result for " + query + ":</strong><br/><a href=\"" + Tools.escapeHTML(result.items[0].url) + "\">" + Tools.escapeHTML(result.items[0].title) + "</a>");
}
room.update();
});
});
};
exports.commands = {
ygo: 'yugioh',
mtg: 'yugioh',
magic: 'yugioh',
yugioh: function (target, room, user, connection, cmd) {
tcgsearch(target, room, user, cmd, this);
}
};