New Command Added

* Play/Stop Music
This commit is contained in:
Indrajith K L
2021-03-11 22:01:49 +05:30
parent beab08570b
commit c3b6af4db3
3 changed files with 1140 additions and 3 deletions

1082
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -11,9 +11,15 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@discordjs/opus": "^0.4.0",
"discord.js": "^12.5.1", "discord.js": "^12.5.1",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"node-fetch": "^2.6.1" "ffmpeg-static": "^4.2.7",
"node-fetch": "^2.6.1",
"yt-search": "^2.7.3",
"ytdl-core": "^4.5.0"
}, },
"engines": { "node": "12.16.3" } "engines": {
"node": "12.16.3"
}
} }

View File

@@ -1,7 +1,9 @@
require('dotenv').config(); require('dotenv').config();
const { Client, MessageEmbed, Collection } = require('discord.js'); const { Client, MessageEmbed, Collection } = require('discord.js');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
const client = new Client(); const client = new Client();
const CMD_PREFIX = "!" const CMD_PREFIX = "!"
client.on('message', (message) => { client.on('message', (message) => {
@@ -66,6 +68,8 @@ function parseCMD(message) {
.split(/\s+/); .split(/\s+/);
switch (CMD_NAME) { switch (CMD_NAME) {
case 'jokes': randomJokes(message); return {}; case 'jokes': randomJokes(message); return {};
case 'play' : playMusik(message, args); return {};
case 'stop' : stopMusik(message); return {};
default: return null; 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) client.login(process.env.LUL_BOT_TKN)
.then(() => { .then(() => {
console.log("BOT Logged in"); console.log("BOT Logged in");