diff --git a/README.md b/README.md index 5532844b..9492f717 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/commands/random/fact.js b/commands/random/fact.js new file mode 100644 index 00000000..d127cd5c --- /dev/null +++ b/commands/random/fact.js @@ -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; + } +}; diff --git a/package.json b/package.json index 8122fb4f..4c9974b9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "73.0.1", + "version": "73.1.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {