繁体   English   中英

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

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

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

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

author = discord.User.id

await bot.say(str(author))

我们这样试过,因为文档说,用户的 ID 在类 User 中。 但我们唯一得到的是

<member 'id' of 'User' objects>

在我们看来,我们获得了正确的参数,但无法获得 ID 本身? 我们需要以某种方式转换它吗?

要让机器人在异步分支中提及消息的作者,您需要通过调用命令ctx.message的消息来引用该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))

您需要在函数名称中提供一个参数。 每个 bot.command 至少需要一个参数,称为“上下文”。 如:

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

我们正在为Discord Server写一个机器人,该机器人应该提到启动特定命令的人。 为此,我需要用户的ID,但无法弄清楚如何获取它。

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

author = discord.User.id

await bot.say(str(author))

由于文档说,我们以这种方式尝试了该用户的ID在User类中。 但是我们唯一得到的是

<member 'id' of 'User' objects>

在我们眼中,我们获得了正确的参数,但无法获取ID本身? 我们需要以某种方式进行转换吗?

暂无
暂无

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

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