简体   繁体   English

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

[英]How do I get the ID of an user in Discord.py

We're writing a bot for our Discord Server which is supposed to mention the person that starts a specific command.我们正在为我们的 Discord 服务器编写一个机器人,它应该提到启动特定命令的人。 For this I'd need the ID of the user, but can't figure out how to get it.为此,我需要用户的 ID,但不知道如何获取它。

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

author = discord.User.id

await bot.say(str(author))

We tried it this way, since the documentation says, the ID of a user is in the class User.我们这样试过,因为文档说,用户的 ID 在类 User 中。 But the only thing we get is但我们唯一得到的是

<member 'id' of 'User' objects>

In our eyes we got the right param but can't get the ID itself?在我们看来,我们获得了正确的参数,但无法获得 ID 本身? Do we need to convert it somehow?我们需要以某种方式转换它吗?

To get the bot to mention the author of the message in the async branch, you need to refer to that author through the message that invoked the command, ctx.message :要让机器人在异步分支中提及消息的作者,您需要通过调用命令ctx.message的消息来引用该ctx.message

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

To get their id:要获取他们的 id:

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

You need to provide a parameter in the function name.您需要在函数名称中提供一个参数。 Each bot.command needs to have at least one parameter known as the "context".每个 bot.command 至少需要一个参数,称为“上下文”。 Such as:如:

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

We're writing a bot for our Discord Server which is supposed to mention the person that starts a specific command.我们正在为Discord Server写一个机器人,该机器人应该提到启动特定命令的人。 For this I'd need the ID of the user, but can't figure out how to get it.为此,我需要用户的ID,但无法弄清楚如何获取它。

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

author = discord.User.id

await bot.say(str(author))

We tried it this way, since the documentation says, the ID of a user is in the class User.由于文档说,我们以这种方式尝试了该用户的ID在User类中。 But the only thing we get is但是我们唯一得到的是

<member 'id' of 'User' objects>

In our eyes we got the right param but can't get the ID itself?在我们眼中,我们获得了正确的参数,但无法获取ID本身? Do we need to convert it somehow?我们需要以某种方式进行转换吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM