aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorBobby <[email protected]>2024-11-06 09:42:25 -0500
committerGitHub <[email protected]>2024-11-06 09:42:25 -0500
commitd92fd2a29796c9c5d6ddeb9718bf6fcd6ee5958a (patch)
tree81961cc995661448794669617f1ed439cbe84a3d /src/utils
parentb605bf220859acd767533e0ab9436ced771bb8e2 (diff)
parent716d6d9f4f2cd1a6872e463e9877203a259478a3 (diff)
downloadmuse-master.tar.xz
muse-master.zip
Merge branch 'museofficial:master' into masterHEADmaster
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/build-embed.ts10
-rw-r--r--src/utils/get-youtube-and-spotify-suggestions-for.ts79
2 files changed, 47 insertions, 42 deletions
diff --git a/src/utils/build-embed.ts b/src/utils/build-embed.ts
index b8e725c..23db0b9 100644
--- a/src/utils/build-embed.ts
+++ b/src/utils/build-embed.ts
@@ -5,8 +5,6 @@ import getProgressBar from './get-progress-bar.js';
import {prettyTime} from './time.js';
import {truncate} from './string.js';
-const PAGE_SIZE = 10;
-
const getMaxSongTitleLength = (title: string) => {
// eslint-disable-next-line no-control-regex
const nonASCII = /[^\x00-\x7F]+/;
@@ -77,7 +75,7 @@ export const buildPlayingMessageEmbed = (player: Player): EmbedBuilder => {
return message;
};
-export const buildQueueEmbed = (player: Player, page: number): EmbedBuilder => {
+export const buildQueueEmbed = (player: Player, page: number, pageSize: number): EmbedBuilder => {
const currentlyPlaying = player.getCurrent();
if (!currentlyPlaying) {
@@ -85,14 +83,14 @@ export const buildQueueEmbed = (player: Player, page: number): EmbedBuilder => {
}
const queueSize = player.queueSize();
- const maxQueuePage = Math.ceil((queueSize + 1) / PAGE_SIZE);
+ const maxQueuePage = Math.ceil((queueSize + 1) / pageSize);
if (page > maxQueuePage) {
throw new Error('the queue isn\'t that big');
}
- const queuePageBegin = (page - 1) * PAGE_SIZE;
- const queuePageEnd = queuePageBegin + PAGE_SIZE;
+ const queuePageBegin = (page - 1) * pageSize;
+ const queuePageEnd = queuePageBegin + pageSize;
const queuedSongs = player
.getQueue()
.slice(queuePageBegin, queuePageEnd)
diff --git a/src/utils/get-youtube-and-spotify-suggestions-for.ts b/src/utils/get-youtube-and-spotify-suggestions-for.ts
index 6594b52..bd6f1c8 100644
--- a/src/utils/get-youtube-and-spotify-suggestions-for.ts
+++ b/src/utils/get-youtube-and-spotify-suggestions-for.ts
@@ -14,28 +14,18 @@ const filterDuplicates = <T extends {name: string}>(items: T[]) => {
return results;
};
-const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify: SpotifyWebApi, limit = 10): Promise<APIApplicationCommandOptionChoice[]> => {
- const [youtubeSuggestions, spotifyResults] = await Promise.all([
- getYouTubeSuggestionsFor(query),
- spotify.search(query, ['track', 'album'], {limit: 5}),
- ]);
+const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: SpotifyWebApi, limit = 10): Promise<APIApplicationCommandOptionChoice[]> => {
+ // Only search Spotify if enabled
+ const spotifySuggestionPromise = spotify === undefined
+ ? undefined
+ : spotify.search(query, ['album', 'track'], {limit});
- const totalYouTubeResults = youtubeSuggestions.length;
-
- const spotifyAlbums = filterDuplicates(spotifyResults.body.albums?.items ?? []);
- const spotifyTracks = filterDuplicates(spotifyResults.body.tracks?.items ?? []);
-
- const totalSpotifyResults = spotifyAlbums.length + spotifyTracks.length;
+ const youtubeSuggestions = await getYouTubeSuggestionsFor(query);
- // Number of results for each source should be roughly the same.
- // If we don't have enough Spotify suggestions, prioritize YouTube results.
- const maxSpotifySuggestions = Math.floor(limit / 2);
- const numOfSpotifySuggestions = Math.min(maxSpotifySuggestions, totalSpotifyResults);
-
- const maxYouTubeSuggestions = limit - numOfSpotifySuggestions;
- const numOfYouTubeSuggestions = Math.min(maxYouTubeSuggestions, totalYouTubeResults);
+ const totalYouTubeResults = youtubeSuggestions.length;
+ const numOfYouTubeSuggestions = Math.min(limit, totalYouTubeResults);
- const suggestions: APIApplicationCommandOptionChoice[] = [];
+ let suggestions: APIApplicationCommandOptionChoice[] = [];
suggestions.push(
...youtubeSuggestions
@@ -46,23 +36,40 @@ const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify: Spotif
}),
));
- const maxSpotifyAlbums = Math.floor(numOfSpotifySuggestions / 2);
- const numOfSpotifyAlbums = Math.min(maxSpotifyAlbums, spotifyResults.body.albums?.items.length ?? 0);
- const maxSpotifyTracks = numOfSpotifySuggestions - numOfSpotifyAlbums;
-
- suggestions.push(
- ...spotifyAlbums.slice(0, maxSpotifyAlbums).map(album => ({
- name: `Spotify: 💿 ${album.name}${album.artists.length > 0 ? ` - ${album.artists[0].name}` : ''}`,
- value: `spotify:album:${album.id}`,
- })),
- );
-
- suggestions.push(
- ...spotifyTracks.slice(0, maxSpotifyTracks).map(track => ({
- name: `Spotify: 🎵 ${track.name}${track.artists.length > 0 ? ` - ${track.artists[0].name}` : ''}`,
- value: `spotify:track:${track.id}`,
- })),
- );
+ if (spotify !== undefined && spotifySuggestionPromise !== undefined) {
+ const spotifyResponse = (await spotifySuggestionPromise).body;
+ const spotifyAlbums = filterDuplicates(spotifyResponse.albums?.items ?? []);
+ const spotifyTracks = filterDuplicates(spotifyResponse.tracks?.items ?? []);
+
+ const totalSpotifyResults = spotifyAlbums.length + spotifyTracks.length;
+
+ // Number of results for each source should be roughly the same.
+ // If we don't have enough Spotify suggestions, prioritize YouTube results.
+ const maxSpotifySuggestions = Math.floor(limit / 2);
+ const numOfSpotifySuggestions = Math.min(maxSpotifySuggestions, totalSpotifyResults);
+
+ const maxSpotifyAlbums = Math.floor(numOfSpotifySuggestions / 2);
+ const numOfSpotifyAlbums = Math.min(maxSpotifyAlbums, spotifyResponse.albums?.items.length ?? 0);
+ const maxSpotifyTracks = numOfSpotifySuggestions - numOfSpotifyAlbums;
+
+ // Make room for spotify results
+ const maxYouTubeSuggestions = limit - numOfSpotifySuggestions;
+ suggestions = suggestions.slice(0, maxYouTubeSuggestions);
+
+ suggestions.push(
+ ...spotifyAlbums.slice(0, maxSpotifyAlbums).map(album => ({
+ name: `Spotify: 💿 ${album.name}${album.artists.length > 0 ? ` - ${album.artists[0].name}` : ''}`,
+ value: `spotify:album:${album.id}`,
+ })),
+ );
+
+ suggestions.push(
+ ...spotifyTracks.slice(0, maxSpotifyTracks).map(track => ({
+ name: `Spotify: 🎵 ${track.name}${track.artists.length > 0 ? ` - ${track.artists[0].name}` : ''}`,
+ value: `spotify:track:${track.id}`,
+ })),
+ );
+ }
return suggestions;
};