From 1b013581ded6119743f4dca50d2727fa4272b2f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hor=C3=A1=C4=8Dek?= Date: Thu, 9 Apr 2020 09:54:18 +0200 Subject: [PATCH] music --- commands/play.js | 20 ++++++++++ helpers/music.js | 97 +++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 6 +-- 3 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 commands/play.js diff --git a/commands/play.js b/commands/play.js new file mode 100644 index 0000000..a091860 --- /dev/null +++ b/commands/play.js @@ -0,0 +1,20 @@ +exports.conf = { + enabled: true, + guildOnly: false, + aliases: [], + permLevel: 'User', + requiredPerms: [], + cooldown: 2000 +} + +exports.help = { + name: 'play', + category: 'Music', + description: 'Plays or adds to queue requested music.', + usage: 'play [query]', + params: '[query] - A query to find video by or a link to the video.' +} + +exports.run = async (client, message, args, level, data) => { + client.music.play(message, args[0]); +} \ No newline at end of file diff --git a/helpers/music.js b/helpers/music.js index 0d40e90..510e0be 100644 --- a/helpers/music.js +++ b/helpers/music.js @@ -5,7 +5,12 @@ const getYoutubeId = require('get-youtube-id') const fetch = require('node-fetch') */ +const ytdl = require('ytdl-core-discord'); +const fetch = require('node-fetch'); + module.exports = client => { + client.music = {guilds: {}}; + // MUSIC - TIMESTAMP client.createTimestamp = function (duration) { var hrs = ~~(duration / 60 / 60) @@ -31,4 +36,96 @@ module.exports = client => { var time = hrs + min + ':' + sec return time } + + client.music.getGuild = function(id) { + let guild = client.music.guilds[id]; + + if(!guild) { + guild = {}; + + guild.dispatcher = null; + guild.playing = false; + guild.queue = []; + + client.music.guilds[id] = guild; + }; + + return guild; + }; + + client.music.isYouTubeLink = function(query) { + return query.startsWith('https://youtube.com/') || query.startsWith('http://youtube.com/') || query.startsWith('https://youtu.be/') || query.startswith('http://youtu.be/') || query.startsWith('https://m.youtube.com/') || query.startsWith('http://m.youtube.com/') || query.startsWith('https://www.youtube.com/') || query.startsWith('http://www.youtube.com/'); + }; + + client.music.getLinkFromID = function(id) { + return 'https://www.youtube.com/watch?v=' + id; + }; + + client.music.getVideoByQuery = async function(query) { + let isLink = client.music.isYouTubeLink(query); + + let response; + + if(isLink) { + response = await fetch('https://www.googleapis.com/youtube/v3/search?key=' + client.config.keys.yt + '&part=id,snippet&maxResults=1&id=' + id); + } else { + response = await fetch('https://www.googleapis.com/youtube/v3/search?key=' + client.config.keys.yt + '&part=id,snippet&maxResults=1&q=' + encodeURIComponent(query)); + }; + + let json = await response.json(); + + let parsed = JSON.parse(json); + + if(parsed.items) { + let video = parsed.items[0]; + + if(video) { + return video; + } else { + return false; + }; + } else { + return false; + }; + }; + + client.music.play = async function(message, query) { + let guild = client.music.getGuild(message.guild.id); + + if(!message.member.voice.channel && !guild.voiceChannel) { + return message.member.reply('you are not in a voice channel!'); + } + + let vc = message.member.voice.channel; + + let video = client.music.getVideoByQuery(query); + + if(video) { + // Fix the bot if somehow broken + // music "playing", nothing in queue + if((guild.playing || guild.dispatcher) && guild.queue.length == 0) { + guild.playing = false; + guild.dispatcher = null; + // music not playing, something is in queue + } else if(!guild.playing && !guild.dispatcher && guild.queue.length > 0) { + guild.queue = []; + }; + + // Add video to queue + guild.queue.push({video: video, requestedBy: message.member.id}); + + // Figure out if the bot should add it to queue or play it right now + if(guild.playing) { + + } else { + guild.playing = true; + + let connection = await vc.join(); + guild.dispatcher = connection.play(await ytdl(client.music.getLinkFromID(guild.queue[0].id.videoId)), {type: 'opus'}); + guild.dispatcher.setVolume(0.5); + }; + } else { + return message.member.reply('failed to find the video!'); + }; + }; } diff --git a/package-lock.json b/package-lock.json index 8447144..0ae0fc1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -276,9 +276,9 @@ "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==" }, "@types/node": { - "version": "13.9.8", - "resolved": "https://registry.npmjs.org/@types/node/-/node-13.9.8.tgz", - "integrity": "sha512-1WgO8hsyHynlx7nhP1kr0OFzsgKz5XDQL+Lfc3b1Q3qIln/n8cKD4m09NJ0+P1Rq7Zgnc7N0+SsMnoD1rEb0kA==" + "version": "13.11.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.11.1.tgz", + "integrity": "sha512-eWQGP3qtxwL8FGneRrC5DwrJLGN4/dH1clNTuLfN81HCrxVtxRjygDTUoZJ5ASlDEeo0ppYFQjQIlXhtXpOn6g==" }, "abort-controller": { "version": "3.0.0",