You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Discord.py获取发送命令用户的Discord ID?

解决Discord.py获取发送命令用户ID的问题

你当前代码里用client.user.id获取的是机器人自身的ID,要拿到发送命令的用户ID,应该使用message对象里的author属性。

发送消息的用户信息都包含在message.author中,对应的ID属性是message.author.id。修改你的代码即可实现需求:

if message.content.startswith('$register'):
    await message.channel.send('You are registered successfully')
    print(message.author.id)  # 打印发送命令的用户ID

说明:

  • message.author代表发送这条消息的用户对象,通过.id就能直接获取该用户的Discord ID
  • 不需要把print的结果赋值给变量userk,直接调用print即可完成输出

内容的提问来源于stack exchange,提问作者Mudit Dagar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 23:20:43