使用discord-player开发Discord音乐机器人时遇TypeError报错求助
Discord-Player v6.1.0 导入报错:TypeError: Cannot read properties of undefined (reading 'includes')
问题详情
我在开发Discord音乐机器人时使用discord-player库,启动机器人时触发TypeError,报错指向bot.js文件中的discord-player导入行。当前使用discord.js v14.2.0,导入代码如下:
const { Player } = require("discord-player");
错误信息
C:\Users\Darx\projects\haubot\node_modules\discord-player\dist\index.js:2095 if (import_voice4.version.includes("-dev") || import_voice4.version.startsWith("1")) ^ TypeError: Cannot read properties of undefined (reading 'includes') at C:\Users\Darx\projects\haubot\node_modules\discord-player\dist\index.js:2095:29 at Object.<anonymous> (C:\Users\Darx\projects\haubot\node_modules\discord-player\dist\index.js:2099:3) at Module._compile (node:internal/modules/cjs/loader:1120:14) at Module._extensions..js (node:internal/modules/cjs/loader:1174:10) at Module.load (node:internal/modules/cjs/loader:998:32) at Module._load (node:internal/modules/cjs/loader:839:12) at Module.require (node:internal/modules/cjs/loader:1022:19) at require (node:internal/modules/cjs/helpers:102:18) at Object.<anonymous> (C:\Users\Darx\haubot\bot.js:9:20) at Module._compile (node:internal/modules/cjs/loader:1120:14) Node.js v18.6.0
项目依赖
"@discordjs/builders": "^0.15.0", "@discordjs/opus": "^0.8.0", "@discordjs/rest": "^0.5.0", "@discordjs/voice": "^0.10.0", "discord-api-types": "^0.34.0", "discord-player": "^6.1.0", "discord.js": "^14.2.0", "dotenv": "^16.0.1", "ffmpeg-static": "^5.0.2", "libsodium-wrappers": "^0.7.10", "nodemon": "^2.0.19", "yt-search": "^2.10.4", "ytdl": "^1.4.1", "ytdl-core": "^4.11.2"
解决方案
原因:discord-player v6.x 对@discordjs/voice版本有严格兼容要求,你当前使用的@discordjs/voice v0.10.0与discord-player v6.1.0不匹配,导致库无法正确读取voice模块的version属性,触发报错。
修复步骤
- 卸载当前不兼容的
@discordjs/voice版本:npm uninstall @discordjs/voice - 安装与discord-player v6.x兼容的
@discordjs/voice版本(推荐v0.13.x系列):npm install @discordjs/voice@^0.13.0 - (可选)若仍有问题,清理依赖缓存后重新安装所有依赖:
rm -rf node_modules package-lock.json npm install
或者直接重新安装discord-player,让它自动拉取适配的依赖版本:
npm uninstall discord-player npm install discord-player@^6.1.0
内容的提问来源于stack exchange,提问作者oyraddd
相关产品推荐
相关产品推荐

