开发Discord Bot时遭遇GatewayIntentBits未定义错误求助
解决Discord Bot中"GatewayIntentBits is not defined"错误
错误原因
你使用的是Discord.js v14及以上版本,GatewayIntentBits是Discord.js导出的核心对象,但你的代码没有正确引用它——要么没从Discord.js中提取该对象,要么没通过Discord前缀访问。
修复方案
方案1:通过Discord对象直接访问
修改客户端初始化代码,给GatewayIntentBits加上Discord.前缀即可:
const client = new Discord.Client({ intents: [ Discord.GatewayIntentBits.Guilds // 添加Discord.前缀 ] })
方案2:解构导入更简洁
将开头的Discord.js导入语句改为解构形式,直接提取需要的对象,后续代码可以直接使用GatewayIntentBits,同时简化Collection的调用:
const { Client, GatewayIntentBits, Collection } = require("discord.js") require("dotenv").config() // 客户端初始化无需前缀 const client = new Client({ intents: [ GatewayIntentBits.Guilds ] }) // slashcommands定义也无需Discord.前缀 client.slashcommands = new Collection()
额外修复:变量名不一致问题
你的代码里定义了const Guilds = "1079351793550643352",但在ready事件中调用的是guildId,这会导致找不到目标服务器。需要统一变量名:
// 将Guilds改为guildId const guildId = "1079351793550643352"
内容的提问来源于stack exchange,提问作者Barnaby
相关产品推荐
相关产品推荐

