查找消息的作者

Finding author of a message

如果有人写“?name (arg)”,我希望我的机器人说消息的作者 + “,你的名字是” + arg。 虽然我找不到邮件的作者

@client.command()
async def name(their_name):
    await client.say("{0}, your name is {1}.".format("""author of the message goes here""", their_name))

要获取邮件作者的姓名,您可以使用:

message.author.id

要获取邮件作者的姓名,您需要使用 context

@client.command(pass_context=True)
async def name(ctx):
    username = ctx.message.author.name
    ...

您也可以使用 User.display_name,这将尝试使用用户服务器特定的昵称,但如果找不到则回退到通用用户名:

@client.command(pass_context=True)
async def name(ctx):
    username = ctx.message.author.display_name

您也可以使用 .mention,所以如果您将其发送到频道,它会标记作者

@client.command(pass_context=True)
async def name(ctx):
    username = ctx.message.author.mention