Fact Command

This commit is contained in:
Daniel Odendahl Jr
2018-04-21 15:22:51 +00:00
parent 265fa75d91
commit bf23390e7c
3 changed files with 58 additions and 2 deletions
+2 -1
View File
@@ -12,7 +12,7 @@ Xiao is a Discord bot coded in JavaScript with
300 commands, she is one of the most feature-filled bots out there, and formerly
served over 10,000 servers with a uniquely devoted fanbase.
## Commands (304)
## Commands (305)
### Utility:
* **prefix**: Shows or sets the command prefix.
@@ -60,6 +60,7 @@ served over 10,000 servers with a uniquely devoted fanbase.
* **discord-email-fun-fact**: Responds with a random fun fact from the Discord emails.
* **dog-fact**: Responds with a random dog fact.
* **dog**: Responds with a random dog image.
* **fact**: Responds with a random fact.
* **fact-core**: Responds with a random Fact Core quote.
* **fidget**: Responds with a random image of Fidget.
* **fortune**: Responds with a random fortune.
+55
View File
@@ -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;
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "73.0.1",
"version": "73.1.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {