This snippet returns a list of the top 10 tracks from Spotify's [Today's Top Hits](https://open.spotify.com/playlist/37i9dQZF1DXcBWIGoYBM5M?si=06a424fc3e284683) playlist displaying the name of the songs & aritsts and the playlist cover. Make sure to register a command named "spotify-top-10-songs" via the [command builder](https://autocode.com/tools/discord/command-builder/).
// Here's the link to the guide to get app credentials if you ever need it in future: https://raxlitude-1.gitbook.io/spotify-application/
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const {Spotify} = require('spotify-info.js');
const infos = new Spotify({
clientID: process.env.SPOTIFY_CLIENTID,
clientSecret: process.env.SPOTIFY_CLIENTSECRET,
});
let event = context.params.event;
// `37i9dQZF1DXcBWIGoYBM5M` is the ID for the official "Today's Top Hits" playlist by Spotify
const tthP = await infos.getPlaylist('37i9dQZF1DXcBWIGoYBM5M');
// getting song's artists' names using the map() function
// adding a comma between names if artists are multiple using .join()
let tArtists1 = tthP.tracks.items[0].track.artists
.map(({name}) => name)
.join(', ');
let tArtists2 = tthP.tracks.items[1].track.artists
.map(({name}) => name)
.join(', ');
let tArtists3 = tthP.tracks.items[2].track.artists
.map(({name}) => name)
.join(', ');
let tArtists4 = tthP.tracks.items[3].track.artists
.map(({name}) => name)
.join(', ');
let tArtists5 = tthP.tracks.items[4].track.artists
.map(({name}) => name)
.join(', ');
let tArtists6 = tthP.tracks.items[5].track.artists
.map(({name}) => name)
.join(', ');
let tArtists7 = tthP.tracks.items[6].track.artists
.map(({name}) => name)
.join(', ');
let tArtists8 = tthP.tracks.items[7].track.artists
.map(({name}) => name)
.join(', ');
let tArtists9 = tthP.tracks.items[8].track.artists
.map(({name}) => name)
.join(', ');
let tArtists10 = tthP.tracks.items[9].track.artists
.map(({name}) => name)
.join(', ');
let songs = [
{
name: tthP.tracks.items[0].track.name,
value: `By ${tArtists1}`,
},
{
name: tthP.tracks.items[1].track.name,
value: `By ${tArtists2}`,
},
{
name: tthP.tracks.items[2].track.name,
value: `By ${tArtists3}`,
},
{
name: tthP.tracks.items[3].track.name,
value: `By ${tArtists4}`,
},
{
name: tthP.tracks.items[4].track.name,
value: `By ${tArtists5}`,
},
{
name: tthP.tracks.items[5].track.name,
value: `By ${tArtists6}`,
},
{
name: tthP.tracks.items[6].track.name,
value: `By ${tArtists7}`,
},
{
name: tthP.tracks.items[7].track.name,
value: `By ${tArtists8}`,
},
{
name: tthP.tracks.items[8].track.name,
value: `By ${tArtists9}`,
},
{
name: tthP.tracks.items[9].track.name,
value: `By ${tArtists10}`,
},
];
let tColorStr = `0x${tthP.primary_color}`;
// converting string to integer, since the `color` param requires it
let eColor = parseInt(tColorStr.split('x'));
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: event.channel_id,
content: `Here's the current Top 10 songs from Spotify's Today's Top Hits!`,
embeds: [
{
title: `Today's Top Hits`,
url: tthP.external_urls.spotify,
description: tthP.description,
fields: songs,
thumbnail: {url: tthP.images[0].url},
color: eColor,
},
],
});