Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找消息的作者_Python_Discord_Discord.py - Fatal编程技术网

Python 查找消息的作者

Python 查找消息的作者,python,discord,discord.py,Python,Discord,Discord.py,如果有人写“?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

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

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

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

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

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

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

我试过这段代码,它说消息没有定义,所以我在代码顶部定义了消息,现在它说str没有author属性。等等,ctx.author.name也可以吗?我试着用我的机器人,它返回了正确的值。两种方法都有效。我之所以选择这个表单,是因为我确信
消息
ctx
的一部分,因为我个人在我的bot中使用stock discord.py,而不是
ext.commands
,后者提供了
命令()
装饰器和默认的
ctx
对象。
@client.command(pass_context=True)
async def name(ctx):
    username = ctx.message.author.mention