You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Discord机器人检测用户Spotify播放状态及显示曲目?

解决Discord机器人获取用户Spotify播放曲目的问题

你的问题核心是presence.activities是一个活动数组,不能直接访问.name属性,必须先遍历数组定位到对应的Spotify活动。另外,判断Spotify活动有更精准的方式,结合活动类型和名称双重校验更可靠。

以下是修正后的代码示例:

// 假设已获取目标用户对象mentionedMember
const spotifyActivity = mentionedMember.presence.activities.find(activity => 
  activity.type === 'LISTENING' && activity.name === 'Spotify'
);

if (spotifyActivity) {
  // 从活动属性中提取曲目详情
  const trackName = spotifyActivity.details;
  const artistName = spotifyActivity.state;
  // 发送到Discord频道的示例
  message.channel.send(`${mentionedMember.displayName} 正在播放:${trackName} - ${artistName}`);
} else {
  message.channel.send('该用户当前没有播放Spotify曲目');
}

额外注意事项:

  • 必须在Discord开发者门户的机器人设置中开启Presence Intent和Server Members Intent,否则无法获取用户的在线状态及活动信息。
  • 如果需要展示专辑封面,可从spotifyActivity.assets.largeImage提取,格式为https://i.scdn.co/image/${spotifyActivity.assets.largeImage.slice(8)}。

内容的提问来源于stack exchange,提问作者randomman010

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 21:18:36