【问题标题】:Python How to send multiple filesPython如何发送多个文件
【发布时间】:2018-08-19 09:47:33
【问题描述】:

如何让机器人上传多张带有内容描述的图片。我可以添加多行它可以工作,但是当它有超过 5 个await bot.send 行时,机器人会减慢发布速度。我需要添加一些图像,所以如果可能的话如何在同一行中添加。

@bot.command(pass_context=True)
async def ping(ctx):
    await bot.send_file(ctx.message.channel, "Image1.png", content="Image1")

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    您想在一次等待中运行多个异步任务。

    你应该使用asyncio.wait:

    import asyncio
    
    @bot.command(pass_context=True)
    async def ping(ctx):
      files = ...  # Set the 5 files (or more ?) you want to upload here
      await asyncio.wait([bot.send_file(ctx.message.channel, f['filename'], content=f['content'] for f in files)])
    

    (见Combine awaitables like Promise.all

    【讨论】:

    • 您的理解中缺少)[bot.send_file(ctx.message.channel, f['filename'], content=f['content']) for f in files]
    • files 是您要上传的文件列表。根据你的问题,你想上传多个文件。您应该将... 替换为您要上传的文件。
    • 根据我的 sn-p,更像 files = [{"filename": "image1.png", "content": "image1"}, {"filename": "image2.png", "content": "image2"}, {"filename": "image3.png", "content": "image3"}],但您可以根据需要/需要进行编辑。
    • @Blusky 是的,它现在正在工作。除了 1 个问题,它不会按顺序一一发送。每次使用命令时,它都会随机发送上下命令。比如 image3, image5, image1, image 2 像这样。
    • 正常的,是同时上传的(所以比较快)。但你不能确定哪一个会是第一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    相关资源
    最近更新 更多