-1

I am trying to create a listener for meme. The bot will respond to the message when it has "meme" in it. It has to send a meme. I am using Praw for that purpose.

This is the code I have written:

@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 = "👍"
    subreddit = reddit.subreddit("memes")
    for submission in subreddit.random():
        submission_link = submission.url
    
    if findCoindences('meme')(content.lower()):
        await message.reply(submission_link)
        await message.add_reaction(f"<{reaction}>")

Note: I have defined reddit already above some lines. That is not the cause I guess.


The problem:

Whenever I send meme in the chat it sends the same meme every time. I want it to be different but don't know where I am having this issue.

This is what it looks like: Ufff

5
  • random_submission = reddit.subreddit('memes').random(). I am not sure what iterating over subreddit.random() is even doing. Commented May 28, 2021 at 3:10
  • @Goion It throws an error. I had tried it before. Submission is not iterable. Commented May 28, 2021 at 4:00
  • Dude, you have the submission. Just post the link. Why do you want to iterate so badly? Commented May 28, 2021 at 4:03
  • Umm! I don't know how to work with Praw friend. I never used it. You might need to tell me. Commented May 28, 2021 at 4:07
  • I have answered your question. It should work. Commented May 28, 2021 at 4:25

1 Answer 1

1

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.

2
  • @BhavyadeepYadav Np. I am pretty sure you would have been able to figure it out as well if you took a minute to think. Commented May 28, 2021 at 4:43
  • Yep! Maybe! But I know now anyways. Commented May 28, 2021 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.