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

discord.js v14中Cannot read properties of undefined(reading 'GUILDS')错误求助

解决discord.js v14.11.0中TypeError: Cannot read properties of undefined (reading 'GUILDS')错误

错误原因

discord.js v14版本重构了权限意图的结构,GatewayIntentBits不再包含FLAGS子属性。你代码中直接访问GatewayIntentBits.FLAGS.GUILDS时,FLAGS是未定义的,因此触发了该类型错误。

修复方案

将客户端初始化代码中的GatewayIntentBits.FLAGS.*改为直接使用GatewayIntentBits.*,具体修改如下:

错误代码行

const client = new Client({ intents: [GatewayIntentBits.FLAGS.GUILDS, GatewayIntentBits.FLAGS.GUILD_MESSAGES, GatewayIntentBits.FLAGS.GUILD_MEMBERS] });

修正后代码行

const client = new Client({ intents: [GatewayIntentBits.GUILDS, GatewayIntentBits.GUILD_MESSAGES, GatewayIntentBits.GUILD_MEMBERS] });

额外优化建议

  1. 添加必要的消息内容权限:v14中GUILD_MESSAGES意图仅能监听消息事件,若需要读取message.content,必须同时添加GatewayIntentBits.MESSAGE_CONTENT意图,否则消息内容会为空。
  2. 清理冗余导入:代码中同时导入了整个Discord模块和解构的Client等对象,可删除const Discord = require('discord.js');以减少内存占用。
  3. 优化异步逻辑:原代码中使用forEach配合async/await会导致并发执行问题,建议改用for...of循环确保顺序执行。

完整修正后的代码示例

const { Client, MessageEmbed, GatewayIntentBits } = require('discord.js');
require('dotenv').config();

const client = new Client({ 
  intents: [
    GatewayIntentBits.GUILDS, 
    GatewayIntentBits.GUILD_MESSAGES, 
    GatewayIntentBits.GUILD_MEMBERS,
    GatewayIntentBits.MESSAGE_CONTENT
  ] 
});

client.on('ready', () => {
    console.log(`${client.user.tag} olarak giriş yaptiniz!`);
});

client.on('messageCreate', async (message) => {
  if(message.content == '!massiveunban') {
    const guild = client.guilds.cache.get('Cencored');
    try { 
      const bans = await guild.bans.fetch();
      const ids = bans.map(u => u.user.id);
      
      for (const id of ids) {
        try {
          const user = await guild.members.unban(id, 'Temizlik');
          console.log(`Engeli acildi: ${user.tag}`);
        } catch (banError) {
          console.error(`Engeli açma hatası (ID: ${id}):`, banError);
        }
      }
    } catch (error) {
      console.error(error);
    }
  }
});

client.login(process.env.TOKEN);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 12:14:58