mirror of
https://github.com/BluemediaDev/muse.git
synced 2025-06-28 01:32:42 +02:00
Migrate to slash commands (#431)
Co-authored-by: Federico fuji97 Rapetti <fuji1097@gmail.com>
This commit is contained in:
parent
e883275d83
commit
56a469a999
51 changed files with 1270 additions and 1294 deletions
70
src/utils/get-youtube-and-spotify-suggestions-for.ts
Normal file
70
src/utils/get-youtube-and-spotify-suggestions-for.ts
Normal file
|
@ -0,0 +1,70 @@
|
|||
import {ApplicationCommandOptionChoice} from 'discord.js';
|
||||
import SpotifyWebApi from 'spotify-web-api-node';
|
||||
import getYouTubeSuggestionsFor from './get-youtube-suggestions-for.js';
|
||||
|
||||
const filterDuplicates = <T extends {name: string}>(items: T[]) => {
|
||||
const results: T[] = [];
|
||||
|
||||
for (const item of items) {
|
||||
if (!results.some(result => result.name === item.name)) {
|
||||
results.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
};
|
||||
|
||||
const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify: SpotifyWebApi, limit = 10): Promise<ApplicationCommandOptionChoice[]> => {
|
||||
const [youtubeSuggestions, spotifyResults] = await Promise.all([
|
||||
getYouTubeSuggestionsFor(query),
|
||||
spotify.search(query, ['track', 'album'], {limit: 5}),
|
||||
]);
|
||||
|
||||
const totalYouTubeResults = youtubeSuggestions.length;
|
||||
|
||||
const spotifyAlbums = filterDuplicates(spotifyResults.body.albums?.items ?? []);
|
||||
const spotifyTracks = filterDuplicates(spotifyResults.body.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 maxYouTubeSuggestions = limit - numOfSpotifySuggestions;
|
||||
const numOfYouTubeSuggestions = Math.min(maxYouTubeSuggestions, totalYouTubeResults);
|
||||
|
||||
const suggestions: ApplicationCommandOptionChoice[] = [];
|
||||
|
||||
suggestions.push(
|
||||
...youtubeSuggestions
|
||||
.slice(0, numOfYouTubeSuggestions)
|
||||
.map(suggestion => ({
|
||||
name: `YouTube: ${suggestion}`,
|
||||
value: suggestion,
|
||||
}),
|
||||
));
|
||||
|
||||
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}`,
|
||||
})),
|
||||
);
|
||||
|
||||
return suggestions;
|
||||
};
|
||||
|
||||
export default getYouTubeAndSpotifySuggestionsFor;
|
Loading…
Add table
Add a link
Reference in a new issue