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

Discord Bot调用GitBook内容报错:Cannot read properties of undefined (reading 'FLAGS')

解决Discord Bot中Cannot read properties of undefined (reading 'FLAGS')错误

问题详情

开发一款可调取GitBook内容的Discord Bot时,运行程序出现以下错误:

Cannot read properties of undefined (reading 'FLAGS')
at Object.<anonymous> (C:\Users\91827\Desktop\real\bot.js:6:47)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47

原错误代码片段:

const { Client, Intents } = require('discord.js');
const axios = require('axios');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

错误原因

该错误源于Discord.js版本不兼容:你的代码使用的是v13及更早版本的Intent声明方式,但当前安装的是v14或更高版本。在v14+版本中,Discord.js移除了Intents.FLAGS的写法,不再导出Intents对象,而是改用IntentsBitField来声明权限意图。

修复方案

方案1:适配Discord.js v14+(推荐)

修改导入语句和Intent的引用方式,适配新版本规范:

  1. 将导入的Intents替换为IntentsBitField
  2. 把Intents.FLAGS.XXX改为IntentsBitField.Flags.XXX

修改后的核心代码:

const { Client, IntentsBitField } = require('discord.js');
const axios = require('axios');
const client = new Client({ 
  intents: [
    IntentsBitField.Flags.Guilds, 
    IntentsBitField.Flags.GuildMessages
  ] 
});

方案2:降级到Discord.js v13

若不想修改代码,可卸载当前版本并安装v13稳定版:

npm uninstall discord.js
npm install discord.js@13.17.1

完整修复后代码(适配v14+)

const { Client, IntentsBitField } = require('discord.js');
const axios = require('axios');
const client = new Client({ 
  intents: [
    IntentsBitField.Flags.Guilds, 
    IntentsBitField.Flags.GuildMessages
  ] 
});
const prefix = '!';

// Bot就绪事件
client.once('ready', () => {
  console.log(`Logged in as ${client.user.tag}`);
});

client.on('messageCreate', async (message) => {
  // 忽略机器人消息和非前缀开头的消息
  if (message.author.bot || !message.content.startsWith(prefix)) return;

  // 提取命令和参数
  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const command = args.shift().toLowerCase();

  if (command === 'hackathon') {
    const gitbookURL = args[0];

    if (!gitbookURL || !gitbookURL.startsWith('LINK I\'ll ADD LATER')) {
      message.reply('请提供有效的指南URL。');
      return;
    }

    try {
      // 请求GitBook内容
      const response = await axios.get(gitbookURL);
      const sections = response.data.sections || [];
      const query = args.join(' ').toLowerCase();

      // 查找匹配的章节
      let matchingSection = null;
      sections.forEach((section) => {
        if (section.content.toLowerCase().includes(query)) {
          matchingSection = section;
        }
      });

      if (matchingSection) {
        message.reply(`以下是你请求的信息:\n标题:${matchingSection.title}\n链接:${gitbookURL}`);
      } else {
        message.reply('抱歉,在这份指南中没有找到与你的查询相关的信息。');
      }
    } catch (error) {
      console.error('获取指南时出错:', error);
      message.reply('获取指南时发生了错误。');
    }
  }
});

client.login('你的TOKEN');

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 20:55:39