Skip to content
This repository has been archived by the owner on Jan 23, 2022. It is now read-only.

Commit

Permalink
[Module] Voice: Fix playlist not being added when one title fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackhawk-TA committed Nov 21, 2020
1 parent 2788f38 commit 68529c8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"spotify",
"playlist"
],
"version": "1.2.3",
"version": "1.2.4",
"license": "MIT",
"main": "main.js",
"scripts": {
Expand Down
22 changes: 18 additions & 4 deletions src/js-modules/voice/addPlaylist.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ module.exports = {
if (oConnection) {
switch (sPlaylistType) {
case "apple":
oConnection.addAppleMusicPlaylist(sPlaylistUrl).then(() => {
msg.getTextChannel().send("The Apple Music playlist was added to the current playlist.");
oConnection.addAppleMusicPlaylist(sPlaylistUrl).then(aFailedTitles => {
var sAnswer = "The Apple Music playlist was added to the current playlist." + this.generateFailedTitlesWarning(aFailedTitles);
msg.getTextChannel().send(sAnswer);
}).catch(err => {
msg.getTextChannel().send("Could not add the playlist.");
console.error(`${new Date().toLocaleString()}: ${err}`);
});
break;
case "spotify":
oConnection.addSpotifyPlaylist(sPlaylistUrl).then(() => {
msg.getTextChannel().send("The Spotify playlist was added to the current playlist.");
oConnection.addSpotifyPlaylist(sPlaylistUrl).then(aFailedTitles => {
var sAnswer = "The Spotify playlist was added to the current playlist." + this.generateFailedTitlesWarning(aFailedTitles);
msg.getTextChannel().send(sAnswer);
}).catch(err => {
msg.getTextChannel().send("Could not add the playlist.");
console.error(`${new Date().toLocaleString()}: ${err}`);
Expand All @@ -44,5 +46,17 @@ module.exports = {
} else {
msg.getTextChannel().send("You can only edit the playlist when we are in the same voice channel.");
}
},

generateFailedTitlesWarning: function(aFailedTitles) {
var sWarningText = "";

if (aFailedTitles.length === 1) {
sWarningText =`\n${aFailedTitles.length} title could not be added.`;
} else if (aFailedTitles.length > 1) {
sWarningText = `\n${aFailedTitles.length} titles could not be added.`;
}

return sWarningText;
}
};
30 changes: 19 additions & 11 deletions src/js-modules/voice/utils/VoiceConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class VoiceConnection {
/**
* Search each track in the list and adds the Youtube links to the playlist
* @param {object[]} tracks The track to be added to the playlist
* @return {Promise<void|Error>} On resolve it returns nothing, on reject the error
* @return {Promise<object[]|Error>} On resolve it returns an array of the titles which could not be added.
*/
searchAndAddTracks(tracks) {
return new Promise((resolve, reject) => {
Expand All @@ -308,14 +308,22 @@ class VoiceConnection {
aPromises.push(this.searchTitle(sQuery));
}.bind(this));

Promise.all(aPromises).then(function(aResult) {
aResult.forEach(oTitle => {
this._aPlaylist.push(oTitle);
Promise.allSettled(aPromises).then(function(aResult) {
var aFailedTitles = [];
var oTitle;

aResult.forEach(oPromise => {
oTitle = oPromise.value;
if (oPromise.status === "fulfilled") {
this._aPlaylist.push(oTitle);
} else {
aFailedTitles.push(oTitle);
}
});
if (!this._sCurrentTitleUrl) {
this.play();
}
resolve();
resolve(aFailedTitles);
}.bind(this)).catch(err => {
reject(err);
});
Expand All @@ -325,13 +333,13 @@ class VoiceConnection {
/**
* Adds the given spotify playlist to the bots playlist
* @param {string} url The spotify playlist url
* @return {Promise<void|Error>} On resolve it returns nothing, on reject the error
* @return {Promise<object[]|Error>} On resolve it returns an array of the titles which could not be added.
*/
addSpotifyPlaylist(url) {
return new Promise((resolve, reject) => {
playlistImporter.getPlaylistData(url).then(data => {
this.searchAndAddTracks(data.tracklist).then(() => {
resolve();
this.searchAndAddTracks(data.tracklist).then(aFailedTitles => {
resolve(aFailedTitles);
}).catch(err => {
reject(err);
});
Expand All @@ -344,13 +352,13 @@ class VoiceConnection {
/**
* Adds the given apple music playlist to the bots playlist
* @param {string} url The apple music playlist url
* @return {Promise<void|Error>} On resolve it returns nothing, on reject the error
* @return {Promise<object[]|Error>} On resolve it returns an array of the titles which could not be added.
*/
addAppleMusicPlaylist(url) {
return new Promise((resolve, reject) => {
amply.getPlaylist(url).then(aResults => {
this.searchAndAddTracks(aResults).then(() => {
resolve();
this.searchAndAddTracks(aResults).then(aFailedTitles => {
resolve(aFailedTitles);
}).catch(err => {
reject(err);
});
Expand Down

0 comments on commit 68529c8

Please sign in to comment.