【问题标题】:How to use aiohttp to make a reddit discord bot如何使用 aiohttp 制作 reddit discord bot
【发布时间】:2021-03-29 11:51:57
【问题描述】:

最近我一直在用 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)

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

【问题讨论】:

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


    【解决方案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

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

    【讨论】:

    • 谢谢,我去看看!
    猜你喜欢
    • 1970-01-01
    • 2019-04-20
    • 2020-08-12
    • 2021-04-10
    • 2019-02-04
    • 2020-08-23
    • 1970-01-01
    • 2021-03-15
    • 2021-06-15
    相关资源
    最近更新 更多