Categories > Coding > Javascript >

[TUTORIAL] How to get random images form reddit

Wabz

Winning.js

Posts: 2410

Threads: 194

Joined: Apr, 2020

Reputation: 27

Posted

WARNING: It won't work on all subreddits. Subreddits like r/cats and many others won't work

 

1. You need a module to do get requests and stuff like that. I use superagent because a few years ago when i watched a tutorial on discord.js by menudocs that's what he used for his cat command.

 

2. Once you installed superagent, copy this code (it is pretty basic it's not an embed):

// beginning

try {

      var sr = "r/" + args[0];

      var { body } = await superagent.get(

        "https://www.reddit.com/" + sr + "/random.json"

      );

      if (!body[0].data.children[0].data.over_18 || msg.channel.nsfw) {

        msg.channel.send(body[0].data.children[0].data.url_overridden_by_dest);

      } else msg.channel.send("Filtered content.");

    } catch {

      msg.channel.send("I was not able to do it. Try again.");

    }

// end

 

you can put that in your execute for your file

 

for me, msg is the message (discord) and args is the list of arguments after the command

args[0] will be the subreddit of choice

You can check the thing it gives to see stuff as title and other stuff but this was meant to be a simple tutorial

 

it works on: memes, linuxmemes, unixp*rn, leagueofmemes and a ton of other subreddits.

Btw, it filters adult stuff if you are not in a nsfw channel.

 

i wrote that code a long time ago

  • 0

Added

I had to censor unixp*rn otherwise it would not let me but this subreddit is sfw don't worry.

  • 0

My new discord is Wabz#1337 with ID 777154062789509130

Posts: 0

Threads: 0

Joined: ?

Reputation:

Replied

Thx dis is will be so helpful!! :D

  • 0

Hectique

Hectique

Posts: 1307

Threads: 186

Joined: Nov, 2019

Reputation: 14

Replied

Very useful, thanks a lot!

  • 0

a former active and known member of the WRD community, but im kinda just a meme now

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )