SnekFetch reddit posts nsfw tag?

Viewed 596

Currently, I am trying to implement a command for a bot that when called upon will get a random post from a subreddit however I've run into an issue. I don't want NSFW posts to be able to be posted in none NSFW channels. Is there a way could tell if the post is NSFW? I'm using SnekFetch.

sf.get(`https://www.reddit.com/r/${args[0]}/random.json?limit=1`).then(res => {
    const url = res.body[0].data.children[0].data.url

This is my current way of getting a Reddit post. Any help is appreciated. Also, any recommendations on a better request handler is also fine. I'm open to anything. Thanks so much.!

1 Answers

As mentionned in the comments

You can check for data['over_18'], if it's true, you can call the api again – Siddharth

So your code would look like

sf.get("https://www.reddit.com/r/" + args[0] + "/random.json?limit=1").then(res => {
if(res_properties.data['over_18']) return message.channel.send('The post is nsfw !')
    const url = res.body[0].data.children[0].data.url
Related