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

Categories

0 votes
4.7k views
in Technique[技术] by (71.8m points)

python - Programming a bot in Discord- How do I make the bot send a random image from a subreddit?

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...