使用Discord.js交互时触发TypeError: Cannot read properties of undefined (reading 'reduce')
解决Discord.js交互功能报错:Cannot read properties of undefined (reading 'reduce')
错误信息(中文翻译)
未捕获的类型错误 TypeError: 无法读取undefined的属性'reduce' 在 BaseInteraction (node_modules\discord.js\src\structures\BaseInteraction.js:106:43) 在 MessageComponentInteraction (node_modules\discord.js\src\structures\MessageComponentInteraction.js:17:5) 在 ButtonInteraction (node_modules\discord.js\src\structures\ButtonInteraction.js:9:1) 在 handle (node_modules\discord.js\src\client\actions\InteractionCreate.js:90:25) 在 module.exports (node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36) 在 handlePacket (node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31) 在 <anonymous> (node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12) 在 emit (node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:282:31) 在 <anonymous> (node_modules\@discordjs\ws\dist\index.js:1190:51) 在 emit (node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:282:31) 在 onMessage (node_modules\@discordjs\ws\dist\index.js:1005:14) 在 processTicksAndRejections (internal/process/task_queues:95:5)
解决方案
- 匹配Discord.js版本:这个报错核心原因是代码逻辑与安装的Discord.js版本不兼容。比如用了v14的交互写法却装了v13,直接安装对应版本即可:
- 安装最新稳定版(v14):
npm install discord.js@latest - 如需v13版本:
npm install discord.js@13.17.1
- 安装最新稳定版(v14):
- 重置依赖环境:依赖缓存或版本冲突也会引发这类问题,执行以下步骤:
- 删除本地的
node_modules文件夹和package-lock.json文件 - 重新安装所有依赖:
npm install
- 删除本地的
- 检查交互注册逻辑:确保按钮、选择菜单等交互的注册方式符合当前版本要求,比如v14需通过
REST和Routes注册,v13则有不同的注册流程,混用会导致交互数据解析异常。
内容的提问来源于stack exchange,提问作者Elitezen
相关产品推荐
相关产品推荐

