Skip to content

Instantly share code, notes, and snippets.

@Harleythetech
Created April 13, 2023 16:54
Show Gist options
  • Save Harleythetech/e09b50857004bc4da29b42282d7b9670 to your computer and use it in GitHub Desktop.
Save Harleythetech/e09b50857004bc4da29b42282d7b9670 to your computer and use it in GitHub Desktop.
a discord js that pulls the spotify Rich Presence status and a bot replies it back in embed
const { EmbedBuilder, SlashCommandBuilder } = require('discord.js');
const convert = import('parse-ms');
module.exports = {
data: new SlashCommandBuilder()
.setName('spotify')
.setDescription('Displays the currently playing song on Spotify for a given user')
.addUserOption(option =>
option.setName('user')
.setDescription('The user to show the currently playing song for')
.setRequired(false)
),
async execute(interaction) {
const user = interaction.options.getUser('user') || interaction.user;
const member = interaction.guild.members.cache.get(user.id);
if (!member.presence || !member.presence.activities) {
return interaction.reply(`The user is not currently playing any song on Spotify`);
}
const status = member.presence.activities.find(activity => activity.type === 'LISTENING' && activity.name === 'Spotify');
if (!status) {
return interaction.reply(`The user is not currently playing any song on Spotify`);
}
const image = `https://i.scdn.co/image/${status.assets.largeImage.slice(8)}`;
const url = `https://open.spotify.com/track/${status.syncID}`;
const name = status.details;
const artist = status.state;
const album = status.assets.largeText;
const duration = status.timestamps.end - status.timestamps.start;
const timeConvert = convert(duration);
const minutes = timeConvert.minutes < 10 ? `0${timeConvert.minutes}` : timeConvert.minutes;
const seconds = timeConvert.seconds < 10 ? `0${timeConvert.seconds}` : timeConvert.seconds;
const time = `${minutes}:${seconds}`;
const embed = new EmbedBuilder()
.setAuthor(user.tag, user.displayAvatarURL({ dynamic: true }))
.setTitle('Currently Playing on Spotify')
.setColor('GREEN')
.setThumbnail(image)
.addField('Song', `\`${name}\``)
.addField('Artist', `\`${artist}\``)
.addField('Album', `\`${album}\``)
.addField('Duration', `\`${time}\``)
.addField('Listen Now', `[${artist} - ${name}](${url})`)
.setFooter(`Requested by ${interaction.user.tag}`, interaction.user.displayAvatarURL({ dynamic: true }))
.setTimestamp();
return interaction.reply({ embeds: [embed] });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment