Search code examples
pythondiscorddiscord.pypraw

Is there anyway to make PRAW faster for discord.py


This works and I have no issue with it. my bot is about 1100 lines of code so not that much. I have a standard meme command using PRAW. Is there any way to make its response time faster and not embedding it is not an option. My ms is average.

code:

@client.command(pass_context=True)
async def meme(ctx):
    subreddit = reddit.subreddit("meme")
    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)

    embed.set_image(url=url)

    await ctx.send(embed=embed)

Solution

  • You can store a meme and then send it later. By sending and then getting a meme, you do not have to wait to get a meme before sending one. Then you prepare for the next one.

    @client.command(pass_context=True)
    async def meme(ctx):
        
        if not hasattr(client, 'nextMeme'):
            client.nextMeme = getMeme()
    
        name, url = client.nextMeme
        embed = discord.Embed(title = name)
    
        embed.set_image(url=url)
    
        await ctx.send(embed=embed)
        
        client.nextMeme = getMeme()
    
    
    def getMeme():
        subreddit = reddit.subreddit("meme")   
    
        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
    
        return name, url
    
    
    

    I changed bot to client, if client does not work for you then replace it with bot.