From c3b6af4db3bba79a31af0bb1a6b4db38dfe57df4 Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Thu, 11 Mar 2021 22:01:49 +0530 Subject: New Command Added * Play/Stop Music --- src/main.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'src/main.js') 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"); -- cgit v1.2.3