aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Isom <[email protected]>2020-03-18 19:17:47 -0500
committerMax Isom <[email protected]>2020-03-18 19:17:47 -0500
commit035737312360279efd5026b225f30ef14aa55abf (patch)
tree87155d94e351abe640d0de02c0125ee6bd2491b3 /src
parenta0c5875ee43166a9565e4a086a701864c52f549d (diff)
downloadmuse-035737312360279efd5026b225f30ef14aa55abf.tar.xz
muse-035737312360279efd5026b225f30ef14aa55abf.zip
Limit playlist adds to 50 songs
Diffstat (limited to 'src')
-rw-r--r--src/commands/play.ts19
-rw-r--r--src/services/get-songs.ts23
2 files changed, 34 insertions, 8 deletions
diff --git a/src/commands/play.ts b/src/commands/play.ts
index bcafdbf..eceac94 100644
--- a/src/commands/play.ts
+++ b/src/commands/play.ts
@@ -71,6 +71,7 @@ export default class implements Command {
}
const newSongs: QueuedSong[] = [];
+ let extraMsg = '';
// Test if it's a complete URL
try {
@@ -95,7 +96,15 @@ export default class implements Command {
}
}
} else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') {
- const [convertedSongs] = await this.getSongs.spotifySource(args[0]);
+ const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(args[0]);
+
+ if (totalSongs > 50) {
+ extraMsg = 'a random sample of 50 songs was taken';
+ }
+
+ if (nSongsNotFound !== 0) {
+ extraMsg += `and ${nSongsNotFound.toString()} songs were not found`;
+ }
newSongs.push(...convertedSongs);
}
@@ -122,10 +131,14 @@ export default class implements Command {
const firstSong = newSongs[0];
+ if (extraMsg !== '') {
+ extraMsg = ` (${extraMsg})`;
+ }
+
if (newSongs.length === 1) {
- await res.stop(`u betcha, **${firstSong.title}** added to the queue`);
+ await res.stop(`u betcha, **${firstSong.title}** added to the queue${extraMsg}`);
} else {
- await res.stop(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue`);
+ await res.stop(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue${extraMsg}`);
}
if (this.playerManager.get(msg.guild!.id).voiceConnection === null) {
diff --git a/src/services/get-songs.ts b/src/services/get-songs.ts
index 19fc6c0..347d9d6 100644
--- a/src/services/get-songs.ts
+++ b/src/services/get-songs.ts
@@ -7,6 +7,7 @@ import Spotify from 'spotify-web-api-node';
import ytsr from 'ytsr';
import YouTube from 'youtube.ts';
import pLimit from 'p-limit';
+import uniqueRandomArray from 'unique-random-array';
import {QueuedSong, QueuedPlaylist} from '../services/queue';
import {TYPES} from '../types';
@@ -77,10 +78,10 @@ export default class {
});
}
- async spotifySource(url: string): Promise<[QueuedSong[], number]> {
+ async spotifySource(url: string): Promise<[QueuedSong[], number, number]> {
const parsed = spotifyURI.parse(url);
- const tracks: SpotifyApi.TrackObjectSimplified[] = [];
+ let tracks: SpotifyApi.TrackObjectSimplified[] = [];
let playlist: QueuedPlaylist | null = null;
@@ -137,12 +138,24 @@ export default class {
}
default: {
- return [[], 0];
+ return [[], 0, 0];
+ }
+ }
+
+ // Get 50 random songs if many
+ const originalNSongs = tracks.length;
+
+ if (tracks.length > 50) {
+ const random = uniqueRandomArray(tracks);
+
+ tracks = [];
+ for (let i = 0; i < 50; i++) {
+ tracks.push(random());
}
}
// Limit concurrency so hopefully we don't get banned for searching
- const limit = pLimit(3);
+ const limit = pLimit(5);
let songs = await Promise.all(tracks.map(async track => limit(async () => this.spotifyToYouTube(track, playlist))));
let nSongsNotFound = 0;
@@ -158,7 +171,7 @@ export default class {
return accum;
}, []);
- return [songs as QueuedSong[], nSongsNotFound];
+ return [songs as QueuedSong[], nSongsNotFound, originalNSongs];
}
private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, playlist: QueuedPlaylist | null): Promise<QueuedSong | null> {