简体   繁体   中英

Finding a user by Discord Discrim via Python

Once again, I'm into Discord bot coding.

This time, I'm trying to make a way to find a user by it's Discrim. Getting the user by it's Discrim, would easly allow to do another things, like, find his ID, that by the meaning time, the profile picture, name...

According to discord.py, we need to use get_user_info(user_id). But finding someone's ID can be hard for some, so if I could get the ID by the discrim and then do whatever, would be much easier.

I tried do the following:

DefaultUser = discord.User
hi = DiscrimIguess[0]
hi2 = discord.User.discriminator.hi

You get the"DiscrimIguess", when the user types the command plus the discrim.

Any way I can do this? Because it won't work.

Firstly, you should not be using a discrim to find a user. IDs are unique, discrims are not.

Discrims are given to each member to help determine who is who when two members share the same name (ie test#0001 and test#0002 are different people)

Anyway, while you cannot find a single specific user by discrims, you can find a list of all the users who share a discrim by using Client.get_all_members

p = client.get_all_members()
found_members = filter(lambda m: m.discriminator==DiscrimIguess[0], p)

If you also have a username you can narrow that list down to a single member and then get the ID from that.

member = discord.utils.get(found_members, name=username)
id = member.id

This method only works for finding members who share a server with the bot. To find users who do not share a server with the bot, you must have an ID already for `Client.get_user_info'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM