Python 如何在Discord.py中获取用户的ID

Python 如何在Discord.py中获取用户的ID,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我们正在为Discord服务器编写一个bot,它应该提到启动特定命令的人。为此,我需要用户的ID,但不知道如何获取它 @bot.command() async def name(): author = discord.User.id await bot.say(str(author)) 我们这样尝试过,因为文档中说,用户的ID在user类中。但我们唯一得到的是 <member 'id' of 'User' objects> 在我们看来,我们有正确的参数,但不能得到身份证本

我们正在为Discord服务器编写一个bot,它应该提到启动特定命令的人。为此,我需要用户的ID,但不知道如何获取它

@bot.command()
async def name():

author = discord.User.id

await bot.say(str(author))
我们这样尝试过,因为文档中说,用户的ID在user类中。但我们唯一得到的是

<member 'id' of 'User' objects>


在我们看来,我们有正确的参数,但不能得到身份证本身?我们是否需要以某种方式转换它?

您需要在函数名中提供一个参数。每个bot.command需要至少有一个称为“context”的参数。例如:

async def name(ctx):
   author = ctx.message.author
   user_name = author.name

要让bot提到异步分支中消息的作者,您需要通过调用命令的消息引用该作者,
ctx.message

@bot.command(pass_context=True)
async def name(ctx):
    await bot.say("{} is your name".format(ctx.message.author.mention))
要获取其id:

@bot.command(pass_context=True)
async def myid(ctx):
    await bot.say("{} is your id".format(ctx.message.author.id))

重复:如果要提及用户,可以直接使用
user.antify
获取一个字符串,该字符串将在包含在消息中时提及该用户。
discord.user.id
不是获取
user
类实例的
id
,而是从基类本身获取该属性。您需要正确实例化
用户
对象,并从其实例中读取值。如果你还没有实例化它,你怎么能期望你的代码知道你想要哪个用户的ID呢?@PatrickHaugh在这两种情况下,我只得到“ctx是一个缺少的必需参数”。@RandomDavis我期望的是作者的ID,正如它所说的。我想,它会返回发送消息的用户的ID。尝试了这个,得到了“命令引发异常:AttributeError:'str'对象没有属性'author'将其调整为包含消息参数。再试试这个,我们也有这个版本,但好像我打错了。这很好,解释了很多。感谢您的解决方案和帮助。