mirror of
https://github.com/musix-org/musix-oss
synced 2025-07-03 16:24:28 +00:00
Validate url
Validate url for precise song accuracy when using a link to play a song
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
const YouTube = require("simple-youtube-api");
|
||||
const search = require("yt-search");
|
||||
const SpotifyApi = require("spotify-web-api-node");
|
||||
const search = require("yt-search");
|
||||
const ytdl = require("ytdl-core")
|
||||
|
||||
module.exports = {
|
||||
name: "play",
|
||||
@ -41,88 +42,30 @@ module.exports = {
|
||||
return msg.channel.send(client.messages.noPermsConnect);
|
||||
if (!voiceChannel.speakable)
|
||||
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];
|
||||
spotify.getPlaylist(playlistId).then(
|
||||
async function (data) {
|
||||
const lmsg = await msg.channel.send(client.messages.loadingSongs);
|
||||
let failed = 0;
|
||||
for (let i = 0; data.body.tracks.items.length > i; i++) {
|
||||
const track = await data.body.tracks.items[i].track;
|
||||
await client.funcs.sleep(250);
|
||||
await search(
|
||||
`${track.artists[0].name} ${track.name} audio`,
|
||||
async function (err, res) {
|
||||
if (err) return console.log(err);
|
||||
if (res.videos.length === 0) {
|
||||
await search(
|
||||
`${track.artists[0].name} ${track.name} lyrics`,
|
||||
async function (err, res) {
|
||||
if (err) return console.log(err);
|
||||
if (res.videos.length === 0) {
|
||||
await search(
|
||||
`${track.artists[0].name} ${track.name}`,
|
||||
async function (err, res) {
|
||||
if (err) return console.log(err);
|
||||
if (res.videos.length === 0) {
|
||||
failed++;
|
||||
return;
|
||||
}
|
||||
await client.funcs.handleVideo(
|
||||
res.videos[0],
|
||||
msg,
|
||||
voiceChannel,
|
||||
client,
|
||||
true,
|
||||
"ytdl"
|
||||
);
|
||||
}
|
||||
);
|
||||
return;
|
||||
}
|
||||
await client.funcs.handleVideo(
|
||||
res.videos[0],
|
||||
msg,
|
||||
voiceChannel,
|
||||
client,
|
||||
true,
|
||||
"ytdl"
|
||||
);
|
||||
}
|
||||
);
|
||||
failed++;
|
||||
return;
|
||||
}
|
||||
await client.funcs.handleVideo(
|
||||
res.videos[0],
|
||||
msg,
|
||||
voiceChannel,
|
||||
client,
|
||||
true,
|
||||
"ytdl"
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
let message;
|
||||
if (failed === 0) {
|
||||
message = client.messages.playlistAdded.replace(
|
||||
"%TITLE%",
|
||||
data.body.name
|
||||
);
|
||||
} else {
|
||||
message = `${client.messages.playlistAdded.replace(
|
||||
"%TITLE%",
|
||||
data.body.name
|
||||
)}\n${client.messages.failedToLoad + failed}`;
|
||||
}
|
||||
lmsg.edit(message);
|
||||
},
|
||||
function (err) {
|
||||
console.log(err);
|
||||
msg.channel.send(client.messages.noResultsSpotify);
|
||||
}
|
||||
);
|
||||
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(.*)$/)
|
||||
) {
|
||||
@ -147,7 +90,7 @@ module.exports = {
|
||||
);
|
||||
return lmsg.edit(message);
|
||||
} else {
|
||||
search(searchString, function (err, res) {
|
||||
ytSearch(searchString, function (err, res) {
|
||||
if (err) return console.log(err);
|
||||
if (res.videos.length === 0)
|
||||
return msg.channel.send(client.messages.noResults);
|
||||
@ -163,3 +106,78 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
async function searchVideos(data, client, msg, voiceChannel) {
|
||||
const lmsg = await msg.channel.send(client.messages.loadingSongs);
|
||||
let failed = 0;
|
||||
for (let i = 0; data.body.tracks.items.length > i; i++) {
|
||||
const track = await data.body.tracks.items[i].track;
|
||||
await client.funcs.sleep(250);
|
||||
await search(
|
||||
`${track.artists[0].name} ${track.name} audio`,
|
||||
async function (err, res) {
|
||||
if (err) return console.log(err);
|
||||
if (res.videos.length === 0) {
|
||||
await search(
|
||||
`${track.artists[0].name} ${track.name} lyrics`,
|
||||
async function (err, res) {
|
||||
if (err) return console.log(err);
|
||||
if (res.videos.length === 0) {
|
||||
await search(
|
||||
`${track.artists[0].name} ${track.name}`,
|
||||
async function (err, res) {
|
||||
if (err) return console.log(err);
|
||||
if (res.videos.length === 0) {
|
||||
failed++;
|
||||
return;
|
||||
}
|
||||
await client.funcs.handleVideo(
|
||||
res.videos[0],
|
||||
msg,
|
||||
voiceChannel,
|
||||
client,
|
||||
true,
|
||||
"ytdl"
|
||||
);
|
||||
}
|
||||
);
|
||||
return;
|
||||
}
|
||||
await client.funcs.handleVideo(
|
||||
res.videos[0],
|
||||
msg,
|
||||
voiceChannel,
|
||||
client,
|
||||
true,
|
||||
"ytdl"
|
||||
);
|
||||
}
|
||||
);
|
||||
failed++;
|
||||
return;
|
||||
}
|
||||
await client.funcs.handleVideo(
|
||||
res.videos[0],
|
||||
msg,
|
||||
voiceChannel,
|
||||
client,
|
||||
true,
|
||||
"ytdl"
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
let message;
|
||||
if (failed === 0) {
|
||||
message = client.messages.playlistAdded.replace(
|
||||
"%TITLE%",
|
||||
data.body.name
|
||||
);
|
||||
} else {
|
||||
message = `${client.messages.playlistAdded.replace(
|
||||
"%TITLE%",
|
||||
data.body.name
|
||||
)}\n${client.messages.failedToLoad + failed}`;
|
||||
}
|
||||
lmsg.edit(message);
|
||||
}
|
Reference in New Issue
Block a user