Skip to content

Instantly share code, notes, and snippets.

@trueharuu
Created January 23, 2021 00:52
Show Gist options
  • Save trueharuu/f8e52a032c53a5cb1001c3125caacfe7 to your computer and use it in GitHub Desktop.
Save trueharuu/f8e52a032c53a5cb1001c3125caacfe7 to your computer and use it in GitHub Desktop.
Get info on a song on Spotify using Pylon and Slash Commands
export async function api(
url: string,
type: 'json' | 'text' | 'buffer' = 'json'
) {
const a = await fetch(url);
switch (type) {
case 'buffer':
return await a.arrayBuffer();
case 'text':
return await a.text();
case 'json':
return await a.json();
}
}
interface Track {
id: string;
name: string;
explicit: boolean;
popularity: number;
album: {
id: string;
name: string;
size: number;
date: string;
icon: {
height: number;
url: string;
width: number;
};
};
length: number;
artists: { id: string; name: string }[];
}
export async function spotifySearch(search: string) {
// https://evan.lol/spotify/search/top?q=
const cr = await api(
'https://evan.lol/spotify/search/top?q=' + encodeURIComponent(search),
'json'
);
if (cr.status == 404) {
return undefined;
}
return cr as Track;
}
discord.interactions.commands.register(
{
name: 'spotify',
description: 'Songs',
showSourceMessage: false,
options: (opt) => ({
search: opt.string({
name: 'search',
description: 'The song'
})
})
},
async (_, { search }) => {
const track = await spotifySearch(search);
if (!track) {
return await _.respondEphemeral('Not found');
}
const embed = new discord.Embed();
embed.setAuthor({
name: track.artists[0].name,
iconUrl: track.album.icon.url
});
embed.setThumbnail({ url: track.album.icon.url });
embed.setTitle(track.name);
embed.setUrl(`https://open.spotify.com/track/${track.id}`);
embed.addField({
name: '❯ Track Info',
value: `Length: **${fancyTimeFormat(track.length)}**
Explicit: **${track.explicit ? 'Yes' : 'No'}**
Popularity: **${track.popularity}/100**`
});
embed.addField({
name: '❯ Album Info',
value: `Name: [**\`${
track.album.name
}\`**](https://open.spotify.com/album/${track.album.id})
Tracks: **${track.album.size}**
Created At: ${simpleGetLongAgo(
+new Date(track.album.date)
)} ago **[**\`${new Date(track.album.date).toLocaleDateString()}\`**]**`
});
embed.addField({
name: '❯ Artists',
value: track.artists
.map((e) => `[\`${e.name}\`](https://open.spotify.com/artist/${e.id})`)
.join(', ')
});
embed.setColor(embedColor);
await _.acknowledge(true);
await _.respond({
embeds: [embed]
});
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment