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
- 首行导入新增Collection模块:
const { Client, Intents, Collection } = require("discord.js");
- 全局替换所有
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
相关产品推荐
相关产品推荐

