Discord.py - ASYNCPRAW 优化

发布于 2025-01-16 08:04:04 字数 1021 浏览 0 评论 0原文

有什么办法可以优化这段代码吗?只是响应时间太长:

@bot.command()
async def picture(ctx, name=None):
    pic_submission = await reddit.subreddit("Genshin_Impact")
    if name is None:
        submission = random.choice([i async for i in pic_submission.hot(params={'include_over_18': 'on'},
                                                                               limit=100)])
    else:
        submission = random.choice([i async for i in nsfw_submission.search(f"{name}", sort="hot",
                                                                                  params={'include_over_18': 'on'},
                                                                                      limit=100)])
    url = submission.url
    title = submission.title
    embed = discord.Embed(title=f'__{title}__', colour=discord.Colour.light_gray(),
                          timestamp=ctx.message.created_at, url=url)
    embed.set_image(url=url)
    await ctx.send(embed=embed)
    

Is there any way to optimize this code? It just takes tooo long to response:

@bot.command()
async def picture(ctx, name=None):
    pic_submission = await reddit.subreddit("Genshin_Impact")
    if name is None:
        submission = random.choice([i async for i in pic_submission.hot(params={'include_over_18': 'on'},
                                                                               limit=100)])
    else:
        submission = random.choice([i async for i in nsfw_submission.search(f"{name}", sort="hot",
                                                                                  params={'include_over_18': 'on'},
                                                                                      limit=100)])
    url = submission.url
    title = submission.title
    embed = discord.Embed(title=f'__{title}__', colour=discord.Colour.light_gray(),
                          timestamp=ctx.message.created_at, url=url)
    embed.set_image(url=url)
    await ctx.send(embed=embed)
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2025-01-23 08:04:04

我注意到限制是100!由于您的程序会迭代 100 个帖子,因此需要很长时间才能回复,因此请尝试降低限制!

I noticed that the limit is 100! It takes long to respond because your program iterates through 100 posts so try lowering the limit!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文