summaryrefslogtreecommitdiff
path: root/src/commands/fun/randomfacts.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/fun/randomfacts.ts')
-rw-r--r--src/commands/fun/randomfacts.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/commands/fun/randomfacts.ts b/src/commands/fun/randomfacts.ts
new file mode 100644
index 0000000..5d9777e
--- /dev/null
+++ b/src/commands/fun/randomfacts.ts
@@ -0,0 +1,66 @@
+import { Command, CommandoMessage } from 'discord.js-commando';
+import request from 'node-superfetch'
+import emoji from 'emoji-random'
+
+module.exports = class RandomFactFun extends Command {
+ constructor(client) {
+ super(client, {
+ name: 'randomfact',
+ aliases: [
+ 'random-fact',
+ 'fact',
+ 'facts'
+ ],
+ group: 'fun',
+ memberName: 'randomfact',
+ description: 'Gives you a random fact.',
+ examples: ['uwu!randomfact'],
+ throttling: {
+ usages: 5,
+ duration: 30
+ },
+ userPermissions: ['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
+ clientPermissions: ['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
+ });
+ }
+ async run(msg: CommandoMessage) {
+ 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.reply(fact + ' ' + emoji.random());
+ } catch (err) {
+ return msg.reply(`Woops, an error has occured: \`${err.message}\`. Try again later! ${emoji.random()}`)
+ }
+ }
+ 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
+ });
+ if (!body.query.random[0].title) return 'Facts are hard to find sometimes.';
+ return body.query.random[0].title;
+ }
+}; \ No newline at end of file