如何使用 discord.py 获取不和谐用户的用户 ID

Gib*_*eur 2 python discord discord.py

我使用 Discord.py 并且我试图在用户输入频道时获取用户的 Discord 用户 ID。

进入开发者模式时可以找到用户ID,右键单击用户名,会出现“复制ID”选项。

当前的 api 没有说如何做到这一点,或者我一直在想念它

Ale*_*ant 6

文档说User该类具有用户的 id:http :
//discordpy.readthedocs.io/en/latest/api.html#user

Member是一个子类Userhttp :
//discordpy.readthedocs.io/en/latest/api.html#member

因此,如果您收到来自用户的消息,则可以通过以下方式获取 id message.author.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')
Run Code Online (Sandbox Code Playgroud)

  • 有没有一种方法可以从传递到命令函数的 @username 获取 id? (3认同)