问题
I started making a little discord bot to give a key to people who request it. However, requirements changed and now I need to get a valid email from people who want one. I'm not sure how to get a reply in a DM.
I saw Discord.py get message from DM
But I don't really have client.get_user_info() or something?
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)
Printing ctx.message is
<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>>
I'm really not sure. I'm just stupid, please don't yell at me.
回答1:
You can use Client.wait_for to wait for the message and use its results. You already have the User
object (ctx.author
), so you can get the channel to listen for from User.dm_channel. I'm re-using the message_check
function from this previous answer to generate the check
# 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))
来源:https://stackoverflow.com/questions/57851119/get-message-content-from-reply-in-dm