so I was trying to get Spotify Current Playing details from users' activity using discord.js
The thing is, it always returns "You're not playing anything right now" even when I'm playing a track from Spotify.
Here's my code:
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
const { CustomRGB } = require("discordjs-colors-bundle");
module.exports = {
data: new SlashCommandBuilder()
.setName('spotify')
.setDescription('Spotify informations!')
.addSubcommand((sub) =>
sub.setName('current').setDescription('Get your current playing status')
),
async execute(interaction, client) {
if (interaction.options.getSubcommand() == "current") {
const member = interaction.member;
const Embed = new EmbedBuilder()
.setTitle(`${interaction.user.username}'s current playing status`);
if ( member.presence.activities[0].name != "spotify") {
Embed.setColor(CustomRGB(255, 0, 0));
Embed.setDescription("User is not playing anything right now.");
} else {
const activity = member.presence.activities[0];
Embed.addFields(
{ name: "Track Name", value: `${activity.details}`, inline: true },
{ name: "Artist Name", value: `${activity.state}`, inline: true }
);
Embed.setColor(CustomRGB(0, 255, 170));
}
return interaction.reply({ embeds: [Embed], ephemeral: true });
}
},
};
I was expecting to get the current Spotify playing track name and artist name from the activity.
You're code checks if the activity name is spotify
.
// ...
if (member.presence.activities[0].name != "Spotify") {
// ...
If you take a look at the presence activity structure, this is what it looks like:
Activity {
name: 'Spotify',
type: 2,
url: null,
details: 'Some Song',
state: 'Author 1; Author 2',
applicationId: null,
timestamps: { start: 2023-07-08T14:04:32.776Z, end: 2023-07-08T14:08:07.175Z },
party: { id: 'spotify:838620835282812969' },
assets: RichPresenceAssets {
largeText: 'Song',
smallText: null,
largeImage: 'spotify:ab1234',
smallImage: null
},
flags: ActivityFlagsBitField { bitfield: 48 },
emoji: null,
buttons: [],
createdTimestamp: 1688825073865
}
I recommend checking if the user has a presence first, so that you don't run into further errors.