How to use aiohttp to make a reddit discord bot

Viewed 561

Lately I have been making a discord bot with python, and I wanted to add reddit commands, as I've seen with bots like Dank Memer, MEE6, and others send image posts from reddit. I found some code online (I'm pretty new to discord.py) and I found how to do this with aiohttp

async def meme(ctx):
embed = discord.Embed(title="Post from r/memes.", description=None, color=0xff0000)
async with aiohttp.ClientSession() as cs:
    async with cs.get('https://www.reddit.com/r/memes/new.json?sort=hot') as r:
        res = await r.json()
        embed.set_image(url=res['data']['children'] [random.randint(0, 25)]['data']['url'])
        await ctx.send(embed=embed, content=None)

The only problem with this is that I haven't figured out how to also add the url of the post so that the user can visit it.

1 Answers

you should use praw, praw is a reddit API wrapper and is much easier to use and can be installed by using the command pip install -U praw in cmd.

You will need a reddit API client ID and client secret by going to the apps page then pressing the 'are you a developer? create an app...' button.

Make the title, description and redirect uri what ever you want as its not used. Once this is done get your client ID, which can be found under the app name and the client secret.

Now go into your code and add import praw at the start of your code. Then make a new varible called reddit.

reddit = praw.Reddit(client_id-='CLIENTID', client_secret='CLIENTSECRET', user_agent='WhateverYouWant'

Example of a command that shows the hottest posts from a subreddit

I'm sorry if your confused as I haven't explained very well.

Related