juju toolz
juju toolz

Reputation: 41

Get user id from username Discord.py

I want to make a program that will get the user id of a username you enter. Here is what I have so far:

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')

It gives me this error:

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

Does anyone know how I would do this?

Upvotes: 3

Views: 9939

Answers (2)

FileX
FileX

Reputation: 791

If you want to use the name of the user you have to use discord.utils.get(), in this case combined with 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')

Upvotes: 1

Kile Alkuri
Kile Alkuri

Reputation: 31

fetch_user takes in a snowflake or in easier terms, id. Getting a user with usernername is possible by making something like this:

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

This only works if the user shares a guild with your bot though and is case sensitive. So either put a valid user id as an integer in bot.fetch_user or use the code I provided

Upvotes: 1

Related Questions