L
L
LimeGeeg2021-11-18 23:09:21
Python
LimeGeeg, 2021-11-18 23:09:21

Is there any way to resend the image via discord.py that the user submitted?

Is there any way to resend the image via discord.py that the user submitted?
For example, I sent a picture to a bot, and he resent it to another user in private messages. Please help with code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RuslanUC, 2021-11-18
@RuslanUC

Write this code in the on_message function:

if message.attachments:
    files = []
    for attachment in message.attachments:
        files.append(await attachment.to_file())
    if files:
        user = await bot.fetch_user(user_id) # получение пользователя
        dm = await user.create_dm() # получение лс
        await dm.send(files=files)

Replace user_id with the id of the user to whom you want to send pictures (the user must be allowed messages from all users in the discord settings).
The code above will send all files that the bot will receive (including files sent on servers). To send only images, you need to check the file type and the code will look like this:
if message.attachments:
    files = []
    for attachment in message.attachments:
        try:
            if attachment.content_type.startswith("image/"):
                files.append(await attachment.to_file())
        except:
            continue
    if files:
        user = await bot.fetch_user(user_id) # получение пользователя
        dm = await user.create_dm() # получение лс
        await dm.send(files=files)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question