2

How to make bot upload multiple image with content description. i can add multiple lines it works, but bot slows posting when it has more than 5 await bot.send line. i need to add few images so how to do it if possible in same line.

@bot.command(pass_context=True)
async def ping(ctx):
    await bot.send_file(ctx.message.channel, "Image1.png", content="Image1")
Demotry
  • 869
  • 4
  • 21
  • 40

1 Answers1

2

You want to run multiple asynchronous tasks in one await.

You should use 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)])

(See Combine awaitables like Promise.all)

Blusky
  • 3,470
  • 1
  • 19
  • 35
  • 2
    You're missing a `)` in your comprehension: `[bot.send_file(ctx.message.channel, f['filename'], content=f['content']) for f in files]` – Patrick Haugh Aug 19 '18 at 13:09
  • `files` is the list of files you want to upload. According to you question, you want to upload multiple files. You shoul replace `...` with the files you want to upload. – Blusky Aug 20 '18 at 13:03
  • 1
    More like `files = [{"filename": "image1.png", "content": "image1"}, {"filename": "image2.png", "content": "image2"}, {"filename": "image3.png", "content": "image3"}]` according to my snippet, but you can edit things according to what you want/need. – Blusky Aug 21 '18 at 17:25
  • @Blusky yes its working now. except 1 problem it won't send by one by one in order. everytime when command used it sends randomly up and down order. example image3, image5, image1, image 2 like this. – Demotry Aug 22 '18 at 07:54
  • It's normal, it's uploaded simultaneously (so it's faster). But you can't know for sure which one will be the first. – Blusky Aug 22 '18 at 19:14