mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-26 22:32:52 +02:00
Fact Command
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
const { Command } = require('discord.js-commando');
|
||||
const snekfetch = require('snekfetch');
|
||||
|
||||
module.exports = class FactCommand extends Command {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: 'fact',
|
||||
aliases: ['wikifakt'],
|
||||
group: 'random',
|
||||
memberName: 'fact',
|
||||
description: 'Responds with a random fact.'
|
||||
});
|
||||
}
|
||||
|
||||
async run(msg) {
|
||||
try {
|
||||
const article = await this.randomWikipediaArticle();
|
||||
const { body } = await snekfetch
|
||||
.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 snekfetch
|
||||
.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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user