You need to enable JavaScript to run this app. discord.js补全intents后启动报Cannot read property 'Collection' of undefined求助
优惠活动
大模型
产品
解决方案
定价
更多

discord.js补全intents后启动报Cannot read property 'Collection' of undefined求助

错误原因

你当前的核心代码第一行仅从discord.js中解构导入了Client和Intents两个模块,没有导入完整的Discord命名空间对象,但后续代码中多次调用new Discord.Collection(),此时Discord变量属于未定义状态,自然无法读取其下的Collection属性。


两种修复方案任选其一即可

方案一:导入完整Discord对象

替换原有第一行的导入语句即可,后续所有Discord.xxx调用无需修改:

const Discord = require("discord.js");
const bot = new Discord.Client({ 
  intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES] 
});

方案二:单独解构导入Collection

  1. 首行导入新增Collection模块:
const { Client, Intents, Collection } = require("discord.js");
  1. 全局替换所有new Discord.Collection()为new Collection(),一共3处:
  • bot.commands = new Collection();
  • bot.cooldowns = new Collection();
  • cooldowns.set(command.name, new Collection());

额外注意事项

你使用的是discord.js v13及以上版本,原有message事件已经被废弃,建议将bot.on("message", async (message) => {修改为bot.on("messageCreate", async (message) => {,避免后续功能失效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 10:48:04