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

discord.js v13音频/音乐机器人出现MODULE_NOT_FOUND错误如何解决

问题排查与修复方案

存在的错误点:

  • args未定义:你直接调用args[0]获取链接,但没有提前拆分消息内容生成参数数组,会直接抛出args is not defined报错
  • 消息匹配逻辑错误:你用message.content == 'goplay'做判断,用户发送带链接的指令(如goplay 视频链接)时根本不会触发后续逻辑,同时也拿不到链接参数
  • joinVoiceChannel参数错误:guildId字段你错误传入了整个guild对象,需要传入服务器ID字符串
  • 缺少用户语音状态校验:没有判断触发指令的用户是否加入了语音频道,若用户未进频道调用channel.id会直接报错
  • 音频流配置缺失:ytdl输出的原始流直接传入createAudioResource可能出现格式不兼容问题,需补充流类型配置
  • 依赖引用不规范:discordjs/voice包的正确引用名是@discordjs/voice,需补充前缀

修正后代码:

const ytdl = require('ytdl-core');
const { createAudioPlayer, createAudioResource, joinVoiceChannel, StreamType } = require('@discordjs/voice');

console.log("is this working");

// PLAYER
client.on("messageCreate", async message => {
  // 忽略机器人自身消息避免循环触发
  if (message.author.bot) return;
  // 匹配goplay开头的指令
  if (!message.content.startsWith('goplay')) return;
  // 拆分消息内容生成参数数组
  const args = message.content.split(' ').slice(1);
  const url = args[0];
  if(!url) return message.channel.send('no url');

  // 校验用户是否处于语音频道
  const channel = message.member.voice.channel;
  if (!channel) return message.channel.send('你需要先加入一个语音频道');

  const stream = ytdl(url, { 
    filter: 'audioonly',
    highWaterMark: 1 << 25
  });

  const player = createAudioPlayer();
  const resource = createAudioResource(stream, {
    inputType: StreamType.Arbitrary
  });

  const connection = joinVoiceChannel({
    channelId: channel.id,
    guildId: message.guild.id,
    adapterCreator: message.guild.voiceAdapterCreator,
  });

  player.play(resource)
  connection.subscribe(player);
});

额外注意事项

  • 确保你已经在Discord开发者后台给机器人开启了GUILD_VOICE_STATES等必需的网关意图
  • 确保你安装的@discordjs/voice版本和discord.js v13版本匹配
  • 确保你的运行环境安装了ffmpeg相关依赖,否则音频转码会失败

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 18:36:06