Adding post URLs when fetching Reddit content with aiohttp for Discord bot

I’m working on a Discord bot using Python and trying to implement Reddit functionality similar to popular bots. I managed to get images from Reddit posts using aiohttp, but I’m stuck on one thing.

async def reddit_post(context):
    post_embed = discord.Embed(title="Random post from r/funny", color=0x00ff00)
    async with aiohttp.ClientSession() as session:
        async with session.get('https://www.reddit.com/r/funny/hot.json') as response:
            data = await response.json()
            random_post = data['data']['children'][random.randint(0, 24)]['data']
            post_embed.set_image(url=random_post['url'])
            await context.send(embed=post_embed)

This code works fine for displaying images, but I can’t figure out how to include the actual Reddit post link so users can click through to see comments and upvotes. Any ideas on how to extract and add the permalink to the embed?

You can also use post_embed.description to combine the title and link. Try post_embed.description = f"{random_post['title']}\n\n[View full post](https://www.reddit.com{random_post['permalink']})" - gives you a clean layout. I’ve run this setup for months and it works great with text and image posts. Don’t forget to check random_post['over_18'] for NSFW posts before sending. Learned that the hard way when mods started complaining about content filtering.

just add random_post['permalink'] for the link! use post_embed.url = 'https://www.reddit.com' + random_post['permalink'] to make the title clickable or throw it in a field. works great on my bot.

Yeah, the permalink approach works, but here’s a cleaner way to do it. Skip making the title clickable - just add the URL as its own field: post_embed.add_field(name="View on Reddit", value=f"https://www.reddit.com{random_post['permalink']}", inline=False). Way more obvious for users since they can see the link right away instead of guessing the title’s clickable. I get way better click-through rates this way. Also throw in some error handling for your random selection - posts can get stickied or deleted and break things.