繁体   English   中英

当我的机器人向用户发送 DM 时,我如何使用 discord.py(在 DM 内)获取下一条消息

[英]When my bot DMs a user, how do i get it's next message with discord.py (within DMs)

我正在研究一个机器人。 当你说r!report时,它会向用户发送 DM,逐一询问用户名、原因、附件等信息。

因此,当我的机器人向用户发送 DM 时,我如何获取用户在我的机器人的 DM 之后发送的下一条消息,并存储它,然后使用他们提供的所有信息创建一个嵌入? 我可以嵌入,但我似乎找不到检查消息的方法。

另外,如果您可以包括如何获取他们在 DM 中发送的 png/jpg 文件,我将如何存储它?

https://gyazo.com/7aedd5d89dc54fc48ec6b63cd34d9024

你可以使用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)

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM