最近我一直在用 python 制作一个 discord 机器人,我想添加 reddit 命令,就像我在 Dank Memer、MEE6 和其他机器人上看到的那样,它们从 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)
唯一的问题是我还没有想出如何添加帖子的 url 以便用户可以访问它。
最佳答案
你应该使用praw ,praw 是一个 reddit API 包装器,使用起来更容易,可以通过在 cmd 中使用命令 pip install -U praw
进行安装。
您需要一个 reddit API 客户端 ID 和客户端 key ,方法是转到 apps page然后按下'你是开发人员吗?创建应用...”按钮。
将标题、描述和重定向 uri 设为您想要的任何内容,因为它们未被使用。完成此操作后,获取您的客户端 ID,该 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
如果您感到困惑,我很抱歉,因为我没有很好地解释。
关于discord - 如何使用 aiohttp 制作 reddit discord 机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65363590/