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

标签 python python-3.x discord 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:

@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/

相关文章:

python - Python中的 `await`是否会屈服于事件循环?

javascript - Discord.js 存在事件状态和名称存在问题

javascript - 随机化两个数组中的链接元素 Discord.js

python - 使用Python利用爬取的数据

python - Django 1.1 和 Google 应用引擎有稳定的集成程序/插件吗?

Python:以 bool 值作为返回值的列表理解

javascript - 使用 Discord.js 获取所有公会 ID

python - 如何在 Windows 7 上安装 python 2.6.8?

python - 修剪 sklearn 决策树以确保单调性

python - 在Python中读取单个字符(而不是字节)而不等待行尾或EOF