【问题标题】: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
【问题描述】:
【问题讨论】:
标签:
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)
参考: