1
0
mirror of https://github.com/musix-org/musix-oss synced 2024-11-14 16:00:17 +00:00

Validate url

Validate url for precise song accuracy when using a link to play a song
This commit is contained in:
MatteZ02 2020-04-24 21:35:50 +03:00
parent 6408cbe525
commit cbdc82a655
3 changed files with 103 additions and 88 deletions

View File

@ -1,6 +1,7 @@
const YouTube = require("simple-youtube-api"); const YouTube = require("simple-youtube-api");
const search = require("yt-search");
const SpotifyApi = require("spotify-web-api-node"); const SpotifyApi = require("spotify-web-api-node");
const search = require("yt-search");
const ytdl = require("ytdl-core")
module.exports = { module.exports = {
name: "play", name: "play",
@ -41,10 +42,72 @@ module.exports = {
return msg.channel.send(client.messages.noPermsConnect); return msg.channel.send(client.messages.noPermsConnect);
if (!voiceChannel.speakable) if (!voiceChannel.speakable)
return msg.channel.send(client.messages.noPermsSpeak); return msg.channel.send(client.messages.noPermsSpeak);
if (url.match(/^https?:\/\/(open.spotify.com|spotify.com)(.*)$/)) { if (ytdl.validateURL(url)) {
const song = await ytdl.getInfo(url);
const resource = {
title: song.title,
url: url
}
client.funcs.handleVideo(
resource,
msg,
voiceChannel,
client,
true,
"ytdl"
);
} else if (url.match(/^https?:\/\/(open.spotify.com|spotify.com)(.*)$/)) {
const playlistId = url.split("/playlist/")[1].split("?")[0]; const playlistId = url.split("/playlist/")[1].split("?")[0];
spotify.getPlaylist(playlistId).then( spotify.getPlaylist(playlistId).then(
async function (data) { async function (data) {
searchVideos(data, client, msg, voiceChannel)
},
function (err) {
console.log(err);
msg.channel.send(client.messages.noResultsSpotify);
});
} else if (
url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)
) {
const lmsg = await msg.channel.send(client.messages.loadingSongs);
const playlist = await youtube.getPlaylist(url);
const videos = await playlist.getVideos();
for (const video of Object.values(videos)) {
const video2 = await youtube.getVideoByID(video.id);
await client.funcs.handleVideo(
video2,
msg,
voiceChannel,
client,
true,
"ytdl"
);
}
let message;
message = client.messages.playlistAdded.replace(
"%TITLE%",
playlist.title
);
return lmsg.edit(message);
} else {
ytSearch(searchString, function (err, res) {
if (err) return console.log(err);
if (res.videos.length === 0)
return msg.channel.send(client.messages.noResults);
client.funcs.handleVideo(
res.videos[0],
msg,
voiceChannel,
client,
false,
"ytdl"
);
});
}
},
};
async function searchVideos(data, client, msg, voiceChannel) {
const lmsg = await msg.channel.send(client.messages.loadingSongs); const lmsg = await msg.channel.send(client.messages.loadingSongs);
let failed = 0; let failed = 0;
for (let i = 0; data.body.tracks.items.length > i; i++) { for (let i = 0; data.body.tracks.items.length > i; i++) {
@ -117,49 +180,4 @@ module.exports = {
)}\n${client.messages.failedToLoad + failed}`; )}\n${client.messages.failedToLoad + failed}`;
} }
lmsg.edit(message); lmsg.edit(message);
}, }
function (err) {
console.log(err);
msg.channel.send(client.messages.noResultsSpotify);
}
);
} else if (
url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)
) {
const lmsg = await msg.channel.send(client.messages.loadingSongs);
const playlist = await youtube.getPlaylist(url);
const videos = await playlist.getVideos();
for (const video of Object.values(videos)) {
const video2 = await youtube.getVideoByID(video.id);
await client.funcs.handleVideo(
video2,
msg,
voiceChannel,
client,
true,
"ytdl"
);
}
let message;
message = client.messages.playlistAdded.replace(
"%TITLE%",
playlist.title
);
return lmsg.edit(message);
} else {
search(searchString, function (err, res) {
if (err) return console.log(err);
if (res.videos.length === 0)
return msg.channel.send(client.messages.noResults);
client.funcs.handleVideo(
res.videos[0],
msg,
voiceChannel,
client,
false,
"ytdl"
);
});
}
},
};

View File

@ -1,7 +1,5 @@
module.exports = async function (guild, song, client, seek, play) { module.exports = async function (guild, song, client, seek, play) {
const { const { Readable: ReadableStream } = require("stream");
Readable: ReadableStream
} = require("stream");
const Discord = require("discord.js"); const Discord = require("discord.js");
const ytdl = require("ytdl-core"); const ytdl = require("ytdl-core");
const streamConfig = require("../config/streamConfig.js"); const streamConfig = require("../config/streamConfig.js");
@ -16,8 +14,7 @@ module.exports = async function (guild, song, client, seek, play) {
streamConfig.options.seek = seek; streamConfig.options.seek = seek;
let input = song.url; let input = song.url;
if (song.type === "ytdl") if (song.type === "ytdl") input = ytdl(song.url, streamConfig.ytdlOptions);
input = ytdl(song.url, streamConfig.ytdlOptions);
const ffmpegArgs = [ const ffmpegArgs = [
"-analyzeduration", "-analyzeduration",