Izzy Skywalker
Izzy Skywalker

Reputation: 13

discord.py: Lookup user by id

I'm new to making Discord bots using Python. I have a list of user ids and I need to be able to resolve them to Discord usernames. Running client.get_user(my_id) returns my Discord username with discriminator, except it doesn't work on anyone else and it doesn't work for my username 100% of the time. According to the documentation, I should be able to run it for any user ID but it returns None no matter what. No matter what I look up, I always get the same answer -- client.get_user() and it's driving me crazy. My code to run the bot is as follows

client = commands.Bot(command_prefix = '.b ')
client.remove_command('help')
------
client.run(TOKEN)

Upvotes: 0

Views: 3840

Answers (2)

unex
unex

Reputation: 1356

If you are running discord.py v1.5, please see this documentation page on how to enable Privileged Intents for your bot. This is a recent change that would explain why your bot cannot see any users.

Upvotes: 0

Sachin Raja
Sachin Raja

Reputation: 304

Not sure if this is exactly the problem. get_user will not work for any users that are not in any guilds with the bot. However, fetch_user will work for all users:

await client.fetch_user(user_id)

Ensure that user_id is an integer, not a string. Also, use this sparingly as it is an API call and thus is subject to rate limits.

Upvotes: 1

Related Questions