diff options
author | Indrajith K L | 2021-03-11 22:01:49 +0530 |
---|---|---|
committer | Indrajith K L | 2021-03-11 22:01:49 +0530 |
commit | c3b6af4db3bba79a31af0bb1a6b4db38dfe57df4 (patch) | |
tree | 5df1f645bacf913a8a70a092f83200c09b56f2c4 /src | |
parent | beab08570bf52b1903f9ee9b3ef05a88f3774b44 (diff) | |
download | lul-bot-c3b6af4db3bba79a31af0bb1a6b4db38dfe57df4.tar.gz lul-bot-c3b6af4db3bba79a31af0bb1a6b4db38dfe57df4.tar.bz2 lul-bot-c3b6af4db3bba79a31af0bb1a6b4db38dfe57df4.zip |
New Command Added
* Play/Stop Music
Diffstat (limited to 'src')
-rw-r--r-- | src/main.js | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/src/main.js b/src/main.js index 9eeb182..d9f36d4 100644 --- a/src/main.js +++ b/src/main.js @@ -1,7 +1,9 @@ require('dotenv').config(); - const { Client, MessageEmbed, Collection } = require('discord.js'); const fetch = require('node-fetch'); +const ytdl = require('ytdl-core'); +const ytSearch = require('yt-search'); + const client = new Client(); const CMD_PREFIX = "!" client.on('message', (message) => { @@ -66,6 +68,8 @@ function parseCMD(message) { .split(/\s+/); switch (CMD_NAME) { case 'jokes': randomJokes(message); return {}; + case 'play' : playMusik(message, args); return {}; + case 'stop' : stopMusik(message); return {}; default: return null; } } @@ -89,6 +93,51 @@ function randomJokes(message) { }) } +async function playMusik(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(); + const video = await searchVideo(args.join(' ')); + + if(video) { + const youtubeStream = ytdl(video.url, {filter: 'audioonly'}); + connection.play(youtubeStream, {seek: 0, volume: 1}) + .on('finish', () => { + voiceChannel.leave(); + }); + await message.reply(`Now Playing ${video.title}...`); + } else { + message.channel.send("No music found to play for you mate. Try again! 👍"); + } +} + +async function stopMusik(message) { + const voiceChannel = message.member.voice.channel; + + if (!voiceChannel) { + return message.channel.send("Join a voice channel to Execute this command"); + } + + await voiceChannel.leave(); + await message.channel.send("Stoping Music... Baye Baye.... 😅"); +} + +async function searchVideo(query) { + const searchResult = await ytSearch(query); + return (searchResult.videos.length > 1) ? searchResult.videos[0] : null; +} + client.login(process.env.LUL_BOT_TKN) .then(() => { console.log("BOT Logged in"); |