Home > Blockchain >  discord.py repost images the user attached
discord.py repost images the user attached

Time:01-30

I have this code

  if message.content.startswith("add"):
    if len(message.attachments) == 0:
      await message.channel.send("There's no picture.")
    elif message.content == "add":
      await message.channel.send("There's no text.")
    else:
      text = str(message.attachments)[0]
      url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.& ]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F])) ', text) # extracts the url out of the string
      await message.channel.send(f"{message.content[4:]}")
      await message.channel.send(file=discord.File(str(url)))

For example, I send "add Abc" with an attachment of a picture of an airplane, it should just repost that airplane picture and also send Abc

But it doesn't for some reason, the thing returns

[]

CodePudding user response:

what are you doing there with the URL? please read the documentation

also please note that Discord sends Images as a Image if you just post the URL, so you could send the Message with the following line: await message.channel.send(content=f'{message.content[4:]}\nmessage.attachments[0].url'

CodePudding user response:

attached = await message.attachments[0].to_file()
await message.channel.send(file=attached)
  •  Tags:  
  • Related