discord.js报错:Module 'discord.js' 无导出成员'Intents'求助
解决Discord.js报错:Module 'discord.js' has no exported member 'Intents'
这个错误的核心原因是discord.js版本不同,Intents的导入和使用方式存在差异,同时你的代码缺少必要的导入语句和环境变量加载步骤,以下分版本给出修复方案:
版本1:discord.js v14及以上
v14开始移除了Intents导出,改用GatewayIntentBits,同时意图常量的命名规则改为驼峰复数形式:
import dotenv from 'dotenv'; import { Client, GatewayIntentBits } from 'discord.js'; // 加载.env文件中的环境变量 dotenv.config(); const bot = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages ] }); bot.on('ready', ()=>{ console.log("ready") }) bot.login(process.env.TOKEN)
版本2:discord.js v13及以下
v13及以下仍使用Intents,但需要确保正确导入,同时注意意图常量的复数命名:
import dotenv from 'dotenv'; import { Client, Intents } from 'discord.js'; // 加载.env文件中的环境变量 dotenv.config(); const bot = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES // 原代码少了末尾的s,需修正 ] }); bot.on('ready', ()=>{ console.log("ready") }) bot.login(process.env.TOKEN)
额外注意事项
- 执行
npm list discord.js命令可查看当前使用的discord.js版本,匹配对应方案 - 必须调用
dotenv.config(),否则无法读取.env文件中的TOKEN,导致登录失败
内容的提问来源于stack exchange,提问作者Eros
相关产品推荐
相关产品推荐

