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

Discord.js示例代码无法运行,GatewayIntentBits.Guilds报错求助

Discord.js中GatewayIntentBits.Guilds报错的解决方法

问题原因

你这段代码是Discord.js v14版本的写法,但当前安装的Discord.js是v13或更早版本。v13及以前的版本里不存在GatewayIntentBits这个对象,对应的intent标识是放在Intents.FLAGS下面的,因此会触发Cannot read property 'Guilds' of undefined报错。

注释掉该行后程序能启动,是因为旧版本Discord.js在未显式指定intents时,会默认启用部分基础intent,但这种方式不推荐,后续使用服务器相关功能时会出现权限缺失问题。

两种解决方法

方法一:升级到Discord.js v14

如果想继续使用v14的语法,执行以下命令更新依赖:

# 卸载旧版本
npm uninstall discord.js
# 安装最新版(v14)
npm install discord.js@latest

安装完成后,运行npm list discord.js确认版本号为14.x.x,原代码即可正常运行。

方法二:适配当前已安装的旧版本(v13及更早)

修改代码中的intent导入和使用方式,替换为旧版本的写法:

// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');

// Create a new client instance
const client = new Client({ intents: [
    Intents.FLAGS.GUILDS
] });

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');
});

// Login to Discord with your client's token
client.login(token);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 04:30:56