{
"version": 3,
"sources": ["../../../../server/chat-plugins/tcgtabletop.ts"],
"sourcesContent": ["/**\r\n* TCG & Tabletop: Yugioh wiki plugin\r\n* This is a command that allows users to search the yugioh wiki for cards.\r\n* It will display the closest match with a given query, or a separate message if there isn't anything found.\r\n* By bumbadadabum with help from ascriptmaster, codelegend and the PS development team.\r\n*/\r\n\r\n\r\nimport {Net, Utils} from '../../lib';\r\nconst SEARCH_PATH = '/api/v1/Search/List/';\r\nconst DETAILS_PATH = '/api/v1/Articles/Details/';\r\n\r\nasync function getFandom(site: string, pathName: string, search: AnyObject) {\r\n\tconst body = await Net(`https://${site}.fandom.com/${pathName}`).get({query: search});\r\n\tconst json = JSON.parse(body);\r\n\tif (!json) throw new Error(`Malformed data`);\r\n\tif (json.exception) throw new Error(Utils.getString(json.exception.message) || `Not found`);\r\n\treturn json;\r\n}\r\n\r\nasync function searchFandom(site: string, query: string) {\r\n\tconst result = await getFandom(site, SEARCH_PATH, {query, limit: 1});\r\n\tif (!Array.isArray(result.items) || !result.items.length) throw new Error(`Malformed data`);\r\n\tif (!result.items[0] || typeof result.items[0] !== 'object') throw new Error(`Malformed data`);\r\n\treturn result.items[0];\r\n}\r\n\r\nasync function getCardDetails(site: string, id: string) {\r\n\tconst specifications = {\r\n\t\tids: id,\r\n\t\tabstract: 0,\r\n\t\twidth: 80,\r\n\t\theight: 115,\r\n\t};\r\n\r\n\tconst result = await getFandom(site, DETAILS_PATH, specifications);\r\n\tif (typeof result.items !== 'object' || !result.items[id] || typeof result.items[id] !== 'object') {\r\n\t\tthrow new Error(`Malformed data`);\r\n\t}\r\n\treturn result.items[id];\r\n}\r\n\r\nexport const commands: Chat.ChatCommands = {\r\n\tygo: 'yugioh',\r\n\tyugioh(target, room, user) {\r\n\t\tthis.checkBroadcast();\r\n\t\troom = this.requireRoom('tcgtabletop' as RoomID);\r\n\t\tconst subdomain = 'yugioh';\r\n\t\tconst query = target.trim();\r\n\t\tif (!query) return this.parse('/help yugioh');\r\n\r\n\t\treturn searchFandom(subdomain, query).then((data: {url: unknown, title: unknown, id: unknown}) => {\r\n\t\t\tif (!this.runBroadcast()) return;\r\n\t\t\tconst entryUrl = Utils.getString(data.url);\r\n\t\t\tconst entryTitle = Utils.getString(data.title);\r\n\t\t\tconst id = Utils.getString(data.id);\r\n\t\t\tlet htmlReply = Utils.html`Best result for ${query}:
${entryTitle}`;\r\n\t\t\tif (id) {\r\n\t\t\t\tgetCardDetails(subdomain, id).then((card: {thumbnail: unknown}) => {\r\n\t\t\t\t\tif (!room) return; // do nothing if the room doesn't exist anymore\r\n\t\t\t\t\tconst thumb = Utils.getString(card.thumbnail);\r\n\t\t\t\t\tif (thumb) {\r\n\t\t\t\t\t\thtmlReply = `
| ${htmlReply} |