summaryrefslogtreecommitdiff
path: root/server/src/commands/fun/Fact
diff options
context:
space:
mode:
author8cy <[email protected]>2020-07-23 23:24:17 -0700
committer8cy <[email protected]>2020-07-23 23:24:17 -0700
commitbb511abc03bb66848947e37a999502b813c77269 (patch)
tree612c010fc8317e1cdf11471a18aad0270819d33e /server/src/commands/fun/Fact
parentfix: if clear amount equal or over 100, round down to 99 (diff)
downloaddep-core-bb511abc03bb66848947e37a999502b813c77269.tar.xz
dep-core-bb511abc03bb66848947e37a999502b813c77269.zip
goodbye old uwufier :cry:
Diffstat (limited to 'server/src/commands/fun/Fact')
-rw-r--r--server/src/commands/fun/Fact67
1 files changed, 67 insertions, 0 deletions
diff --git a/server/src/commands/fun/Fact b/server/src/commands/fun/Fact
new file mode 100644
index 0000000..c942b45
--- /dev/null
+++ b/server/src/commands/fun/Fact
@@ -0,0 +1,67 @@
+import { Command } from 'discord-akairo';
+import { Message } from 'discord.js';
+import request from 'node-superfetch';
+
+export default class FactFun extends Command {
+ public constructor() {
+ super('fact', {
+ aliases: ['fact', 'facts'],
+ category: 'fun',
+ description: {
+ content: 'Grabs a random fact.',
+ usage: '',
+ examples: [
+ ''
+ ]
+ },
+ ratelimit: 3
+ });
+ }
+
+ public async exec(msg: Message): Promise<Message> {
+ 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: '',
+ //@ts-ignore
+ formatversion: 2
+ })
+ .catch(err => {
+ console.error(err);
+ msg.reply('Woops, there was an error regarding the (http://en.wikipedia.org) API.');
+ });
+ //@ts-ignore
+ 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);
+ }
+
+ public async randomWikipediaArticle() {
+ const { body } = await request.get('https://en.wikipedia.org/w/api.php')
+ .query({
+ action: 'query',
+ list: 'random',
+ //@ts-ignore
+ rnnamespace: 0,
+ //@ts-ignore
+ rnlimit: 1,
+ format: 'json',
+ //@ts-ignore
+ formatversion: 2
+ });
+ //@ts-ignore
+ if (!body.query.random[0].title) return 'Facts are hard to find sometimes.';
+ //@ts-ignore
+ return body.query.random[0].title;
+ }
+} \ No newline at end of file