如何使用discord.py获取Discord用户的用户ID

4
我正在使用Discord.py,并尝试在用户在频道中输入时获取其Discord用户ID。 当您进入开发者模式并右键单击用户名时,可以找到用户ID,并显示“复制ID”选项。 当前的API没有说明如何执行此操作,或者我一直错过了它。
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
12
文档中提到 User 类包含用户的 ID:
http://discordpy.readthedocs.io/en/latest/api.html#userMemberUser 的子类:
http://discordpy.readthedocs.io/en/latest/api.html#member 所以,如果你从用户那里收到了一条消息,你可以用 message.author.id 来获取他们的ID。
import discord
import asyncio

client = discord.Client()

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

@client.event
async def on_message(message):
    print(message.author.id)

client.run('token')

谢谢,我有另一个关于Discord OAuth2的问题。我正在尝试使用OAuth2获取用户ID,但它没有返回。我正在使用这个插件https://github.com/teamreflex/oauth2-discord - Gibs LeFleur
@GibsLeFleur 这超出了我的知识范围,我建议您另外提一个关于这个问题的问题。 - Alexandre Sejournant
3
有没有一种方法可以从传递到命令函数的@用户名中获取一个ID? - Chibi Ayano

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,