从username Discord.py获取用户id

2024-04-27 02:24:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个程序,将获得您输入的用户名的用户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.

有人知道我会怎么做吗


Tags: 用户fromimport程序clientidbot情况
2条回答

fetch_user采用雪花或更简单的术语id。通过如下方式可以获得具有usernername的用户:

@bot.command()
async def info(ctx, user:discord.User):
    return await ctx.send(user.name)

这只适用于用户与您的机器人共享公会并且区分大小写的情况。因此,要么在bot.fetch_user中将有效的用户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')

相关问题 更多 >