首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未关闭的客户端会话

未关闭的客户端会话
EN

Stack Overflow用户
提问于 2021-09-06 16:58:00
回答 1查看 283关注 0票数 0

我正在尝试向discord机器人发出命令,它从这个脚本中获取列表,并从它们中随机发送一个。我大约一个月前开始用Python编写程序,所以这对我来说是相当困难的。

问题是,当我运行此脚本时出现错误:未关闭的客户端会话client_session:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-06 17:13:21

我看得出来你在尝试做一个表情包命令。我会推荐asyncpraw的reddit API。下面是一个简单的例子:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69078058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档