【问题标题】:Get message content from reply in DM从 DM 中的回复中获取消息内容
【发布时间】:2019-09-09 09:20:13
【问题描述】:

我开始制作一个不和谐的小机器人,为请求它的人提供钥匙。但是,需求发生了变化,现在我需要从想要的人那里获得一封有效的电子邮件。我不确定如何在 DM 中获得回复。

我看到Discord.py get message from DM

但我真的没有 client.get_user_info() 什么的?

bot = commands.Bot(command_prefix='!')


@bot.command(pass_context =True, no_pm=True, name='key', help="I give you a key. you're welcome", )



async def key_giver(ctx):
    commandTime = str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))


##check if you're PMing bot
    elif isinstance (ctx.channel, discord.DMChannel):
        print(ctx.message)
        await ctx.author.send(keyArray[0])

    else:
        response = "Hello my friend. I give you a key for a game. This is my purpose. Please enter your email!"



    ##Send key via PM 
    await ctx.author.send(response)



打印ctx.message是

<Message id=620538695124254720 channel=<DMChannel id=614377525417738240 recipient=<User id=605223566371192852 name='aon' discriminator='5906' bot=False>> type=<MessageType.default: 0> author=<User id=605223566371192852 name='aon' discriminator='5906' bot=False>>

我真的不确定。我只是笨,请不要对我大喊大叫。

【问题讨论】:

    标签: python discord discord.py discord.py-rewrite


    【解决方案1】:

    您可以使用Client.wait_for 等待消息并使用其结果。您已经拥有User 对象(ctx.author),因此您可以从User.dm_channel 获取要收听的频道。我正在重新使用message_check 函数from this previous answer 来生成支票

    # Send them a message in a DM
    await ctx.author.send("Please give an email address")
    
    # Wait for a response
    response = await bot.wait_for('message', check=message_check(channel=ctx.author.dm_channel))
    

    【讨论】:

    • 嗯。 respose 只是返回相同的对象有点像<Message id=620845388961808406 channel=<DMChannel id=614377525417738240 recipient=<User id=605223566371192852 name='aon' discriminator='5906' bot=False>> type=<MessageType.default: 0> author=<User id=605223566371192852 name='aon' discriminator='5906' bot=False>> 不是消息的实际内容?
    • 您可以通过response.content访问内容
    猜你喜欢
    • 2019-04-21
    • 2013-04-02
    • 2021-05-24
    • 2014-10-25
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多