求助:Discord机器人报TypeError: Cannot read properties of undefined (reading 'GUILDS')
问题解决:TypeError: Cannot read properties of undefined (reading 'GUILDS')
错误原因
你在用discord.js v14版本开发机器人,但代码里用了旧版本的Intent写法:v14里GatewayIntentBits本身就是枚举对象,不存在FLAGS属性;同时客户端初始化配置的intents字段名应该是intents,不是GatewayIntentBits。这两个错误导致读取GatewayIntentBits.FLAGS时触发undefined报错。
修正后的代码
修改clientSettingsObject函数的返回内容:
function clientSettingsObject() { return { shards: "auto", allowedMentions: { parse: ["roles", "users", "everyone"], repliedUser: false, }, failIfNotExists: false, partials: ['MESSAGE', 'CHANNEL', 'REACTION'], intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildVoiceStates ] } }
关键修改点
- 把配置里的
GatewayIntentBits键名改成intents,这是discord.js v14要求的正确字段名 - 移除
.FLAGS后缀,直接使用GatewayIntentBits枚举的大驼峰属性(比如GatewayIntentBits.Guilds)
修改后机器人就能正确加载所需的intents,该报错会被解决。
内容的提问来源于stack exchange,提问作者DSkyyy
相关产品推荐
相关产品推荐

