DiscordJS audio feature not functioning in custom bot

Hey everyone I’m having trouble with my Discord bot. I made it with DiscordJS and I want it to play YouTube videos when you type ‘!music’ plus a video link. But now it’s not working at all. I’ve tried fixing it many times but no luck. The rest of the bot is fine it’s just the music part that’s broken. Here’s my code:

if (msg.startsWith('!play')) {
  let url = args[0]
  const audioStream = require('audio-streamer');
  const audioSettings = { start: 0, loudness: 1 };
  const audioOutput = client.createAudioOutput();

  client.audioChannel.connect()
    .then(voiceConn => {
      const audio = audioStream(args[0], { type: 'audio' });
      audioOutput.streamAudio(audio);
      const player = voiceConn.playAudioOutput(audioOutput);
    })
    .catch(err => console.log(err));
}

Any ideas what might be wrong? Thanks!

I’ve encountered similar issues with DiscordJS audio. The problem might be related to outdated dependencies or incompatible audio libraries. Have you considered using the @discordjs/voice package? It’s specifically designed for voice connections and audio playback in Discord.js v13+.

Here’s a simplified example of how you could implement it:

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

if (message.content.startsWith('!play')) {
  const url = message.content.split(' ')[1];
  const channel = message.member.voice.channel;

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

  const player = createAudioPlayer();
  const resource = createAudioResource(ytdl(url, { filter: 'audioonly' }));

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

Make sure to install the necessary packages. This approach has worked well for me in recent projects.

have u tried using discord-player? it’s way easier 2 setup than custom audio stuff. just install it and use their examples. here’s a quick snippet:

const { Player } = require('discord-player');
const player = new Player(client);

client.on('messageCreate', async msg => {
  if (msg.content.startsWith('!play')) {
    const queue = player.createQueue(msg.guild);
    await queue.connect(msg.member.voice.channel);
    const track = await player.search(msg.content.split(' ')[1], {
      requestedBy: msg.user
    }).then(x => x.tracks[0]);
    queue.play(track);
  }
});

hope this helps!

I ran into a similar issue when building my music bot. From your code, it looks like you’re using some outdated or non-standard audio libraries. I’d recommend switching to the discord-player package - it’s much more reliable and easier to work with for audio streaming.

Here’s a basic setup that worked for me:

const { Player } = require('discord-player');
const player = new Player(client);

client.on('messageCreate', async (message) => {
  if (message.content.startsWith('!play')) {
    const query = message.content.split(' ')[1];
    const queue = player.createQueue(message.guild, {
      metadata: message.channel
    });

    try {
      if (!queue.connection) await queue.connect(message.member.voice.channel);
    } catch {
      queue.destroy();
      return await message.reply('Could not join your voice channel!');
    }

    const track = await player.search(query, {
      requestedBy: message.user
    }).then(x => x.tracks[0]);
    if (!track) return await message.reply('Track not found!');

    queue.play(track);
    return await message.reply(`Loading track **${track.title}**!`);
  }
});

This approach has been much more stable for me. Remember to install discord-player and its dependencies. Hope this helps!