const Command = require('../../structures/Command'); const request = require('node-superfetch'); const { MessageEmbed } = require('discord.js'); const { shorten } = require('../../util/Util'); module.exports = class VNDBCommand extends Command { constructor(client) { super(client, { name: 'vndb', aliases: ['visual-novel', 'vn'], group: 'search', memberName: 'vndb', description: 'Responds with information on a Visual Novel.', clientPermissions: ['EMBED_LINKS'], args: [ { key: 'query', prompt: 'What visual novel would you like to get information on?', type: 'string' } ] }); } async run(msg, { query }) { try { 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/') .setTitle(data.title) .setDescription(shorten(data.description || 'No description available.')) .setURL(data.url) .setThumbnail(data.image) .addField('❯ Developer', `[${data.developer.name}](${data.developer.url})`); 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://vndb.org/v/all') .query({ q: query }); const id = text.match(/Description<\/h2>

(.+)<\/p><\/td>/)[1] .replace(/
/g, '\n') .replace(/
(.+)<\/a>/g, '[$2]($1)'); return { id: id[1], url: `https://vndb.org/v${id}`, title: text.match(/(.+)<\/title>/)[1], developer, description: description === '-' ? null : description, 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}` }; } };