discord.py / PRAW: AttributeError: 'coroutine' object has no attribute 'hot'

discord.py / PRAW: AttributeError: 'coroutine' object has no attribute 'hot'

我最近创建了一个模因机器人,类似于 DankMemer。当我 运行 这个:

    @client.event
async def on_message(message):
    if message.author.bot:
        return
    if '!meme' in message.content:
        await message.channel.purge(limit=1)
        memes_submissions = reddit.subreddit('memes').hot()
        post_to_pick = random.randint(1, 10)
        for i in range(0, post_to_pick):
            submission = next(x for x in memes_submissions if not x.stickied)

        embed = discord.Embed(title=submission.title)
        embed.set_image(url=submission.url)
        await message.channel.send(embed=embed)

出现这个错误:

C:/Users/HP/PycharmProjects/Nudity/main.py:28: RuntimeWarning: coroutine 'SubredditHelper.__call__' was never awaited
  memes_submissions = reddit.subreddit('memes').hot()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\HP\PycharmProjects\Nudity\venv\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/HP/PycharmProjects/Nudity/main.py", line 28, in on_message
    memes_submissions = reddit.subreddit('memes').hot()
AttributeError: 'coroutine' object has no attribute 'hot'

我该如何解决这个问题? (另外,我知道我可以使用@client.command,但我更喜欢 Event one)

您需要等待功能:

subreddit = await reddit.subreddit('memes')
memes_submissions = subreddit.hot()

你可能需要在右边的整个东西周围加上一些括号,我不记得了。

已编辑 - 抱歉,我没有测试它...

我假设你是 Async PRAW,因为 PRAW 不使用协程。

Async PRAW的所有方法都是协程,你需要await每一个。

由于.subreddit()是一个协程,你需要等待.subreddit(),然后在解决后调用.hot()

下面是一个有效的例子:

meme_subreddit = await reddit.subreddit('memes')
memes_submissions = meme_subreddit.hot()