diff --git a/commands/search/gelbooru.js b/commands/search/gelbooru.js index 10a29fea..f43e9020 100644 --- a/commands/search/gelbooru.js +++ b/commands/search/gelbooru.js @@ -1,6 +1,8 @@ const { Command } = require('discord.js-commando'); const snekfetch = require('snekfetch'); -const { xml2js } = require('xml-js'); +const { parseString } = require('xml2js'); +const { promisify } = require('util'); +const xml = promisify(parseString); module.exports = class GelbooruCommand extends Command { constructor(client) { @@ -31,9 +33,10 @@ module.exports = class GelbooruCommand extends Command { q: 'index', tags: query }); - const parsed = xml2js(text, { compact: true }).posts; - if (!parsed.post || !parsed.post.length) return msg.say('Could not find any results.'); - return msg.say(parsed.post[Math.floor(Math.random() * parsed.post.length)]._attributes.file_url); + const body = await xml(text); + const data = body.posts.post; + if (!data || !data.length) return msg.say('Could not find any results.'); + return msg.say(data[Math.floor(Math.random() * data.length)].$.file_url); } 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 index cabe5360..f87a68fb 100644 --- a/commands/search/my-anime-list-anime.js +++ b/commands/search/my-anime-list-anime.js @@ -1,7 +1,9 @@ const { Command } = require('discord.js-commando'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); -const { xml2js } = require('xml-js'); +const { parseString } = require('xml2js'); +const { promisify } = require('util'); +const xml = promisify(parseString); const { shorten, cleanXML } = require('../../util/Util'); const { MAL_LOGIN } = process.env; @@ -29,23 +31,23 @@ module.exports = class MyAnimeListAnimeCommand extends Command { 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.entry.length ? body.entry[0] : body.entry; + const body = await xml(text); + const data = body.anime.entry[0]; 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))) + .setURL(`https://myanimelist.net/anime/${data.id[0]}`) + .setThumbnail(data.image[0]) + .setTitle(data.title[0]) + .setDescription(shorten(cleanXML(data.synopsis[0]))) .addField('❯ Type', - `${data.type._text} - ${data.status._text}`, true) + `${data.type[0]} - ${data.status[0]}`, true) .addField('❯ Episodes', - data.episodes._text, true) + data.episodes[0], true) .addField('❯ Start Date', - data.start_date._text !== '0000-00-00' ? data.start_date._text : '???', true) + data.start_date[0] !== '0000-00-00' ? data.start_date[0] : '???', true) .addField('❯ End Date', - data.end_date._text !== '0000-00-00' ? data.end_date._text : '???', true); + data.end_date[0] !== '0000-00-00' ? data.end_date[0] : '???', true); return msg.embed(embed); } catch (err) { if (err.message === 'Parse Error') return msg.say('Could not find any results.'); diff --git a/commands/search/my-anime-list-manga.js b/commands/search/my-anime-list-manga.js index f7ab50bf..868df425 100644 --- a/commands/search/my-anime-list-manga.js +++ b/commands/search/my-anime-list-manga.js @@ -1,7 +1,9 @@ const { Command } = require('discord.js-commando'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); -const { xml2js } = require('xml-js'); +const { parseString } = require('xml2js'); +const { promisify } = require('util'); +const xml = promisify(parseString); const { shorten, cleanXML } = require('../../util/Util'); const { MAL_LOGIN } = process.env; @@ -29,23 +31,23 @@ module.exports = class MyAnimeListMangaCommand extends Command { 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.entry.length ? body.entry[0] : body.entry; + const body = await xml(text); + const data = body.manga.entry[0]; 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))) + .setURL(`https://myanimelist.net/manga/${data.id[0]}`) + .setThumbnail(data.image[0]) + .setTitle(data.title[0]) + .setDescription(shorten(cleanXML(data.synopsis[0]))) .addField('❯ Type', - `${data.type._text} - ${data.status._text}`, true) + `${data.type[0]} - ${data.status[0]}`, true) .addField('❯ Volumes / Chapters', - `${parseInt(data.volumes._text, 10) || '???'} / ${parseInt(data.chapters._text, 10) || '???'}`, true) + `${parseInt(data.volumes[0], 10) || '???'} / ${parseInt(data.chapters[0], 10) || '???'}`, true) .addField('❯ Start Date', - data.start_date._text !== '0000-00-00' ? data.start_date._text : '???', true) + data.start_date[0] !== '0000-00-00' ? data.start_date[0] : '???', true) .addField('❯ End Date', - data.end_date._text !== '0000-00-00' ? data.end_date._text : '???', true); + data.end_date[0] !== '0000-00-00' ? data.end_date[0] : '???', true); return msg.embed(embed); } catch (err) { if (err.message === 'Parse Error') return msg.say('Could not find any results.'); diff --git a/commands/search/safebooru.js b/commands/search/safebooru.js index 5561e2bb..85ad4f7f 100644 --- a/commands/search/safebooru.js +++ b/commands/search/safebooru.js @@ -1,6 +1,8 @@ const { Command } = require('discord.js-commando'); const snekfetch = require('snekfetch'); -const { xml2js } = require('xml-js'); +const { parseString } = require('xml2js'); +const { promisify } = require('util'); +const xml = promisify(parseString); module.exports = class SafebooruCommand extends Command { constructor(client) { @@ -30,9 +32,10 @@ module.exports = class SafebooruCommand extends Command { q: 'index', tags: query }); - const parsed = xml2js(text, { compact: true }).posts; - if (!parsed.post || !parsed.post.length) return msg.say('Could not find any results.'); - return msg.say(`https:${parsed.post[Math.floor(Math.random() * parsed.post.length)]._attributes.file_url}`); + const body = await xml(text); + const data = body.posts.post; + if (!data || !data.length) return msg.say('Could not find any results.'); + return msg.say(`https:${data[Math.floor(Math.random() * data.length)].$.file_url}`); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } diff --git a/package.json b/package.json index 8db30f74..f28eacc3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "51.0.0", + "version": "51.0.1", "description": "Your personal server companion.", "main": "XiaoBot.js", "scripts": { @@ -40,7 +40,7 @@ "node-opus": "^0.2.7", "snekfetch": "^3.5.7", "uws": "^8.14.1", - "xml-js": "^1.5.1", + "xml2js": "^0.4.19", "zlib-sync": "^0.1.3" }, "devDependencies": {