aboutsummaryrefslogtreecommitdiff
path: root/src/commands/play.js
diff options
context:
space:
mode:
authorIndrajith K L2021-03-27 21:57:16 +0530
committerIndrajith K L2021-03-27 21:57:16 +0530
commite3dba82cf3cdbdf5c0aad3d308013695a2074ecf (patch)
tree83b1b0f5f968ca3f4c260a068fe364821e68176a /src/commands/play.js
parent6048206733b3d6bb9ffbacb3e54f4bef88777585 (diff)
downloadlul-bot-e3dba82cf3cdbdf5c0aad3d308013695a2074ecf.tar.gz
lul-bot-e3dba82cf3cdbdf5c0aad3d308013695a2074ecf.tar.bz2
lul-bot-e3dba82cf3cdbdf5c0aad3d308013695a2074ecf.zip
Code Refactoring
* Adds Config for commands * Moved Casual Messages to language.json
Diffstat (limited to 'src/commands/play.js')
-rw-r--r--src/commands/play.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/commands/play.js b/src/commands/play.js
new file mode 100644
index 0000000..24e4fcd
--- /dev/null
+++ b/src/commands/play.js
@@ -0,0 +1,44 @@
+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();
+ const video = await searchVideo(args.join(' '));
+
+ if (video) {
+ const youtubeStream = ytdl(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 ${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);
+ }
+} \ No newline at end of file