问题
If I have a user's Discord name and discriminator as a string (e.g ExampleUser#1234
) how can I get their user ID from it? I've found get_user(id)
, but that returns a user object from an ID. I'm using Python and Discord.py.
回答1:
In one line just do
discord.utils.get(client.get_all_members(), name="ExampleUser", discriminator="1234").id
notice the .id
at the end, that gives you the ID of that person.
Also for the discriminator part don't include the hash(#)
回答2:
As stated in this excellent answer by Sam Rockett (Finding a User ID by Discord Discrim via Python (1st ans) you can try this:
p = client.get_all_members()
found_members = filter(lambda m: m.discriminator==your_discrim, p)
member = discord.utils.get(found_members, name=your_username)
id = member.id
P.S. This snippet 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 already an ID.
回答3:
Discord Bots Hub
Hello Discord User
I will tell you the best way to do this. You have to search the member user and matching username with member user.
message.guild.members.find(m => m.user.username === 'USERNAME').user.id
https://thediscordbots.blogspot.com/2019/10/what-are-discord-bots-really-and-which.html
来源:https://stackoverflow.com/questions/57649868/get-discord-user-id-from-username