discord.js v13开发Bot时出现Collection/Discord未定义报错如何解决
discord.js v13 Collection未定义报错修复方案
- 错误原因:你使用解构导入从discord.js引入模块时,没有将
Collection加入导入列表,因此直接调用Collection或通过未定义的Discord对象调用都会触发ReferenceError。 - 此前你尝试将导入变量命名为
Discord仍报错,是因为你用的是解构导入语法,而非将整个discord.js模块对象赋值给Discord变量,解构出来的对象中不包含Collection属性。
方案1:继续使用解构导入
直接在第一行的解构列表中加入Collection即可:
// 新增Collection到导入列表 const { Client, Intents, DiscordAPIError, CommandInteractionOptionResolver, Collection } = require("discord.js"); const bot = new Client({ intents: ["DIRECT_MESSAGES", "GUILDS", "GUILD_MESSAGES"] }); const fs = require("fs") bot.Commands = new Collection();
注意:discord.js v13接收消息需要额外在Discord开发者后台的Bot设置页面开启
Message Content Intent权限,否则无法正常读取消息内容。
方案2:沿用旧教程的全局导入写法
如果你想和教程的代码格式对齐,可以直接导入整个discord.js模块对象到Discord变量,同时补充v13要求的intents配置:
const Discord = require('discord.js') const bot = new Discord.Client({ intents: [ Discord.Intents.FLAGS.DIRECT_MESSAGES, Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES ] }); const fs = require("fs") bot.commands = new Discord.Collection();
内容的提问来源于stack exchange,提问作者Ocean
相关产品推荐
相关产品推荐

