How Do I Add a Reddit Function to My Python Discord Bot?

How Do I Add a Reddit Function to My Python Discord Bot?

I want to make a bot that when you do "!meme it posts a random meme from r/memes/hot/. How would I do that? I already have some base code, but it doesn't work. Also, I'm not sure what exactly is a user_agent is. could someone give me an example?

client = Bot(command_prefix=BOT_PREFIX)
reddit = praw.Reddit(client_id='id',
                     client_secret='secret',
                     user_agent='windows 10: Meme Scraper (by /u/PotatoLord1207)')

@client.command()
async def meme():
    memes_submissions = reddit.subreddit('memes').hot()
    post_to_pick = random.randint(1, 20)
    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)
9

1 Answer

I have made a meme command in discord.py rewrite version. This may help you:

@client.command()
async def meme(ctx):
    subreddit = reddit.subreddit("memes")
    all_subs = []

    top = subreddit.top(limit = 50)

    for submission in top:
        all_subs.append(submission)

    random_sub = random.choice(all_subs)

    name = random_sub.title
    url = random_sub.url

    embed = discord.Embed(title = name, color=discord.Color.gold())
    embed.set_image(url = url)
    embed.set_footer(text=f"Asked by {ctx.author.name}")

    await ctx.send(embed=embed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.