0

I'm trying to make command to discord bot ,that takes list from this script and send one random from them.I started program in Python about month ago soo it's actually pretty hard for me.

Problem is that when i run this script appears error : Unclosed client session client_session: <aiohttp.client.ClientSession object at 0x000002EE51BE5B80>

import asyncpraw
import asyncio
from aiohttp import ClientSession

async def get_meme(posses=100):
    posts = []
    async with ClientSession() as session:
        async for subreddit in subreddits:
            sub = await reddit.subreddit(subreddit).top(limit=posses, time_filter="week")
            for post in sub:
                await posts.append(post.url)
        await session.close()
        return posts


async def main():
    task = asyncio.create_task(get_meme())
    reddit_memes = await task
    print(reddit_memes)
Angus
  • 70
  • 6
Vipek
  • 13
  • 3

1 Answers1

1

I can see that you are trying to make a meme command. I would recommend asyncpraw the reddit API. Here is a simple example :-

import asyncpraw #Register at https://www.reddit.com/prefs/apps

reddit = asyncpraw.Reddit(client_id = 'client_id',
                     client_secret = 'client_secret',
                     username = 'username',
                     password = 'password',
                     user_agent = 'user_agent')


@client.command()
async def meme(ctx):
  subreddit = await reddit.subreddit("memes")
  all_subs = []
  top = subreddit.top(limit = 100)
  async for submission in top:
      
    all_subs.append(submission)
    
  random_sub = random.choice(all_subs)
  name = random_sub.title
  url = random_sub.url
  link = random_sub.permalink
  embed = discord.Embed(title=name color=ctx.author.color)
  embed.set_image(url=url)
  await ctx.send(embed=embed)
ChaoticNebula
  • 263
  • 1
  • 3
  • 18
  • Well actually i did something like this ,but i need first append it to 'posts' variable and then change my reddit_memes value ,beacuse if i do it like this and want for example get 100 top posts from 10 subreddits it would make big lag for bot (I think it will and image that i want to repeat it every single hour...) EDIT : Sorry for bad eng,this is not my native lang – Vipek Sep 06 '21 at 18:22
  • Its alright, I asked a question, I had this problem too which has been solved. [Here](https://stackoverflow.com/questions/67101891/discord-py-meme-command-takes-a-lot-of-time) is the question I asked. – ChaoticNebula Sep 07 '21 at 03:08
  • Thanks,that's exactly what i wanted :) – Vipek Sep 07 '21 at 11:40