discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.39k stars 3.97k forks source link

Music is not playing in voice channel #10408

Closed iRadioMan closed 3 months ago

iRadioMan commented 3 months ago

Which package is this bug report for?

voice

Issue description

Music is not playing using AudioPlayer, no errors in the ouput, the bot connects to the voice channel and nothing happens. I wrote the code based on the official documentation and it doesn't work. Many other Discord bots don't work either.

  1. Create voice channel connection
  2. Create audio resource
  3. Create audio player
  4. Subscribe connection to audio player
  5. Play audio

Audio file exists, it is accessible and playable with music player.

Code sample

if (message.member.voice.channel) {
  try {
    // connect to voice channel
    const connection = joinVoiceChannel({
      channelId: message.member.voice.channel.id,
      guildId: message.guild.id,
      adapterCreator: message.guild.voiceAdapterCreator,
    });

    // create audio resource and player
    const resource = createAudioResource(join(__dirname, 'file.mp3'));
    const player = createAudioPlayer();

    // catch player errors
    player.on('error', error => {
      console.error('Playback:', error.message);
    });

    // play music
    player.play(resource);
    connection.subscribe(player);

  } catch (err) { 
    console.log("Playback error: " + err);
  }
} else {
  message.reply('You need to join a voice channel first!');
}

Versions

Issue priority

Medium (should be fixed soon)

Which partials do you have configured?

Not applicable

Which gateway intents are you subscribing to?

Guilds, GuildMessages, MessageContent

I have tested this issue on a development release

No response

nyapat commented 3 months ago

What's your opus encoding library? Which package is used for encryption? Have you tried using the alternatives?

I notice you don't say you have the GuildVoiceStates intent but if the bot connects anyways that shouldn't be a problem.

If you see no issues, try listening to the debug event emitted by the audio player. Also you should join the discord for a faster response and are encouraged to do so if it's not an issue with the library.

iRadioMan commented 3 months ago

What's your opus encoding library? Which package is used for encryption? Have you tried using the alternatives?

I notice you don't say you have the GuildVoiceStates intent but if the bot connects anyways that shouldn't be a problem.

If you see no issues, try listening to the debug event emitted by the audio player. Also you should join the discord for a faster response and are encouraged to do so if it's not an issue with the library.

Thank you very much! I didn't add GuildVoiceStates intent because I didn't know it was necessary at all. My bad, sorry. But many other bots, as I said before, don't work. Perhaps because of this. That's why I decided to create my own bot.