Discord.js v13, @discordjs/voice Play Music Command

Viewed 2899

This is my code, The command executes perfectly, The bot joins the voice channel and also sends the name of the song its about to play, but it doesnt play the song in the voice channel. This is my first time ever asking a question on stackoverflow so dont mind the format and stuff. But I really need help here. Discord v13 and latest node module.

const ytsearch = require('yt-search');
const Discord = require('discord.js')
const {
    joinVoiceChannel,
    createAudioPlayer,
    createAudioResource,
    NoSubscriberBehavior
} = require('@discordjs/voice');


module.exports = {
    name: "play",
    description: "test command",

    async run(client, message, args) {

        const voiceChannel = message.member.voice.channel;
        if (!voiceChannel) return message.channel.send('Please connect to a voice channel!');
        if (!args.length) return message.channel.send('Please Provide Something To Play!')

        const connection = await joinVoiceChannel({
            channelId: message.member.voice.channel.id,
            guildId: message.guild.id,
            adapterCreator: message.guild.voiceAdapterCreator
        });
        const videoFinder = async (query) => {
            const videoResult = await ytsearch(query);
            return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;


        }
        const video = await videoFinder(args.join(' '));
        if (video) {
            const stream = ytdl(video.url, { filter: 'audioonly' });
            const player = createAudioPlayer();
            const resource = createAudioResource(stream)

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




            await message.reply(`:thumbsup: Now Playing ***${video.title}***`)
            
        } else {
            message.channel.send('No video results found');
        }



    }
}``` 
1 Answers

I would suggest you look at the music bot example at @discordjs/voice.

They do a good job of how to extract the stream from ytdl.

I'm currently still learning how this all works but the part that you will want to look at is in the createAudioResource function.

public createAudioResource(): Promise<AudioResource<Track>> {
    return new Promise((resolve, reject) => {
        const process = ytdl(
            this.url,
            {
                o: '-',
                q: '',
                f: 'bestaudio[ext=webm+acodec=opus+asr=48000]/bestaudio',
                r: '100K',
            },
            { stdio: ['ignore', 'pipe', 'ignore'] },
        );
        if (!process.stdout) {
            reject(new Error('No stdout'));
            return;
        }
        const stream = process.stdout;
        const onError = (error: Error) => {
            if (!process.killed) process.kill();
            stream.resume();
            reject(error);
        };
        process
            .once('spawn', () => {
                demuxProbe(stream)
                    .then((probe) => resolve(createAudioResource(probe.stream, { metadata: this, inputType: probe.type })))
                    .catch(onError);
            })
            .catch(onError);
    });
}
Related