<property object at 0x...> 尝试获取 Discord 名称时 (discord.py)

问题描述 投票:0回答:1

discord.py 新手,在尝试获取用户名时立即遇到障碍。

这是我的代码:

import discord
from discord.ext import commands

intents = discord.Intents.all()
client = commands.Bot(command_prefix = '!', intents=intents)

user = discord.Member

@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.online, activity=discord.Activity(type = discord.ActivityType.watching, name = 'Test!'))
    print(f"""Bot is on!  Hello World :D!
Logged in as {client.user}
ID: {client.user.id}""")
    
@client.command()
async def t(ctx):
    await ctx.send(user.name)

它不打印名称,而是打印“”。

也尝试过使用discord.User,但没有成功。

python discord.py
1个回答
0
投票

首先,

discord.Member
是一个类对象,当前没有任何内容分配给它。如果您想获取特定用户,可以使用
user = await client.fetch_user(user_id)
,然后使用
await ctx.send(user.display_name) #or username

最后您还需要一个

client.run("TOKEN")

© www.soinside.com 2019 - 2024. All rights reserved.