This snippet allows you to get information about Spotify songs, albums & artists using a slash command. You need to register a slash command named "search-spotify" via the [command builder](https://autocode.com/tools/discord/command-builder/) with two string options, `search` & `query`. 'search' will be for the search type, which can be either 'song', 'album' or 'artist'.
// 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});
let event = context.params.event;
// importing dependencies
const {Spotify} = require('spotify-info.js');
const infos = new Spotify({
clientID: process.env.SPOTIFY_CLIENTID,
clientSecret: process.env.SPOTIFY_CLIENTSECRET,
});
// used to add commas between numbers
const commafy = require('commafy');
// used to convert milliseconds to "hours:minutes:seconds"
const convertMS = require('ms-convert');
// defining command options
let search = event.data.options[0].value;
let query = event.data.options[1].value;
// checking if the user passed 'song' through the `search` option
if (search === 'song') {
// getting track info
const tracks = await infos.searchTrack(query);
let track = tracks.map((track) => tracks[0]);
// getting song's artists' info using the map() function
let trackArtists = track.map((track) => track.artists);
// gets artist names & adds comma(s) between artist names if multiple artists are credited for the song
let aTrackArtists = trackArtists[0].map(({name}) => name).join(', ');
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `Hey **${event.member.user.username}#${event.member.user.discriminator}**! Here's info for "**${query}**".`,
embeds: [
{
title: track[0].name,
url: track[0].external_urls.spotify,
fields: [
{
name: `Artists`,
value: aTrackArtists,
inline: true,
},
{
name: `Album`,
value: track[0].album.name,
inline: true,
},
{
name: `Duration`,
value: convertMS(track[0].duration_ms),
inline: true,
},
{
name: `Album Released on`,
value: track[0].album.release_date,
inline: true,
},
],
thumbnail: {url: track[0].album.images[0].url},
color: 0x1db954,
},
],
});
}
// checks if the user passed 'album' through the `search` option
if (search === 'album') {
const albums = await infos.searchAlbum(query);
let album = albums.map((album) => albums[0]);
// getting album's artists using the map() function
let albumArtists = albums.map((album) => album.artists);
// adds comma(s) between the artist names if multiple artists are credited for the album
let aAlbumArtists = albumArtists[0].map(({name}) => name).join(', ');
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `Hey **${event.member.user.username}#${event.member.user.discriminator}**! Here's info for "**${query}**".`,
embeds: [
{
title: album[0].name,
url: album[0].external_urls.spotify,
fields: [
{
name: `Artists`,
value: aAlbumArtists,
inline: true,
},
{
name: `Songs`,
value: album[0].total_tracks,
inline: true,
},
{
name: `Type`,
value: album[0].type,
inline: true,
},
{
name: `Released on`,
value: album[0].release_date,
inline: true,
},
],
thumbnail: {url: album[0].images[0].url},
color: 0x1db954,
},
],
});
}
// checks if the user passed 'artist' through the `search` option
if (search === 'artist') {
const artists = await infos.searchArtist(query);
let artist = artists.map((artist) => artists[0]);
// the genres by default are returned like `pop,rock,jazz`
// this regex adds a space after each comma so it's easier to read to human eye
let artistGenres = `${artist[0].genres}`.replace(/,(?=[^\s])/g, ', ');
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `Hey **${event.member.user.username}#${event.member.user.discriminator}**! Here's info for "**${query}**".`,
embeds: [
{
title: artist[0].name,
url: artist[0].external_urls.spotify,
fields: [
{
name: `Followers`,
value: commafy(artist[0].followers.total),
inline: true,
},
{
name: `Popularity`,
value: commafy(artist[0].popularity),
inline: true,
},
{
name: `Genres`,
value: artistGenres,
},
],
thumbnail: {url: artist[0].images[0].url},
color: 0x1db954,
},
],
});
}