1
0
mirror of https://github.com/musix-org/musix-oss synced 2025-07-06 19:00:50 +00:00
Files
musix-oss/src/events/dispatcherEvents/finish.js
2024-02-10 09:33:59 +02:00

96 lines
3.1 KiB
JavaScript

const similarSongs = require("similar-songs");
const ytdl = require("ytdl-core");
module.exports = {
async execute(client, guild) {
const queue = client.queue.get(guild.id);
queue.playing = false;
if (queue.endReason === "seek") {
return (queue.playing = true);
}
if (!queue.songLooping) {
if (queue.looping) {
queue.songs.push(queue.songs[0]);
}
queue.time = 0;
queue.votes = 0;
queue.voters = [];
if (queue.endReason !== "replay") {
if (queue.endReason === "previous")
queue.songs.unshift(queue.prevSongs.pop());
if (queue.endReason !== "previous")
queue.prevSongs.push(queue.songs.shift());
if (
client.global.db.guilds[guild.id].autoPlay &&
!queue.songs[0] &&
queue.endReason !== "stop"
) {
if (queue.prevSongs.length > 0)
return findSimilar(client, queue, queue.prevSongs, guild);
}
}
}
client.funcs.play(guild, queue.songs[0], client, 0, true);
},
};
function findSimilar(client, queue, prevSongs, guild) {
let retries = 0;
const query =
prevSongs[Math.floor(Math.random() * Math.floor(prevSongs.length))];
if (!query || !query.track) return findSimilar(client, queue, prevSongs, guild);
similarSongs.find({
title: query.track.name,
artist: query.track.artists[0].name,
limit: 10,
lastfmAPIKey: client.config.lastfm_api_key,
lastfmAPISecret: client.config.lastfm_secret,
youtubeAPIKey: client.config.youtube_api_key,
},
async function (err, songs) {
if (err) {
if (
err.message ==
'The request cannot be completed because you have exceeded your <a href="/youtube/v3/getting-started#quota">quota</a>.'
) {
queue.voiceChannel.leave();
queue.exists = false;
client.queue.delete(guild.id);
queue.textChannel.send(client.messages.quotaReached);
return;
}
console.log(err.message);
queue.voiceChannel.leave();
queue.exists = false;
client.queue.delete(guild.id);
return queue.textChannel.send(client.messages.error);
}
if (songs[0]) {
const random = Math.floor(Math.random() * Math.floor(songs.length));
const songInfo = await ytdl.getInfo(
`https://www.youtube.com/watch?v=${songs[random].youtubeId}`
);
queue.songs.push({
title: songInfo.videoDetails.title,
url: `https://www.youtube.com/watch?v=${songs[random].youtubeId}`,
author: client.user,
type: "ytdl",
info: songInfo.videoDetails,
track: query.track,
});
client.funcs.play(guild, queue.songs[0], client, 0, true);
} else {
if (prevSongs.length > 4 && retries < 6) {
findSimilar(client, queue, prevSongs, guild);
retries++;
return;
}
queue.textChannel.send(client.messages.noSimilarResults);
client.funcs.play(guild, queue.songs[0], client, 0, true);
}
}
);
}