Praw & Discord.py: The bot keep sending the same meme. I want the bot to send different meme whenever it is asked

There is no need for you to iterate over anything. Praw provides a method which allows you to grab a random submission from a subreddit.

Documentation: random()

Code:

@nd.listen()
async def on_message(message):
    if message.author == nd.user:
        return
    
    def findCoindences(w):
        return re.compile(r'\b({0})\b'.format(w)).search
    content = message.content
    reaction = "👍"
    
    if findCoindences('meme')(content.lower()):
        random_submission = reddit.subreddit('memes').random()
        await message.reply(random_submission.url)
        await message.add_reaction(f"<{reaction}>")

Note: I have move the random_submission inside the If-statement because there is no need for you to look up a submission if the message doesn’t say “meme”. It would just waste rate limit.

Leave a Comment