aboutsummaryrefslogtreecommitdiff
path: root/src/commands/play.js
blob: 9c2d7a3f37e246b48c0649630474ff001fd0d403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
async function playMusik(client, message, args) {
    const voiceChannel = message.member.voice.channel;
    if (!voiceChannel) {
        return message.channel.send("Join a voice channel to Play Music");
    }
    const permission = voiceChannel.permissionsFor(message.client.user);
    if (!permission.has('CONNECT') || !permission.has('SPEAK')) {
        return message.channel.send("You don't have the permission to play music. 😥");
    }

    if (!args.length) {
        return message.channel.send("Please pass something to play as second argument");
    }

    const connection = await voiceChannel.join();
    let videoUrl = false;
    let video;
    const youtubeRegex = /(http:|https:)?\/\/(www\.)?(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/; 
    if (youtubeRegex.test(args[0])) {
        video = args[0];
        videoUrl = true;
    } else {
        video = await searchVideo(args.join(' '));
    }

    if (video) {
        const youtubeStream = ytdl(videoUrl ? video : video.url, { filter: 'audioonly' });
        client.user.setPresence({ activity: { name: `${video.title}`, type: 'LISTENING' } });
        connection.play(youtubeStream, { seek: 0, volume: 1 })
            .on('finish', () => {
                voiceChannel.leave();
                client.user.setPresence({ activity: { name: ` ` } });
            });
        await message.reply(`Now Playing ${videoUrl ? args[0] : video.title}...`);
    } else {
        message.channel.send("No music found to play for you mate. Try again! 👍");
    }
}

async function searchVideo(query) {
    const searchResult = await ytSearch(query);
    return (searchResult.videos.length > 1) ? searchResult.videos[0] : null;
}


module.exports = {
    execute(client,message, args) {
        playMusik(client,message,args);
    }
}