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

Node.js v18.12.1下Cannot read properties of undefined (reading 'GUILD')错误求助

解决Discord机器人错误:Cannot read properties of undefined (reading 'GUILD')

问题根源及修复方案:


1. 语法错误:Client实例化方式错误

你的代码中创建Client对象时误用了方括号[],这是语法错误——构造函数调用必须用圆括号()包裹配置参数。
错误代码:

const client = new Client [{
  intents: [
    Intents.GUILD,
    Intents.GUILD_MESSAGES,
    Intents.GUILD_VOICE_STATES
  ]
}];

修复后:

const client = new Client({
  intents: [
    // 后续调整Intents字段
  ]
});

2. Discord.js版本适配:Intents结构变更

如果你使用的是Discord.js v14及以上版本(结合Node.js v18的环境,大概率是v14+),原有的Intents枚举已被IntentsBitField替代,Intents.GUILD这类属性不再存在,需要改用IntentsBitField.Flags下的对应字段。
完整修复后的客户端初始化代码:

require('dotenv').config();

const {REST} = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
// 替换Intents为IntentsBitField
const { Client, IntentsBitField, Collection } = require('discord.js');
const { Player } = require("discord-player");

const fs = require('fs');
const path = require('path');

const client = new Client({
  intents: [
    IntentsBitField.Flags.Guild,
    IntentsBitField.Flags.GuildMessages,
    IntentsBitField.Flags.GuildVoiceStates
  ]
});

先修正Client实例化的语法错误,再适配Discord.js新版本的Intents结构,即可解决该错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 13:55:17