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

Discord.js中Intents.FLAGS读取报undefined错误求助

Discord.js 报错:TypeError: Cannot read properties of undefined (reading 'FLAGS') 解决方法

你遇到的TypeError: Cannot read properties of undefined (reading 'FLAGS')错误出现在代码第2行,且已在Discord开发者门户启用相关intents但问题仍存在,这个问题和门户设置无关,核心是Discord.js版本不兼容导致的。

问题原因

Discord.js v14版本开始彻底移除了Intents.FLAGS写法,改用GatewayIntentBits直接调用对应的权限常量。如果你的代码还在用Intents.FLAGS但安装的是v14+版本,就会触发这个报错。

解决步骤

  1. 确认Discord.js版本
    在项目目录运行命令:
npm list discord.js

查看当前安装的版本号。

  1. 根据版本修改代码
  • 如果是v14及以上版本,修改代码如下:
const { Client, GatewayIntentBits, MessageEmbed } = require('discord.js');
// 用GatewayIntentBits替代原有的Intents.FLAGS
const client = new Client({ 
  intents: [
    GatewayIntentBits.Guilds, 
    GatewayIntentBits.GuildMessages
  ] 
});

client.login('no peeking');

client.on('ready', readyUser);

function readyUser(){
    console.log('Bot已就绪并登录');
}

client.on("messageCreate", (message) => {
    const prefix = "!";
    const args = message.content.substring(prefix.length).split(" ");
    const command = args.shift().toLowerCase();
    if (message.author.id != client.user.id) {
        // 这里可以添加你的命令逻辑
    }
});

注意:v14里channels不需要单独导入,直接通过client.channels调用即可。

  • 如果是v13及以下版本,先检查导入是否正确,若没问题仍报错,尝试重新安装对应版本依赖:
npm install discord.js@13

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 06:36:32