我们正在为我们的 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
:
@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))
关于python - 如何在 Discord.py 中获取用户的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53052814/