Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meme Command discord.py

I have 2 meme commands in my discord.py bot. I am using the redditeasy api as someone told me that it is faster asyncpraw and praw(which is true).

my Question: Is there any way to randomise the subreddits? What i mean to say is, The api should take a random subreddit from the list of subreddits given, and then pick a random post from that randomly selected subreddit.

I tried doing:

random_sub_list = random.choice['Subreddits here']

and then passing that variable in subreddit but doesn't work.

Here is my code:

#memes


@bot.command(aliases = ['m'])
async def meme(ctx):
  post = redditeasy.AsyncSubreddit(subreddit = 'dankmemes',                              client_id='id here',
                                client_secret = 'secret',
                                user_agent = 'memes')

  postoutput = await post.get_post()

  em2 = discord.Embed(title = f'{postoutput.title}')
  url = postoutput.content
  
  em2.set_image(url = url)
  await ctx.send(embed = em2)


#narutomemes


@bot.command(aliases = ['Nmeme', 'NMEME', 'nm', 'NM', 'Nm'])
async def nmeme(ctx):
  post = redditeasy.AsyncSubreddit(subreddit = 'narutomemes',                              client_id='id here',
                                client_secret = 'secret',
                                user_agent = 'memes')

  postoutput = await post.get_post()

  em3 = discord.Embed(title = f'{postoutput.title}')
  url = postoutput.content
  
  em3.set_image(url = url)
  await ctx.send(embed = em3)
like image 497
Nishil Sheth Avatar asked Sep 15 '26 01:09

Nishil Sheth


1 Answers

I tried something like this.

def get_post_from_random_subreddit():
    subreddit_name = random.choice(["dankmemes", "narutomemes"])
    subreddit = redditeasy.AsyncSubreddit(
        subreddit=subreddit_name,
        client_secret="secret",
        user_agent="memes"
    )
    post = asyncio.run(subreddit.get_post())
    return f"{post.title} from {post.subreddit_name}"

But without the discord bot and it works perfect. The posts are randomly from the 2 subreddits. I also used your client secret.

Be careful with sharing this secret. Some might abuse it!

like image 125
Sowasvonbot Avatar answered Sep 17 '26 15:09

Sowasvonbot