From e5a5038e98dc2927fed56ac196ff2f67d790978a Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Fri, 5 Oct 2018 22:54:57 +0000 Subject: [PATCH] ESRB Command is back! --- README.md | 3 +- commands/search/esrb.js | 70 +++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 commands/search/esrb.js diff --git a/README.md b/README.md index 02d670ec..b43d3311 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Xiao is a Discord bot coded in JavaScript with The bot is no longer available for invite. You can self-host the bot, or use her on the [home server](https://discord.gg/sbMe32W). -## Commands (324) +## Commands (325) ### Utility: * **eval:** Executes JavaScript code. @@ -131,6 +131,7 @@ on the [home server](https://discord.gg/sbMe32W). * **dictionary:** Defines a word. * **discord-js-docs:** Searches the Discord.js docs for your query. * **eshop:** Searches the Nintendo eShop for your query. +* **esrb:** Searches ESRB for your query. * **flickr:** Searches Flickr for your query. * **forecast:** Responds with the seven-day forecast for a specific location. * **giphy:** Searches Giphy for your query. diff --git a/commands/search/esrb.js b/commands/search/esrb.js new file mode 100644 index 00000000..96ea208a --- /dev/null +++ b/commands/search/esrb.js @@ -0,0 +1,70 @@ +const Command = require('../../structures/Command'); +const request = require('node-superfetch'); +const cheerio = require('cheerio'); +const { MessageEmbed } = require('discord.js'); +const ratings = { + EC: 'Early Childhood', + E: 'Everyone', + E10plus: 'Everyone 10+', + T: 'Teen', + M: 'Mature', + AO: 'Adults Only' +}; + +module.exports = class ESRBCommand extends Command { + constructor(client) { + super(client, { + name: 'esrb', + aliases: ['esrb-rating'], + group: 'search', + memberName: 'esrb', + description: 'Searches ESRB for your query.', + clientPermissions: ['EMBED_LINKS'], + args: [ + { + key: 'query', + prompt: 'What game would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + try { + const data = await this.search(query); + if (!data) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0x231F20) + .setAuthor('ESRB', 'https://i.imgur.com/dV2BamF.jpg', 'https://www.esrb.org/') + .setTitle(data.title) + .setDescription(data.summary || 'No summary available.') + .setThumbnail(data.image) + .addField('❯ Rating', ratings[data.rating], true) + .addField('❯ Content Descriptors', data.descriptors.length ? data.descriptors.join('\n') : 'None', true); + return msg.embed(embed); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async search(query) { + const { text } = await request + .get('https://www.esrb.org/ratings/search.aspx') + .query({ + from: 'home', + titleOrPublisher: query + }); + const $ = cheerio.load(text); + const result = $('table').first().children().eq(1).children(); + if (!result.length) return null; + const image = result.find('td[data-title="Ratings"]').first().find('img').attr('src'); + return { + title: result.find('td[data-title="Title"]').first().text().trim(), + rating: image.match(/(EC|E|E10plus|T|M|AO)\.png/i)[1], + descriptors: result.find('td[data-title="Content Descriptors"]').first().text().trim().split(', '), + summary: result.find('td[style="border-width: 0 3px 0 0; padding: 10px;"]').first().text().trim() || null, + image + }; + } +}; diff --git a/package.json b/package.json index 7f454905..b03e5a76 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "92.2.6", + "version": "92.3.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {