简体   繁体   中英

How do I get the User ID of a discord user using discord.py

Im using Discord.py and Im trying to get the Discord userid of a user when they type into a channel.

The userid can be found when you go into developer mode, and right click on a username, there will be an option "copy id".

The current api is not saying how to do this, or I keep missing it

The documentation says that the User class have the user's id:
http://discordpy.readthedocs.io/en/latest/api.html#user

And that Member is a subclass of User :
http://discordpy.readthedocs.io/en/latest/api.html#member

So if you got a message from a user, you can get the id with message.author.id

import discord
import asyncio

client = discord.Client()

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

@client.event
async def on_message(message):
    print(message.author.id)

client.run('token')

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