Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to make my bot send a random image from a subreddit (r/memes). Here's what I have:

@client.event
async def on_message(message):
if message.content.startswith('.meme'):
    memes_submissions = reddit.subreddit('memes').hot()
    post_to_pick = random.randint(1, 50)
    for i in range(0, post_to_pick):
        submission = next(x for x in memes_submissions if not x.stickied)

    await bot.say(submission.url)

It doesn't seem to be working, any tips?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
4.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

bot.say was removed in discord.py 1.0 and replaced with .send.

So the line await bot.say(submission.url) should be replaced with await message.channel.send(submission.url)

https://discordpy.readthedocs.io/en/latest/migrating.html?highlight=say#removed-helpers

In your for loop you recreate the list of non-sticked post each time and then call next but because you recreate it will always return first element of recreated list. You should move the x for x in memes_submissions if not x.stickied out of the loop into a varible. You could then user next but easier would be just to index that variable like it is a list with the random number.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...