Ajitesh Irij
Ajitesh Irij

Reputation: 51

Discord.js - How to play a video from a bot?

I have successfully played a audio from a youtube link, now i am trying to play both the audio and the video from the bot, is it possible? In discord, you get a option to share your video, so I thought it can be possible.

I searched on discord.js and found no documentation related to starting video and stuffs. So, How can you play both audio and video with discord.js

My code of playing audio :-

const ytdl = require('ytdl-core');

module.exports = {
    name: 'play_music',
    description: "Plays the music",
    async execute(message, args){
        const memberInGroup = [];
        let musicChannel;

        let fullArg = '';
        args.forEach(part => {
            fullArg = fullArg + part + ' ';
        });
        fullArg = fullArg.trimEnd();

        const validate = ytdl.validateURL(fullArg);
        
        if(!validate) {
            message.channel.send("Plzz enter a valid Youtube Url");
        } else {

        const songInfo = await ytdl.getInfo(fullArg);
        console.log(songInfo.videoDetails.video_url);

        const channels = message.guild.channels.cache.filter(c => c.id === 'myChannelId' && c.type === 'voice');
        for (const [channelID, channel] of channels) {
            musicChannel = channel;
            for (const [memberID, member] of channel.members) {
              memberInGroup.push(memberID);
            }
        }

        if(!memberInGroup.includes(message.author.id)) {
            message.channel.send("You must be in the music group.");
        } else {
            message.channel.send("Playing music");
            musicChannel.join().then(connection => {
                const dispatcher = connection.play(ytdl(songInfo.videoDetails.video_url))
                    
                dispatcher.on('finish', () => {
                    connection.disconnect();
                })
            })
        }
    }

    }
}

Upvotes: 1

Views: 5631

Answers (2)

Montagist
Montagist

Reputation: 401

You would have to use a "self-bot" which uses a user-token instead of a developer portal-generated bot-token and is a against the terms of service.

There is still some useful functionality you can provide, however. You can still:

  • as you mentioned already, stream the audio

  • still upload/post the video from Youtube Downloader to the channel, thus avoiding ad$ for people in your server

  • spin up a local webserver - or remotely control an already running one - to stream the video after you've Youtube Downloader'd there. You'd have to give the users a link to this server but it wouldn't take much glue code or frontend code to make this work. That downside is outweighed by the fact that there'd be no lag, which RTM to Discord definitely introduces, and you'd even be able to sync everyone's player easily.

Upvotes: 1

Skerrepy
Skerrepy

Reputation: 392

I don't have an experience with discord js but from what i have seen in the bots that were made the only thing that you can play is audio you can send a link to youtube via your bot and users will play them thats the only solution.

Upvotes: 1

Related Questions