首页 > 解决方案 > 链接到 Discord.py Meme 命令中的 meme

问题描述

我正在 discord.py 中制作一个机器人,并使用 AIOHTTP 制作了一个 meme 命令。我希望嵌入标题成为模因的链接,并且页脚应该显示赞成票和屏幕截图中的所有内容。在此处输入图像描述

这是代码

@client.command(aliases=['memes'])
async def meme(ctx):
    embed = discord.Embed(title='Meme', description=None)

    async with aiohttp.ClientSession() as cs:
        async with cs.get('https://www.reddit.com/r/wholesomememes/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)
    ```

标签: discorddiscord.py

解决方案


用此代码替换它 教程第 1 步:安装 praw (pip install praw) 第 2 步:转到https://www.reddit.com/prefs/apps创建应用第 3 步:复制 client_id 和 client_secret 第 4 步:运行机器人

@bot.command()
async def meme(ctx):

    reddit = praw.Reddit(client_id='clientidhere',
                     client_secret='clientsecret',
                     user_agent='python')

    memes = reddit.subreddit("memes").random()
    await ctx.send(memes.url)

推荐阅读