未关闭的客户端会话
Unclosed client session
我正在尝试向 discord bot 发出命令,它从这个脚本中获取列表并从 them.I 大约一个月前在 Python 启动的程序中随机发送一个,所以这对我来说实际上很难.
问题是当我运行这个脚本出现错误:未关闭的客户端会话
client_session:
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)
我看到你正在尝试制作表情包命令。我会推荐 asyncpraw reddit API。这是一个简单的例子:-
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)
我正在尝试向 discord bot 发出命令,它从这个脚本中获取列表并从 them.I 大约一个月前在 Python 启动的程序中随机发送一个,所以这对我来说实际上很难.
问题是当我运行这个脚本出现错误:未关闭的客户端会话
client_session:
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)
我看到你正在尝试制作表情包命令。我会推荐 asyncpraw reddit API。这是一个简单的例子:-
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)