You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在discord.py中获取用户的显示昵称?

获取用户显示昵称的正确方式
  • 在服务器(Guild)场景下,不要使用msg.author.name(该属性在新版Discord.js中行为已变更,可能返回用户ID),应使用msg.member.displayName——它会优先返回用户在当前服务器的自定义昵称,若未设置则自动回退到用户的全局用户名。
  • 若处于私信等非服务器场景,因不存在服务器专属昵称,直接使用msg.author.username即可。

示例代码:

// 兼容服务器与私信场景的写法
const userNickname = msg.member?.displayName || msg.author.username;
console.log(userNickname);

内容的提问来源于stack exchange,提问作者Esea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 22:42:04