如何使用 aiohttp 制作 reddit discord 机器人

How to use aiohttp to make a reddit discord bot

最近我一直在用 python 制作一个不和谐的机器人,我想添加 reddit 命令,正如我在 Dank Memer、MEE6 和其他发送图像的机器人中看到的那样 post来自 reddit。我在网上找到了一些代码(我是 discord.py 的新手)并且我找到了如何使用 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)

唯一的问题是我还没有想出如何添加 post 的 url 以便用户可以访问它。

你应该使用 praw,praw 是一个 reddit API 包装器,使用起来更容易,可以在 cmd 中使用命令 pip install -U praw 安装。

您需要一个 reddit API 客户端 ID 和客户端密码,方法是转到 apps page 然后按 'are you a developer? create an app...' 按钮。

标题、描述和重定向 uri 随心所欲,因为它们未被使用。完成此操作后,获取您的客户端 ID,可以在应用程序名称和客户端密码下找到它。

现在进入您的代码并在代码开头添加 import praw。 然后创建一个名为 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

对不起,如果我没有解释清楚,让您感到困惑。