diff --git a/README.md b/README.md index dcd3769a..02d670ec 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 (323) +## Commands (324) ### Utility: * **eval:** Executes JavaScript code. @@ -149,6 +149,7 @@ on the [home server](https://discord.gg/sbMe32W). * **kickstarter:** Searches Kickstarter for your query. * **kitsu-anime:** Searches Kitsu.io for your query, getting anime results. * **kitsu-manga:** Searches Kitsu.io for your query, getting manga results. +* **know-your-meme:** Searches Know Your Meme for your query. * **league-of-legends-champion:** Responds with information on a League of Legends champion. * **mdn:** Searches MDN for your query. * **nasa:** Searches NASA's image archive for your query. diff --git a/commands/search/know-your-meme.js b/commands/search/know-your-meme.js new file mode 100644 index 00000000..abc995ed --- /dev/null +++ b/commands/search/know-your-meme.js @@ -0,0 +1,64 @@ +const Command = require('../../structures/Command'); +const request = require('node-superfetch'); +const cheerio = require('cheerio'); +const { MessageEmbed } = require('discord.js'); +const { shorten } = require('../../util/Util'); + +module.exports = class KnowYourMemeCommand extends Command { + constructor(client) { + super(client, { + name: 'know-your-meme', + aliases: ['kym', 'meme-info', 'meme-search'], + group: 'search', + memberName: 'know-your-meme', + description: 'Searches Know Your Meme for your query.', + clientPermissions: ['EMBED_LINKS'], + args: [ + { + key: 'query', + prompt: 'What meme would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + try { + const location = await this.search(query); + if (!location) return msg.say('Could not find any results.'); + const data = await this.fetchMeme(location); + const embed = new MessageEmbed() + .setColor(0x12133F) + .setAuthor('Know Your Meme', 'https://i.imgur.com/WvcH4Z2.png', 'https://knowyourmeme.com/') + .setTitle(data.name) + .setDescription(shorten(data.description || 'No description available.')) + .setURL(data.url) + .setThumbnail(data.thumbnail); + 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://knowyourmeme.com/search') + .query({ q: query }); + const $ = cheerio.load(text); + const location = $('.entry-grid-body').find('tr td a').first().attr('href'); + if (!location) return null; + return location; + } + + async fetchMeme(location) { + const { text } = await request.get(`https://knowyourmeme.com${location}`); + const $ = cheerio.load(text); + return { + name: $('h1').first().text().trim(), + url: `https://knowyourmeme.com${location}`, + description: $('.bodycopy').children().eq(1).text(), + thumbnail: $('a[class="photo left wide"]').first().attr('href') || null + }; + } +}; diff --git a/commands/search/vndb.js b/commands/search/vndb.js index eceb899e..b9757e19 100644 --- a/commands/search/vndb.js +++ b/commands/search/vndb.js @@ -24,8 +24,9 @@ module.exports = class VNDBCommand extends Command { async run(msg, { query }) { try { - const data = await this.fetchVN(query); - if (!data) return msg.say('Could not find any results.'); + const id = await this.search(query); + if (!id) return msg.say('Could not find any results.'); + const data = await this.fetchVN(id); const embed = new MessageEmbed() .setColor(0x000407) .setAuthor('VNDB', 'https://i.imgur.com/BIxjIby.png', 'https://vndb.org/') @@ -40,32 +41,37 @@ module.exports = class VNDBCommand extends Command { } } - async fetchVN(query) { + async search(query) { const { text } = await request .get('https://vndb.org/v/all') .query({ q: query }); const id = text.match(/Description<\/h2>

(.+)<\/p><\/td>/)[1] + return id[1]; + } + + async fetchVN(id) { + const { text } = await request.get(`https://vndb.org/v${id}`); + const devID = text.match(/Description<\/h2>

(.+)<\/p><\/td>/)[1] .replace(/
/g, '\n') .replace(/
(.+)<\/a>/g, '[$2]($1)'); return { id: id[1], - url, - title: detailsText.match(/(.+)<\/title>/)[1], - developer: { - name: devText.match(/<title>(.+)<\/title>/)[1], - url: devURL - }, + url: `https://vndb.org/v${id}`, + title: text.match(/<title>(.+)<\/title>/)[1], + developer, description: description === '-' ? null : description, - image: detailsText.match(/https:\/\/s.vndb.org\/cv\/[0-9]+\/[0-9]+\.jpg/)[0] + image: text.match(/https:\/\/s.vndb.org\/cv\/[0-9]+\/[0-9]+\.jpg/)[0] + }; + } + + async fetchDeveloper(id) { + const { text } = await request.get(`https://vndb.org/p${id}`); + return { + name: text.match(/<title>(.+)<\/title>/)[1], + url: `https://vndb.org/p${id}` }; } }; diff --git a/package.json b/package.json index 1e1cf190..afae55b9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "92.1.7", + "version": "92.2.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {