使用DisTube跳过歌曲时触发CANNOT_RESOLVE_SONG错误的问题排查
解决DisTube跳过歌曲时出现CANNOT_RESOLVE_SONG错误的问题
你遇到的CANNOT_RESOLVE_SONG错误,核心原因有两个:
- 手动创建
Song对象的方式不符合DisTube内部规范,导致后续跳过操作时无法正确识别该歌曲 - 使用了DisTube未文档化的
addToQueue私有方法,其参数处理逻辑和官方API不一致,会导致队列中的歌曲数据异常
修正添加队列的代码
不要手动实例化Song,直接使用DisTube官方提供的addQueue方法(注意是addQueue而非你用的addToQueue),或者在已有播放队列时用play方法并指定参数来添加到队列。
修正后的添加队列函数示例:
[...] const song = event.options.getString("songname"); const voice = event.member.voice.channel; if (!voice) { event.reply({content: "You have to be in a voice to do this!", ephemeral: true}); return; // 终止后续代码执行 } // 用await简化异步逻辑,需将外层函数声明为async const res = await event.client.DisTube.search(song, {limit: 5, type: "video"}); if (!res.length) { event.reply({content: "No results found!", ephemeral: true}); return; } const queue = event.client.DisTube.getQueue(event.guild); if (queue) { // 已有队列,将歌曲添加到队尾(第二个参数可指定插入位置) await queue.addQueue(res[0]); } else { // 无队列,直接开始播放 await event.client.DisTube.play(voice, res[0]); } [...]
跳过函数的优化
你的跳过函数逻辑本身没问题,但建议增加错误捕获,避免未处理的异常:
[...] try { const skippedSong = await event.client.DisTube.skip(event.guild); console.log(skippedSong.name); } catch (err) { event.reply({content: err.message || "Failed to skip song!", ephemeral: true}); } [...]
关键注意事项
- 禁止使用DisTube未文档化的私有方法(比如
addToQueue),这类方法的实现可能随时变更,不保证兼容性 - DisTube的
search返回结果已经是符合要求的Song实例,无需手动创建 - 异步操作必须正确处理(用
await或链式then),避免因Promise未完成导致队列数据异常
内容的提问来源于stack exchange,提问作者Dave
相关产品推荐
相关产品推荐

