从用户名 Discord.py 获取用户 ID

时间:2021-04-24 22:29:42

标签: python discord discord.py bots

我想制作一个程序来获取您输入的用户名的用户 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.

有人知道我会怎么做吗?

2 个答案:

答案 0 :(得分:0)

fetch_user 接受雪花或更简单的术语,id。使用 usernername 获取用户是可能的,方法如下:

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

这仅在用户与您的机器人共享公会并且区分大小写时才有效。因此,要么将有效的用户 ID 作为整数放入 bot.fetch_user 中,要么使用我提供的代码

答案 1 :(得分:0)

如果要使用用户名,则必须使用 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)
    
    discord.utils.get(guild.members, name="pirace", discriminator="#4637")

    print(user)

client.run('token')