首页 > 解决方案 > 从用户名 Discord.py 获取用户 ID

问题描述

我想制作一个程序来获取您输入的用户名的用户 ID。这是我到目前为止所拥有的:

from discord.ext import commands

client = commands.Bot(description="a", command_prefix=".", self_bot = False)

client.remove_command('help')

@client.event
async def on_ready():
    user = await client.fetch_user("pirace#4637")
    print(user)

client.run('token')

它给了我这个错误:

400 Bad Request (error code: 50035): Invalid Form Body
In user_id: Value "pirace#4637" is not snowflake.

有谁知道我会怎么做?

标签: pythondiscorddiscord.pybots

解决方案


如果你想使用你必须使用的用户名discord.utils.get(),在这种情况下结合client.get_guild(GUILD_ID).

from discord.ext import commands

client = commands.Bot(description="a", command_prefix=".", self_bot = False)

client.remove_command('help')

@client.event
async def on_ready():
    guild = client.get_guild(GUILD_ID)
    
    user = discord.utils.get(guild.members, name="pirace", discriminator="4637")

    print(user)

client.run('token')

推荐阅读