Issue
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)
Solution
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)
Answered By - ChaoticNebula
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.