You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人开发报错:无法读取undefined的'InteractionCreate'属性

解决Discord机器人报错:TypeError: Cannot read properties of undefined (reading 'InteractionCreate')

错误原因

你代码里的写法和当前安装的Discord.js版本不匹配。Events.InteractionCreate是Discord.js v14的枚举事件名,但你的代码用了v13的Intents.FLAGS写法,说明你装的是v13版本——v13里根本没有Events这个枚举对象,自然找不到InteractionCreate属性,所以触发报错。

两种解决办法

办法一:继续用Discord.js v13(无需升级)

直接把事件名换成v13要求的字符串格式:
将代码里的这一行:

client.on(Events.InteractionCreate, async interaction => {

修改为:

client.on("interactionCreate", async interaction => {

另外注意代码最后一行的config. token多了个空格,要改成config.token,否则会找不到机器人令牌。

办法二:升级到Discord.js v14(推荐,官方最新稳定版)

  1. 打开终端,先卸载旧版本,再安装v14:
npm uninstall discord.js && npm install discord.js
  1. 修正Intents的写法,把v13的Intents.FLAGS替换为v14的GatewayIntentBits:
    原客户端初始化代码:
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS] });

改为:

const client = new Client({ 
  intents: [
    GatewayIntentBits.Guilds, 
    GatewayIntentBits.GuildMessages, 
    GatewayIntentBits.GuildMembers
  ] 
});
  1. 如果你用到了MessageEmbed,v14里它改名为EmbedBuilder,需要把导入语句里的MessageEmbed换成EmbedBuilder,使用时也改成new EmbedBuilder()。
  2. 其他事件名统一用Events枚举,比如原来的client.on("ready", ...)可以改成client.on(Events.ClientReady, ...),保持写法一致。

额外检查

  • 确保events文件夹里没有重复的interactionCreate事件文件,避免逻辑冲突。
  • 确认commands文件夹里的每个命令文件都包含data和execute属性,否则会触发警告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 20:15:33