如何使用 aiohttp 制作 reddit discord bot

时间:2020-12-18 20:35:23

标签: discord discord.py discord.py-rewrite

最近我一直在用 python 制作一个不和谐的机器人,我想添加 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)

唯一的问题是我还没有弄清楚如何添加帖子的网址,以便用户可以访问它。

1 个答案:

答案 0 :(得分:1)

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

您需要一个 reddit API 客户端 ID 和客户端密码,方法是转到 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

如果您感到困惑,我很抱歉,因为我没有很好地解释。