0

这是迄今为止我的不和谐音乐机器人的代码。我相信该机器人具有适当的权限,并且能够加入调用“!play”命令的人的语音频道。我遇到的问题是我似乎无法弄清楚为什么音频流卡在“缓冲”状态。文档指出“缓冲”状态应该通过或失败,但它似乎被卡住了。

client.on("messageCreate", async message => {
    if (message.content.slice(0, 6) == "!play ") {
        // check input to see if it is a youtube URL
        let input = message.content.slice(6);
        if (isYoutubeUrl(input)) {
            url = input;
        } else {
            formatInput(input);
            url = await searchVideo(searchTerm);
        }
        if (message.member.voice.channelId !== null) {
            const permissions = message.member.voice.channel.permissionsFor(message.client.user);
            if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
              return message.channel.send(
                "I need the permissions to join and speak in your voice channel!"
              );
            }

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

            const stream = await ytdl(url, { filter:'audioonly' });
        
            const player = createAudioPlayer();
            var resource = createAudioResource(stream, { seek: 0, volume: 1 });
            player.play(resource);
            connection.subscribe(player);

        } else {
            message.reply("You need to be in a voice channel to play music!");
        }
    }
})

这是我 console.log 播放器本身时出现的情况

<ref *1> AudioPlayer {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  subscribers: [
    PlayerSubscription {
      connection: [VoiceConnection],
      player: [Circular *1]
    }
  ],
  _state: {
    status: 'buffering',
    resource: AudioResource {
      playbackDuration: 0,
      started: false,
      silenceRemaining: -1,
      edges: [Array],
      playStream: [OggDemuxer],
      metadata: null,
      silencePaddingFrames: 5,
      audioPlayer: [Circular *1]
    },
    onReadableCallback: [Function: onReadableCallback],
    onFailureCallback: [Function: onFailureCallback],
    onStreamError: [Function: onStreamError]
  },
  behaviors: { noSubscriber: 'pause', maxMissedFrames: 5 },
  debug: [Function (anonymous)],
  [Symbol(kCapture)]: false
}
4

1 回答 1

0

事实证明,流不是问题。我只需要使用“@discordjs/voice”包中的“getVoiceConnection”组件并将流广播到该连接。

于 2021-09-20T09:06:57.260 回答