Когда мой бот отправляет DMS пользователю, как мне получить его следующее сообщение с discord.py (в рамках DMs)

#python #discord #bots #discord.py

#python #Discord #боты #discord.py

Вопрос:

Я работаю над ботом. r!report Когда вы говорите, он отправляет пользователю запрос на получение некоторой информации, такой как имя пользователя, причина, вложение, одно за другим.

Итак, когда мой бот отправляет DMS пользователю, как мне получить следующее сообщение, которое пользователь отправляет после DMS моего бота, и сохранить его, а затем создать вставку со всей информацией, которую они предоставили? Я могу выполнить встраивание, но, похоже, я не нахожу способ проверить сообщение.

Также, если бы вы могли указать, как получить файл png / jpg, который они отправляют в DMs, как бы я его сохранил?

https://gyazo.com/7aedd5d89dc54fc48ec6b63cd34d9024

Ответ №1:

Вы можете использовать wait_for

 @bot.command()
async def whatever(ctx):
    def check(message: discord.Message):
         """Checks if the channel where the message was sent is DM
         and if the author of the message is the same as the invoker of the command"""
         return isinstance(message.channel, discord.DMChannel) and message.author == ctx.author

    await ctx.author.send("Hello! Please reply to this message")
    message = await bot.wait_for("message", check=check) # You can also add a timeout, read the docs for it

    print(f"{ctx.author} replied: {message.content}")
 

Чтобы получить файлы из сообщений, вы можете просто использовать Message.attachments атрибут

 channel = bot.get_channel(ID) # This is just for testing

# Looping through every attachment in the message
for attch in message.attachments:
    f = await attch.to_file() # Returns a `discord.File` instance, NOTE: this does NOT save the file, to save it use `Attachment.save`

    # Sending the file
    await channel.send(file=f)
 

Ссылка: