1
0
mirror of https://github.com/musix-org/musix-oss synced 2025-01-08 07:34:49 +00:00
musix-oss/src/commands/play.js

402 lines
12 KiB
JavaScript
Raw Normal View History

2020-06-17 13:38:43 +00:00
const ytdl = require("ytdl-core");
const ytsr = require("ytsr");
2020-02-05 20:02:53 +00:00
module.exports = {
2020-04-19 17:00:16 +00:00
name: "play",
2020-05-15 16:05:39 +00:00
alias: ["p", "music"],
2020-04-19 17:00:16 +00:00
usage: "<song name>",
description: "Play some music.",
permission: "none",
2020-07-12 16:08:53 +00:00
category: "play",
2024-02-10 07:33:59 +00:00
async execute(msg, args, client, command) {
2020-04-19 17:00:16 +00:00
const searchString = args.slice(1).join(" ");
const url = args[1] ? args[1].replace(/<(.+)>/g, "$1") : "";
const queue = client.queue.get(msg.guild.id);
const voiceChannel = msg.member.voice.channel;
if (
client.global.db.guilds[msg.guild.id].blacklist.includes(
msg.member.voice.channelID
)
)
return msg.channel.send(client.messages.blackListedVC);
if (!queue) {
if (!msg.member.voice.channel)
return msg.channel.send(client.messages.noVoiceChannel);
} else {
if (voiceChannel !== queue.voiceChannel)
return msg.channel.send(client.messages.wrongVoiceChannel);
}
if (!args[1]) return msg.channel.send(client.messages.noQuery);
if (voiceChannel.full) return msg.channel.send(client.messages.channelFull);
if (!voiceChannel.joinable)
return msg.channel.send(client.messages.noPermsConnect);
if (!voiceChannel.speakable)
return msg.channel.send(client.messages.noPermsSpeak);
if (ytdl.validateURL(url)) {
2020-06-17 13:38:43 +00:00
client.funcs.handleVideo(url, msg, voiceChannel, client, false, "ytdl");
} else if (url.match(/^https?:\/\/(open.spotify.com|spotify.com)(.*)$/)) {
2020-06-30 17:43:00 +00:00
if (url.includes("playlist")) {
const playlistId = url.split("/playlist/")[1].split("?")[0];
2020-07-12 16:08:53 +00:00
client.spotify.getPlaylist(playlistId).then(
2020-06-30 17:43:00 +00:00
async function (data) {
2020-07-04 19:17:20 +00:00
searchPlaylist(data, client, msg, voiceChannel);
},
function (err) {
console.log(err);
msg.channel.send(client.messages.noResultsSpotify);
}
2020-06-30 17:43:00 +00:00
);
} else if (url.includes("album")) {
const albumId = url.split("/album/")[1].split("?")[0];
2020-07-12 16:08:53 +00:00
client.spotify.getAlbumTracks(albumId).then(
2020-07-03 08:23:26 +00:00
async function (data) {
2020-07-04 19:17:20 +00:00
searchAlbum(data, client, msg, voiceChannel);
},
function (err) {
console.log(err);
msg.channel.send(client.messages.noResultsSpotify);
}
2020-07-03 08:23:26 +00:00
);
2020-06-30 17:43:00 +00:00
} else if (url.includes("track")) {
return msg.channel.send(client.messages.disabledSpotifySongs);
/*const trackId = url.split("/track/")[1].split("?")[0];
spotify.searchTracks(trackId)
.then(function (data) {
console.log(data.body)
}, function (err) {
console.log('Something went wrong!', err);
});*/
2020-07-03 08:23:26 +00:00
} else msg.channel.send(client.messages.invalidSpotifyUrl);
2020-04-19 17:00:16 +00:00
} else if (
url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)
) {
const lmsg = await msg.channel.send(client.messages.loadingSongs);
2020-07-12 16:08:53 +00:00
const playlist = await client.youtube.getPlaylist(url).catch((err) => {
2020-07-03 08:23:26 +00:00
console.log("err1");
});
const videos = await playlist.getVideos().catch((err) => {
console.log("err2");
});
2020-04-19 17:00:16 +00:00
for (const video of Object.values(videos)) {
2020-07-12 16:08:53 +00:00
const video2 = await client.youtube.getVideoByID(video.id).catch((err) => {
2020-07-03 08:23:26 +00:00
console.log("err3");
});
2020-07-12 16:08:53 +00:00
client.spotify.searchTracks(`track:${video2.name}`).then(
2020-07-03 08:23:26 +00:00
function (data) {
2020-06-30 17:43:00 +00:00
client.funcs.handleVideo(
video2.url,
msg,
voiceChannel,
client,
true,
"ytdl",
data.body.tracks.items[0]
);
2020-07-03 08:23:26 +00:00
},
function (err) {
console.log("Something went wrong!", err);
}
);
2020-04-19 17:00:16 +00:00
}
2020-06-09 18:25:55 +00:00
const message = client.messages.playlistAdded.replace(
2020-04-19 17:00:16 +00:00
"%TITLE%",
playlist.title
);
return lmsg.edit(message);
} else {
2020-06-17 13:38:43 +00:00
ytsr(
2020-07-04 19:17:20 +00:00
searchString, {
2020-06-17 13:38:43 +00:00
limit: 5,
},
function (err, res) {
if (err) console.log(err);
if (!res.items[0]) return msg.channel.send(client.messages.noResults);
2020-07-03 08:23:26 +00:00
const videoResults = res.items.filter(
(item) => item.type === "video"
);
2020-07-12 16:08:53 +00:00
client.spotify.searchTracks(`track:${searchString}`).then(
2020-07-03 08:23:26 +00:00
function (data) {
2020-06-30 17:43:00 +00:00
client.funcs.handleVideo(
videoResults[0].link,
msg,
voiceChannel,
client,
false,
"ytdl",
data.body.tracks.items[0]
);
2020-07-03 08:23:26 +00:00
},
function (err) {
2020-07-12 16:08:53 +00:00
console.log(err);
2020-07-03 08:23:26 +00:00
}
);
2020-06-17 13:38:43 +00:00
}
);
2020-04-19 17:00:16 +00:00
}
},
2020-04-21 19:19:40 +00:00
};
2020-06-30 17:43:00 +00:00
async function searchPlaylist(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);
ytsr(
2020-07-04 19:17:20 +00:00
`${track.artists[0].name} ${track.name} audio`, {
2020-06-30 17:43:00 +00:00
limit: 5,
},
async function (err, res) {
if (err) return console.log(err);
if (!res.items[0]) {
ytsr(
2020-07-04 19:17:20 +00:00
`${track.artists[0].name} ${track.name} lyrics`, {
2020-06-30 17:43:00 +00:00
limit: 5,
},
async function (err, res) {
if (err) return console.log(err);
if (!res.items[0]) {
ytsr(
2020-07-04 19:17:20 +00:00
`${track.artists[0].name} ${track.name}`, {
2020-06-30 17:43:00 +00:00
limit: 5,
},
async function (err, res) {
if (err) console.log(err);
if (!res.items[0]) {
failed++;
}
2020-07-03 08:23:26 +00:00
const videoResults = res.items.filter(
(item) => item.type === "video"
);
2020-06-30 17:43:00 +00:00
client.funcs.handleVideo(
videoResults[0].link,
msg,
voiceChannel,
client,
false,
"spotify",
track
);
}
);
return;
}
2020-07-03 08:23:26 +00:00
const videoResults = res.items.filter(
(item) => item.type === "video"
);
2020-06-30 17:43:00 +00:00
await client.funcs.handleVideo(
videoResults[0].link,
msg,
voiceChannel,
client,
true,
"spotify",
track
);
}
);
failed++;
return;
}
2020-07-03 08:23:26 +00:00
const videoResults = res.items.filter((item) => item.type === "video");
2020-06-30 17:43:00 +00:00
await client.funcs.handleVideo(
videoResults[0].link,
msg,
voiceChannel,
client,
true,
"spotify",
track
);
}
);
}
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);
}
async function searchAlbum(data, client, msg, voiceChannel) {
const lmsg = await msg.channel.send(client.messages.loadingSongs);
let failed = 0;
for (let i = 0; data.body.items.length > i; i++) {
const track = await data.body.items[i];
await client.funcs.sleep(250);
ytsr(
2020-07-04 19:17:20 +00:00
`${track.artists[0].name} ${track.name} audio`, {
2020-06-30 17:43:00 +00:00
limit: 5,
},
async function (err, res) {
if (err) return console.log(err);
if (!res.items[0]) {
ytsr(
2020-07-04 19:17:20 +00:00
`${track.artists[0].name} ${track.name} lyrics`, {
2020-06-30 17:43:00 +00:00
limit: 5,
},
async function (err, res) {
if (err) return console.log(err);
if (!res.items[0]) {
ytsr(
2020-07-04 19:17:20 +00:00
`${track.artists[0].name} ${track.name}`, {
2020-06-30 17:43:00 +00:00
limit: 5,
},
async function (err, res) {
if (err) console.log(err);
if (!res.items[0]) {
failed++;
}
2020-07-03 08:23:26 +00:00
const videoResults = res.items.filter(
(item) => item.type === "video"
);
2020-06-30 17:43:00 +00:00
client.funcs.handleVideo(
videoResults[0].link,
msg,
voiceChannel,
client,
false,
"spotify",
track
);
}
);
return;
}
2020-07-03 08:23:26 +00:00
const videoResults = res.items.filter(
(item) => item.type === "video"
);
2020-06-30 17:43:00 +00:00
await client.funcs.handleVideo(
videoResults[0].link,
msg,
voiceChannel,
client,
true,
"spotify",
track
);
}
);
failed++;
return;
}
2020-07-03 08:23:26 +00:00
const videoResults = res.items.filter((item) => item.type === "video");
2020-06-30 17:43:00 +00:00
await client.funcs.handleVideo(
videoResults[0].link,
msg,
voiceChannel,
client,
true,
"spotify",
track
);
}
);
}
let message;
if (failed === 0) {
2020-07-03 08:23:26 +00:00
message = client.messages.albumAdded.replace(
"%TITLE%",
"yes taht palylist"
);
2020-06-30 17:43:00 +00:00
} else {
message = `${client.messages.albumAdded.replace(
"%TITLE%",
"yes taht palylist"
)}\n${client.messages.failedToLoad + failed}`;
}
lmsg.edit(message);
}
async function searchSong(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);
2020-06-17 13:38:43 +00:00
ytsr(
2020-07-04 19:17:20 +00:00
`${track.artists[0].name} ${track.name} audio`, {
2020-06-17 13:38:43 +00:00
limit: 5,
},
async function (err, res) {
if (err) return console.log(err);
2020-06-17 13:38:43 +00:00
if (!res.items[0]) {
ytsr(
2020-07-04 19:17:20 +00:00
`${track.artists[0].name} ${track.name} lyrics`, {
2020-06-17 13:38:43 +00:00
limit: 5,
},
async function (err, res) {
if (err) return console.log(err);
2020-06-17 13:38:43 +00:00
if (!res.items[0]) {
ytsr(
2020-07-04 19:17:20 +00:00
`${track.artists[0].name} ${track.name}`, {
2020-06-17 13:38:43 +00:00
limit: 5,
},
async function (err, res) {
2020-06-17 13:38:43 +00:00
if (err) console.log(err);
if (!res.items[0]) {
failed++;
}
2020-07-03 08:23:26 +00:00
const videoResults = res.items.filter(
(item) => item.type === "video"
);
2020-06-17 13:38:43 +00:00
client.funcs.handleVideo(
videoResults[0].link,
msg,
voiceChannel,
client,
2020-06-17 13:38:43 +00:00
false,
2020-06-30 17:43:00 +00:00
"spotify",
track
);
}
);
return;
}
2020-07-03 08:23:26 +00:00
const videoResults = res.items.filter(
(item) => item.type === "video"
);
await client.funcs.handleVideo(
2020-06-17 13:38:43 +00:00
videoResults[0].link,
msg,
voiceChannel,
client,
true,
2020-06-30 17:43:00 +00:00
"spotify",
track
);
}
);
failed++;
return;
}
2020-07-03 08:23:26 +00:00
const videoResults = res.items.filter((item) => item.type === "video");
await client.funcs.handleVideo(
2020-06-17 13:38:43 +00:00
videoResults[0].link,
msg,
voiceChannel,
client,
true,
2020-06-30 17:43:00 +00:00
"spotify",
track
);
}
);
}
let message;
if (failed === 0) {
2020-06-17 13:38:43 +00:00
message = client.messages.playlistAdded.replace("%TITLE%", data.body.name);
} else {
message = `${client.messages.playlistAdded.replace(
2020-06-17 13:38:43 +00:00
"%TITLE%",
data.body.name
)}\n${client.messages.failedToLoad + failed}`;
}
lmsg.edit(message);
2024-02-10 07:33:59 +00:00
}