Know Your Meme Command, improve vndb code

This commit is contained in:
Daniel Odendahl Jr
2018-10-04 21:17:22 +00:00
parent 8c6d3fee08
commit e7416e8816
4 changed files with 91 additions and 20 deletions
+2 -1
View File
@@ -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.
+64
View File
@@ -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
};
}
};
+24 -18
View File
@@ -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(/<a href="\/v([0-9]+)" title=/);
if (!id) return null;
const url = `https://vndb.org/v${id[1]}`;
const details = await request.get(url);
const detailsText = details.text;
const dev = detailsText.match(/<a href="\/p([0-9]+)"/);
const devURL = `https://vndb.org/p${dev[1]}`;
const devReq = await request.get(devURL);
const devText = devReq.text;
const description = detailsText.match(/<h2>Description<\/h2><p>(.+)<\/p><\/td>/)[1]
return id[1];
}
async fetchVN(id) {
const { text } = await request.get(`https://vndb.org/v${id}`);
const devID = text.match(/<a href="\/p([0-9]+)"/);
const developer = await this.fetchDeveloper(devID);
const description = text.match(/<h2>Description<\/h2><p>(.+)<\/p><\/td>/)[1]
.replace(/<br>/g, '\n')
.replace(/<a href="(.+)" rel="nofollow">(.+)<\/a>/g, '[$2]($1)');
return {
id: id[1],
url,
title: detailsText.match(/<title>(.+)<\/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}`
};
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "92.1.7",
"version": "92.2.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {