mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-19 21:40:51 +02:00
Change to xml2js
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const snekfetch = require('snekfetch');
|
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 {
|
module.exports = class GelbooruCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
@@ -31,9 +33,10 @@ module.exports = class GelbooruCommand extends Command {
|
|||||||
q: 'index',
|
q: 'index',
|
||||||
tags: query
|
tags: query
|
||||||
});
|
});
|
||||||
const parsed = xml2js(text, { compact: true }).posts;
|
const body = await xml(text);
|
||||||
if (!parsed.post || !parsed.post.length) return msg.say('Could not find any results.');
|
const data = body.posts.post;
|
||||||
return msg.say(parsed.post[Math.floor(Math.random() * parsed.post.length)]._attributes.file_url);
|
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) {
|
} catch (err) {
|
||||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const { MessageEmbed } = require('discord.js');
|
const { MessageEmbed } = require('discord.js');
|
||||||
const snekfetch = require('snekfetch');
|
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 { shorten, cleanXML } = require('../../util/Util');
|
||||||
const { MAL_LOGIN } = process.env;
|
const { MAL_LOGIN } = process.env;
|
||||||
|
|
||||||
@@ -29,23 +31,23 @@ module.exports = class MyAnimeListAnimeCommand extends Command {
|
|||||||
const { text } = await snekfetch
|
const { text } = await snekfetch
|
||||||
.get(`https://${MAL_LOGIN}@myanimelist.net/api/anime/search.xml`)
|
.get(`https://${MAL_LOGIN}@myanimelist.net/api/anime/search.xml`)
|
||||||
.query({ q: query });
|
.query({ q: query });
|
||||||
const body = xml2js(text, { compact: true }).anime;
|
const body = await xml(text);
|
||||||
const data = body.entry.length ? body.entry[0] : body.entry;
|
const data = body.anime.entry[0];
|
||||||
const embed = new MessageEmbed()
|
const embed = new MessageEmbed()
|
||||||
.setColor(0x2D54A2)
|
.setColor(0x2D54A2)
|
||||||
.setAuthor('My Anime List', 'https://i.imgur.com/5rivpMM.png')
|
.setAuthor('My Anime List', 'https://i.imgur.com/5rivpMM.png')
|
||||||
.setURL(`https://myanimelist.net/anime/${data.id._text}`)
|
.setURL(`https://myanimelist.net/anime/${data.id[0]}`)
|
||||||
.setThumbnail(data.image._text)
|
.setThumbnail(data.image[0])
|
||||||
.setTitle(data.title._text)
|
.setTitle(data.title[0])
|
||||||
.setDescription(shorten(cleanXML(data.synopsis._text)))
|
.setDescription(shorten(cleanXML(data.synopsis[0])))
|
||||||
.addField('❯ Type',
|
.addField('❯ Type',
|
||||||
`${data.type._text} - ${data.status._text}`, true)
|
`${data.type[0]} - ${data.status[0]}`, true)
|
||||||
.addField('❯ Episodes',
|
.addField('❯ Episodes',
|
||||||
data.episodes._text, true)
|
data.episodes[0], true)
|
||||||
.addField('❯ Start Date',
|
.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',
|
.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);
|
return msg.embed(embed);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.message === 'Parse Error') return msg.say('Could not find any results.');
|
if (err.message === 'Parse Error') return msg.say('Could not find any results.');
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const { MessageEmbed } = require('discord.js');
|
const { MessageEmbed } = require('discord.js');
|
||||||
const snekfetch = require('snekfetch');
|
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 { shorten, cleanXML } = require('../../util/Util');
|
||||||
const { MAL_LOGIN } = process.env;
|
const { MAL_LOGIN } = process.env;
|
||||||
|
|
||||||
@@ -29,23 +31,23 @@ module.exports = class MyAnimeListMangaCommand extends Command {
|
|||||||
const { text } = await snekfetch
|
const { text } = await snekfetch
|
||||||
.get(`https://${MAL_LOGIN}@myanimelist.net/api/manga/search.xml`)
|
.get(`https://${MAL_LOGIN}@myanimelist.net/api/manga/search.xml`)
|
||||||
.query({ q: query });
|
.query({ q: query });
|
||||||
const body = xml2js(text, { compact: true }).manga;
|
const body = await xml(text);
|
||||||
const data = body.entry.length ? body.entry[0] : body.entry;
|
const data = body.manga.entry[0];
|
||||||
const embed = new MessageEmbed()
|
const embed = new MessageEmbed()
|
||||||
.setColor(0x2D54A2)
|
.setColor(0x2D54A2)
|
||||||
.setAuthor('My Anime List', 'https://i.imgur.com/5rivpMM.png')
|
.setAuthor('My Anime List', 'https://i.imgur.com/5rivpMM.png')
|
||||||
.setURL(`https://myanimelist.net/manga/${data.id._text}`)
|
.setURL(`https://myanimelist.net/manga/${data.id[0]}`)
|
||||||
.setThumbnail(data.image._text)
|
.setThumbnail(data.image[0])
|
||||||
.setTitle(data.title._text)
|
.setTitle(data.title[0])
|
||||||
.setDescription(shorten(cleanXML(data.synopsis._text)))
|
.setDescription(shorten(cleanXML(data.synopsis[0])))
|
||||||
.addField('❯ Type',
|
.addField('❯ Type',
|
||||||
`${data.type._text} - ${data.status._text}`, true)
|
`${data.type[0]} - ${data.status[0]}`, true)
|
||||||
.addField('❯ Volumes / Chapters',
|
.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',
|
.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',
|
.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);
|
return msg.embed(embed);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.message === 'Parse Error') return msg.say('Could not find any results.');
|
if (err.message === 'Parse Error') return msg.say('Could not find any results.');
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const snekfetch = require('snekfetch');
|
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 {
|
module.exports = class SafebooruCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
@@ -30,9 +32,10 @@ module.exports = class SafebooruCommand extends Command {
|
|||||||
q: 'index',
|
q: 'index',
|
||||||
tags: query
|
tags: query
|
||||||
});
|
});
|
||||||
const parsed = xml2js(text, { compact: true }).posts;
|
const body = await xml(text);
|
||||||
if (!parsed.post || !parsed.post.length) return msg.say('Could not find any results.');
|
const data = body.posts.post;
|
||||||
return msg.say(`https:${parsed.post[Math.floor(Math.random() * parsed.post.length)]._attributes.file_url}`);
|
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) {
|
} catch (err) {
|
||||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiaobot",
|
"name": "xiaobot",
|
||||||
"version": "51.0.0",
|
"version": "51.0.1",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "XiaoBot.js",
|
"main": "XiaoBot.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -40,7 +40,7 @@
|
|||||||
"node-opus": "^0.2.7",
|
"node-opus": "^0.2.7",
|
||||||
"snekfetch": "^3.5.7",
|
"snekfetch": "^3.5.7",
|
||||||
"uws": "^8.14.1",
|
"uws": "^8.14.1",
|
||||||
"xml-js": "^1.5.1",
|
"xml2js": "^0.4.19",
|
||||||
"zlib-sync": "^0.1.3"
|
"zlib-sync": "^0.1.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
Reference in New Issue
Block a user