5

如果有人写“?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))
4

4 回答 4

12

要获取消息作者的姓名,您需要使用上下文

@client.command(pass_context=True)
async def name(ctx):
    username = ctx.message.author.name
    ...
于 2017-10-06T21:01:30.930 回答
3

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

@client.command(pass_context=True)
async def name(ctx):
    username = ctx.message.author.display_name
于 2018-09-09T19:36:13.057 回答
0

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

@client.command(pass_context=True)
async def name(ctx):
    username = ctx.message.author.mention
于 2021-06-13T18:41:18.197 回答
0

要获取消息作者的姓名,您可以使用:

message.author.id
于 2017-10-06T18:40:07.230 回答