【问题标题】:When my bot DMs a user, how do i get it's next message with discord.py (within DMs)当我的机器人向用户发送消息时,我如何使用 discord.py 获得下一条消息(在 DM 中)
【发布时间】:2021-04-18 16:23:24
【问题描述】:

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

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

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

https://gyazo.com/7aedd5d89dc54fc48ec6b63cd34d9024

【问题讨论】:

    标签: python discord bots discord.py


    【解决方案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)
    

    参考:

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2022-08-11
      • 1970-01-01
      • 2021-10-04
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 2022-01-11
      相关资源
      最近更新 更多