mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
55 lines
1.3 KiB
JavaScript
55 lines
1.3 KiB
JavaScript
const Command = require('../../structures/Command');
|
|
const request = require('node-superfetch');
|
|
|
|
module.exports = class FactCommand extends Command {
|
|
constructor(client) {
|
|
super(client, {
|
|
name: 'fact',
|
|
group: 'random',
|
|
memberName: 'fact',
|
|
description: 'Responds with a random fact.'
|
|
});
|
|
}
|
|
|
|
async run(msg) {
|
|
try {
|
|
const article = await this.randomWikipediaArticle();
|
|
const { body } = await request
|
|
.get('https://en.wikipedia.org/w/api.php')
|
|
.query({
|
|
action: 'query',
|
|
prop: 'extracts',
|
|
format: 'json',
|
|
titles: article,
|
|
exintro: '',
|
|
explaintext: '',
|
|
redirects: '',
|
|
formatversion: 2
|
|
});
|
|
let fact = body.query.pages[0].extract;
|
|
if (fact.length > 200) {
|
|
const facts = fact.split('.');
|
|
fact = `${facts[0]}.`;
|
|
if (fact.length < 200 && facts.length > 1) fact += `${facts[1]}.`;
|
|
}
|
|
return msg.say(fact);
|
|
} catch (err) {
|
|
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
|
}
|
|
}
|
|
|
|
async randomWikipediaArticle() {
|
|
const { body } = await request
|
|
.get('https://en.wikipedia.org/w/api.php')
|
|
.query({
|
|
action: 'query',
|
|
list: 'random',
|
|
rnnamespace: 0,
|
|
rnlimit: 1,
|
|
format: 'json',
|
|
formatversion: 2
|
|
});
|
|
return body.query.random[0].title;
|
|
}
|
|
};
|