From 6ecba82a7935a309dd518347ac55925868a6d46a Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Sat, 28 Oct 2017 14:55:24 +0000 Subject: [PATCH] Change Kitsu to MAL --- commands/search/kitsu-anime.js | 53 ------------------------- commands/search/kitsu-manga.js | 53 ------------------------- commands/search/my-anime-list-anime.js | 55 ++++++++++++++++++++++++++ commands/search/my-anime-list-manga.js | 55 ++++++++++++++++++++++++++ package.json | 2 +- util/Util.js | 13 ++++++ 6 files changed, 124 insertions(+), 107 deletions(-) delete mode 100644 commands/search/kitsu-anime.js delete mode 100644 commands/search/kitsu-manga.js create mode 100644 commands/search/my-anime-list-anime.js create mode 100644 commands/search/my-anime-list-manga.js diff --git a/commands/search/kitsu-anime.js b/commands/search/kitsu-anime.js deleted file mode 100644 index 565e4f2d..00000000 --- a/commands/search/kitsu-anime.js +++ /dev/null @@ -1,53 +0,0 @@ -const { Command } = require('discord.js-commando'); -const { MessageEmbed } = require('discord.js'); -const snekfetch = require('snekfetch'); -const { shorten } = require('../../util/Util'); - -module.exports = class KitsuAnimeCommand extends Command { - constructor(client) { - super(client, { - name: 'kitsu-anime', - aliases: ['my-anime-list-anime', 'mal-anime', 'anime'], - group: 'search', - memberName: 'kitsu-anime', - description: 'Searches Kitsu.io for your query, getting anime results.', - clientPermissions: ['EMBED_LINKS'], - args: [ - { - key: 'query', - prompt: 'What anime would you like to search for?', - type: 'string' - } - ] - }); - } - - async run(msg, { query }) { - try { - const { text } = await snekfetch - .get('https://kitsu.io/api/edge/anime') - .query({ 'filter[text]': query }); - const body = JSON.parse(text); - if (!body.data.length) return msg.say('Could not find any results.'); - const data = body.data[0].attributes; - const embed = new MessageEmbed() - .setColor(0xF75239) - .setAuthor('Kitsu.io', 'https://i.imgur.com/y7nDpqR.png') - .setURL(`https://kitsu.io/anime/${data.slug}`) - .setThumbnail(data.posterImage ? data.posterImage.original : null) - .setTitle(data.canonicalTitle) - .setDescription(shorten(data.synopsis)) - .addField('❯ Type', - `${data.showType} - ${data.status}`, true) - .addField('❯ Episodes', - data.episodeCount || '???', true) - .addField('❯ Start Date', - data.startDate ? new Date(data.startDate).toDateString() : '???', true) - .addField('❯ End Date', - data.endDate ? new Date(data.endDate).toDateString() : '???', true); - return msg.embed(embed); - } catch (err) { - return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); - } - } -}; diff --git a/commands/search/kitsu-manga.js b/commands/search/kitsu-manga.js deleted file mode 100644 index 095d9cd4..00000000 --- a/commands/search/kitsu-manga.js +++ /dev/null @@ -1,53 +0,0 @@ -const { Command } = require('discord.js-commando'); -const { MessageEmbed } = require('discord.js'); -const snekfetch = require('snekfetch'); -const { shorten } = require('../../util/Util'); - -module.exports = class KitsuMangaCommand extends Command { - constructor(client) { - super(client, { - name: 'kitsu-manga', - aliases: ['my-anime-list-manga', 'mal-manga', 'manga'], - group: 'search', - memberName: 'kitsu-manga', - description: 'Searches Kitsu.io for your query, getting manga results.', - clientPermissions: ['EMBED_LINKS'], - args: [ - { - key: 'query', - prompt: 'What manga would you like to search for?', - type: 'string' - } - ] - }); - } - - async run(msg, { query }) { - try { - const { text } = await snekfetch - .get('https://kitsu.io/api/edge/manga') - .query({ 'filter[text]': query }); - const body = JSON.parse(text); - if (!body.data.length) return msg.say('Could not find any results.'); - const data = body.data[0].attributes; - const embed = new MessageEmbed() - .setColor(0xF75239) - .setAuthor('Kitsu.io', 'https://i.imgur.com/y7nDpqR.png') - .setURL(`https://kitsu.io/manga/${data.slug}`) - .setThumbnail(data.posterImage ? data.posterImage.original : null) - .setTitle(data.canonicalTitle) - .setDescription(shorten(data.synopsis)) - .addField('❯ Type', - `${data.subtype} - ${data.status}`, true) - .addField('❯ Volumes / Chapters', - `${data.volumeCount || '???'} / ${data.chapterCount || '???'}`, true) - .addField('❯ Start Date', - data.startDate ? new Date(data.startDate).toDateString() : '???', true) - .addField('❯ End Date', - data.endDate ? new Date(data.endDate).toDateString() : '???', true); - return msg.embed(embed); - } catch (err) { - return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); - } - } -}; diff --git a/commands/search/my-anime-list-anime.js b/commands/search/my-anime-list-anime.js new file mode 100644 index 00000000..9c6d9750 --- /dev/null +++ b/commands/search/my-anime-list-anime.js @@ -0,0 +1,55 @@ +const { Command } = require('discord.js-commando'); +const { MessageEmbed } = require('discord.js'); +const snekfetch = require('snekfetch'); +const { xml2js } = require('xml-js'); +const { shorten, cleanXML } = require('../../util/Util'); +const { MAL_LOGIN } = process.env; + +module.exports = class MyAnimeListAnimeCommand extends Command { + constructor(client) { + super(client, { + name: 'my-anime-list-anime', + aliases: ['mal-anime', 'anime'], + group: 'search', + memberName: 'my-anime-list-anime', + description: 'Searches My Anime List for your query, getting anime results.', + clientPermissions: ['EMBED_LINKS'], + args: [ + { + key: 'query', + prompt: 'What anime would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + try { + const { text } = await snekfetch + .get(`https://${MAL_LOGIN}@myanimelist.net/api/anime/search.xml`) + .query({ q: query }); + const body = xml2js(text, { compact: true }).anime; + const data = body.length ? body.entry[0] : body.entry; + const embed = new MessageEmbed() + .setColor(0x2D54A2) + .setAuthor('My Anime List', 'https://i.imgur.com/5rivpMM.png') + .setURL(`https://myanimelist.net/anime/${data.id._text}`) + .setThumbnail(data.image._text) + .setTitle(data.title._text) + .setDescription(shorten(cleanXML(data.synopsis._text))) + .addField('❯ Type', + `${data.type._text} - ${data.status._text}`, true) + .addField('❯ Episodes', + data.episodes._text, true) + .addField('❯ Start Date', + data.start_date._text !== '0000-00-00' ? data.start_date._text : '???', true) + .addField('❯ End Date', + data.end_date._text !== '0000-00-00' ? data.end_date._text : '???', true); + return msg.embed(embed); + } catch (err) { + if (err.message === 'Parse Error') return msg.say('Could not find any results.'); + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/commands/search/my-anime-list-manga.js b/commands/search/my-anime-list-manga.js new file mode 100644 index 00000000..c0af0a11 --- /dev/null +++ b/commands/search/my-anime-list-manga.js @@ -0,0 +1,55 @@ +const { Command } = require('discord.js-commando'); +const { MessageEmbed } = require('discord.js'); +const snekfetch = require('snekfetch'); +const { xml2js } = require('xml-js'); +const { shorten, cleanXML } = require('../../util/Util'); +const { MAL_LOGIN } = process.env; + +module.exports = class MyAnimeListMangaCommand extends Command { + constructor(client) { + super(client, { + name: 'my-anime-list-manga', + aliases: ['mal-manga', 'manga'], + group: 'search', + memberName: 'my-anime-list-manga', + description: 'Searches My Anime List for your query, getting manga results.', + clientPermissions: ['EMBED_LINKS'], + args: [ + { + key: 'query', + prompt: 'What manga would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + try { + const { text } = await snekfetch + .get(`https://${MAL_LOGIN}@myanimelist.net/api/manga/search.xml`) + .query({ q: query }); + const body = xml2js(text, { compact: true }).manga; + const data = body.length ? body.entry[0] : body.entry; + const embed = new MessageEmbed() + .setColor(0x2D54A2) + .setAuthor('My Anime List', 'https://i.imgur.com/5rivpMM.png') + .setURL(`https://myanimelist.net/manga/${data.id._text}`) + .setThumbnail(data.image._text) + .setTitle(data.title._text) + .setDescription(shorten(cleanXML(data.synopsis._text))) + .addField('❯ Type', + `${data.type._text} - ${data.status._text}`, true) + .addField('❯ Volumes / Chapters', + `${parseInt(data.volumes._text, 10) || '???'} / ${parseInt(data.chapters._text, 10) || '???'}`, true) + .addField('❯ Start Date', + data.start_date._text !== '0000-00-00' ? data.start_date._text : '???', true) + .addField('❯ End Date', + data.end_date._text !== '0000-00-00' ? data.end_date._text : '???', true); + return msg.embed(embed); + } catch (err) { + if (err.message === 'Parse Error') return msg.say('Could not find any results.'); + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/package.json b/package.json index d4028333..8db30f74 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "50.0.0", + "version": "51.0.0", "description": "Your personal server companion.", "main": "XiaoBot.js", "scripts": { diff --git a/util/Util.js b/util/Util.js index bff2c77f..273cfedf 100644 --- a/util/Util.js +++ b/util/Util.js @@ -56,6 +56,19 @@ class Util { return obj; } + static cleanXML(text) { + return text + .replace(/
/g, '') + .replace(/'|�?39;/g, '\'') + .replace(/—/g, '—') + .replace(/–/g, '–') + .replace(/"|�?34;/g, '"') + .replace(/<|�?60;/g, '<') + .replace(/>|�?62;/g, '>') + .replace(/&|�?38;/g, '&') + .replace(/\[i\]|\[\/i\]/g, '*') + } + static greyscale(ctx, x, y, width, height) { const data = ctx.getImageData(x, y, width, height); for (let i = 0; i < data.data.length; i += 4) {