summaryrefslogtreecommitdiff
path: root/src/models/commands/Subreddit.ts
blob: ea6a9fa95a92afd17191ba64fd6a111bb4d17dd8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { CommandoMessage } from 'discord.js-commando';
const Command = require('../Command.js')
import request from 'node-superfetch'
//@ts-ignore no @types for this package
import emoji from 'emoji-random'

module.exports = class SubredditCommand extends Command {
    subreddit: any;
    postType: any[] | null;
    getIcon: boolean;
    credit: any;
    constructor(client: any, info: { subreddit: any; postType: any; getIcon: boolean; }) {
        super(client, info);

        this.subreddit = info.subreddit;
        this.postType = info.postType ? Array.isArray(info.postType) ? info.postType : [info.postType] : null;
        this.getIcon = info.getIcon || false;
        this.credit.push({
            name: 'Reddit',
            url: 'https://www.reddit.com/',
            reason: 'API',
            reasonURL: 'https://www.reddit.com/dev/api/'
        });
    }

    async run(msg: CommandoMessage, { subreddit }: any, fromPattern: any) {
        if (fromPattern) subreddit = msg.patternMatches[1];
        if (!subreddit) subreddit = typeof this.subreddit === 'function' ? this.subreddit() : this.subreddit;
        try {
            //@ts-ignore no idea why this isnt in the commando libs
            const post = await this.random(subreddit, msg.channel.nsfw);
            if (!post) return msg.reply('Could not find any results. ' + emoji.random());
            // TODO:
            //@ts-ignore
            return msg.say(this.generateText(post.post, post.origin, post.icon));
        } catch (err) {
            if (err.status === 403) return msg.reply('This subreddit is private. ' + emoji.random());
            if (err.status === 404) return msg.reply('Could not find any results. ' + emoji.random());
            return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
        }
    }

    generateText() {
        throw new Error('The generateText method is required.');
    }

    async random(subreddit: any, nsfw: any) {
        let icon = null;
        const { body } = await request
            .get(`https://www.reddit.com/r/${subreddit}/hot.json`)
            // TODO:
            //@ts-ignore
            .query({ limit: 100 });
        //@ts-ignore yes it does
        if (!body.data.children.length) return null;
        //@ts-ignore yes it does
        const posts = body.data.children.filter(post => {
            if (!post.data) return false;
            if (!nsfw && post.data.over_18) return false;
            return (this.postType ? this.postType.includes(post.data.post_hint) : true) && post.data.url && post.data.title;
        });
        if (!posts.length) return null;
        if (this.getIcon) icon = await this.fetchIcon(subreddit);
        return {
            origin: subreddit,
            post: posts[Math.floor(Math.random() * posts.length)].data,
            icon
        };
    }

    async fetchIcon(subreddit: any) {
        const { body } = await request.get(`https://www.reddit.com/r/${subreddit}/about.json`);
        //@ts-ignore yes it does
        if (!body.data.icon_img && !body.data.community_icon) return 'https://i.imgur.com/DSBOK0P.png';
        //@ts-ignore yes it does
        return body.data.icon_img || body.data.community_icon;
    }
};