简体   繁体   中英

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

I recently created a meme bot, similar to DankMemer. When I run this:

    @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)

This error appears:

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'

How can I fix this? (Also, I know I could use @client.command but I prefer the Event one)

I'm going to assume you Async PRAW, since PRAW does not use coroutines.

All of Async PRAW 's methods are coroutines, and you need to await each one.

Since .subreddit() is a coroutine, you need to await .subreddit() , and then call .hot() once it's resolved.

Here is what should be a working example:

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

You need to await the function:

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

You may need some brackets around the whole thing on the right, I can't remember.

EDITED - Sorry, i didn't test it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM