pokemon-showdown/chat-plugins/tcgtabletop.js
Bär Halberkamp 4ca5c1d2c8 Remove /mtg and allow /ygo in all rooms
The only wiki we could use sucks, and I'd rather only have a good command usable everywhere
2016-03-16 02:10:23 +01:00

64 lines
2.4 KiB
JavaScript

/**
* TCG & Tabletop: 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.
*/
'use strict';
const http = require('http');
function noop() {}
function wikiaSearch(subdomain, query, callback) {
http.get('http://' + subdomain + '.wikia.com/api/v1/Search/List/?query=' + encodeURIComponent(query) + '&limit=1', res => {
let buffer = '';
res.setEncoding('utf8');
res.on('data', data => {
buffer += data;
});
res.on('end', () => {
let result;
try {
result = JSON.parse(buffer);
} catch (e) {
return callback(e);
}
if (!result) return callback(new Error("Malformed data"));
if (result.exception) return callback(new Error(Tools.getString(result.exception.message) || "Not found"));
if (!Array.isArray(result.items) || !result.items[0] || typeof result.items[0] !== 'object') return callback(new Error("Malformed data"));
return callback(null, result.items[0]);
});
}).once('error', function (err) {
callback(err);
this.on('error', noop);
});
}
exports.commands = {
ygo: 'yugioh',
yugioh: function (target, room, user, connection) {
if (!this.canBroadcast()) return;
let broadcasting = this.broadcasting;
let subdomain = 'yugioh';
let query = target.trim();
wikiaSearch(subdomain, query, (err, data) => {
if (err) {
if (err instanceof SyntaxError || err.message === 'Malformed data') {
if (!broadcasting) return connection.sendTo(room, "Error: something went wrong in the request: " + err.message);
return room.add("Error: Something went wrong in the request: " + err.message).update();
}
if (!broadcasting) return connection.sendTo(room, "Error: " + err.message);
return room.add("Error: " + err.message).update();
}
let entryUrl = Tools.getString(data.url);
let entryTitle = Tools.getString(data.title);
let htmlReply = "<strong>Best result for " + Tools.escapeHTML(query) + ":</strong><br/><a href=\"" + Tools.escapeHTML(entryUrl) + "\">" + Tools.escapeHTML(entryTitle) + "</a>";
if (!broadcasting) return connection.sendTo(room, "|raw|<div class=\"infobox\">" + htmlReply + "</div>");
room.addRaw("<div class=\"infobox\">" + htmlReply + "</div>").update();
});
},
};