Python 如何更改用户';的昵称,知道其id,但不知道其成员对象(Discord.py)

Python 如何更改用户';的昵称,知道其id,但不知道其成员对象(Discord.py),python,discord,discord.py,Python,Discord,Discord.py,请帮忙。由于某些原因,我无法从id获取成员对象 @bot.command() async def shift_nick(ctx: commands.Context, new_nick : str , line: str = None) #line - Secret string that I use to search for the user id in the database ..... #Working with the database. As a result I get the

请帮忙。由于某些原因,我无法从id获取成员对象

@bot.command()
async def shift_nick(ctx: commands.Context, new_nick : str , line: str = None) #line - Secret string that I use to search for the user id in the database

..... #Working with the database. As a result I get the value of the user_id variable and the value is correct

user =  ctx.guild.get_member(int(user_id)) #And for some reason the value is None
await user.edit(nick=new_nick)#And because l is None, I can't change the username on the server```

get\u member
返回的值为
None
的原因很可能是您没有启用成员特权网关

为了解决您的问题,我将尝试以下方法。首先,通过转到
https://discord.com/developers/applications//bot
并检查“服务器成员意图”

然后,按以下方式使用成员意图:

导入不一致
意图=不和谐。意图(成员=真)
client=commands.Bot(command\u prefix=“!”,intents=intents)
#代码的其余部分。。。

您从未定义过用户id?我的机器人有一个注册,它会在其中保存discord\u id。如果用户需要更改其昵称,他们会向有权使用此命令的特定人群请求。用户只需给出他们的秘密字符串,该字符串是机器人在注册过程中提供给他们的,然后他们更改他们的昵称。你可能会想:“如果你能给用户更改昵称的能力,为什么要这样做呢?”但是在这个机器人存在的情况下,这是“危险的”你确定
user\u id
是正确的吗?根据文档,如果没有找到成员,将返回
None
。user\u是正确的,这是肯定的。也许我的机器人看不到所有用户,但它也不能检查用户id是否是字符串。如果user_id是整数,int()将返回None。若你们调试一点,比如检查用户是否真的被找到了,那个也很好。