Python 如何使用 discord.py 获取不和谐用户的用户 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43543355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:10:24  来源:igfitidea点击:

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

pythondiscorddiscord.py

提问by Gibs LeFleur

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

我使用 Discord.py 并且我试图在用户输入频道时获取用户的 Discord 用户 ID。

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

进入开发者模式时可以找到用户ID,右键单击用户名,会出现“复制ID”选项。

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

当前的 api 没有说如何做到这一点,或者我一直在想念它

回答by Alexandre Sejournant

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

文档说User该类具有用户的 id:http:
//discordpy.readthedocs.io/en/latest/api.html#user

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

Member是一个子类Userhttp:
//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

因此,如果您收到来自用户的消息,则可以通过以下方式获取 id 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')