You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用aiohttp开发Reddit Discord机器人并添加帖子访问链接?

Adding Reddit Post Links to Your Discord Bot's Meme Command

Hey there! Nice start on your Discord bot's meme feature—adding a link to the original Reddit post is a great touch that users will appreciate. Let's adjust your existing code to make that happen smoothly.

First, let's break down what we need to do:

  • Grab the permalink of the Reddit post (this is the relative URL like /r/memes/comments/xyz123/funny_meme/)
  • Combine it with Reddit's base URL to get the full, clickable link
  • Add this link to your Discord Embed—either as a clickable title, a dedicated field, or both

Here's the updated version of your code with these changes:

import discord
import aiohttp
import random

async def meme(ctx):
    embed = discord.Embed(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()
            # Pull a random post from the hot feed
            post_data = res['data']['children'][random.randint(0, 25)]['data']
            
            # Build the full Reddit post link
            post_permalink = post_data['permalink']
            full_post_url = f"https://reddit.com{post_permalink}"
            
            # Customize the embed with clickable title, post content, and image
            embed.title = f"[Hot Post from r/memes]({full_post_url})"
            embed.description = post_data['title']  # Show the original post title
            embed.set_image(url=post_data['url'])
    
    await ctx.send(embed=embed)

Key Changes Explained:

  • Storing post data: We save the selected post's data to post_data first, which makes the code cleaner and avoids repeated nested indexing.
  • Full post link: The permalink field from Reddit's API gives us a relative path—we just prepend https://reddit.com to get a working, clickable URL.
  • Clickable embed title: By wrapping the title text in [] and the URL in (), Discord automatically turns it into a clickable link.
  • Post title as description: Adding the original Reddit post title helps users understand the context of the meme before clicking through.

Bonus: Make It More Robust

Right now, your code might pull non-image posts (like videos or text posts). To fix that, we can filter for only image posts:

import discord
import aiohttp
import random

async def meme(ctx):
    embed = discord.Embed(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()
            # Filter to only include image posts
            image_posts = [
                child['data'] for child in res['data']['children']
                if child['data'].get('post_hint') == 'image'
            ]
            
            if not image_posts:
                await ctx.send("Oops, no image posts found in r/memes right now!")
                return
            
            # Pick a random image post
            post_data = random.choice(image_posts)
            full_post_url = f"https://reddit.com{post_data['permalink']}"
            
            embed.title = f"[Hot Image Post from r/memes]({full_post_url})"
            embed.description = post_data['title']
            embed.set_image(url=post_data['url'])
    
    await ctx.send(embed=embed)

This way, you'll only send valid image posts to your Discord server, avoiding broken embeds or non-image content.

内容的提问来源于stack exchange,提问作者Vorato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:17:23