【问题标题】:How can i make a spotify command?我怎样才能制作一个 spotify 命令?
【发布时间】:2019-06-24 20:44:04
【问题描述】:

Spotify 命令

我尝试了很多东西,但我无法让它工作

@client.command()
async def spotify(ctx, user: discord.Member = None):
        embedspotify = discord.Embed(title=f"{user.name}'s Spotify", color=0x1eba10)
        embedspotify.add_field(name="Song", value=Spotify.title)
        embedspotify.add_field(name="Artist", value=Spotify.artist)
        embedspotify.add_field(name="Album", value=Spotify.album)
        embedspotify.set_thumbnail(url=Spotify.album_cover_url)

我正在尝试获取歌曲的 spotify 标题、艺术家的姓名和我使用命令的人的专辑名称(当他们正在收听 spotify 时)

【问题讨论】:

    标签: python python-3.x spotify discord.py


    【解决方案1】:

    您需要从Member 正在执行的活动列表中获取Spotify 实例:

    @client.command()
    @commands.guild_only() # We can only access activities from a guild
    async def spotify(ctx, user: discord.Member = None):
        user = user or ctx.author  # default to the caller
        spot = next((activity for activity in user.activities if isinstance(activity, discord.Spotify)), None)
        if spot is None:
            await ctx.send(f"{user.name} is not listening to Spotify")
            return
        embedspotify = discord.Embed(title=f"{user.name}'s Spotify", color=0x1eba10)
        embedspotify.add_field(name="Song", value=spot.title)
        embedspotify.add_field(name="Artist", value=spot.artist)
        embedspotify.add_field(name="Album", value=spot.album)
        embedspotify.set_thumbnail(url=spot.album_cover_url)
        await ctx.send(embed=embedspotify)
    

    【讨论】:

      【解决方案2】:

      命令效果很好,我决定通过track_id添加嵌入轨道链接

      [{spot.title}](https://open.spotify.com/track/{spot.track_id})

      async def spotify(self, ctx, user: discord.Member = None):
          user = user or ctx.author  
          spot = next((activity for activity in user.activities if isinstance(activity, discord.Spotify)), None)
          if spot is None:
              await ctx.send(f"{user.name} is not listening to Spotify")
              return
          embed = discord.Embed(title=f"{user.name}'s Spotify", color=spot.color)
          embed.add_field(name="Song", value=spot.title)
          embed.add_field(name="Artist", value=spot.artist)
          embed.add_field(name="Album", value=spot.album)
          embed.add_field(name="Track Link", value=f"[{spot.title}](https://open.spotify.com/track/{spot.track_id})")
          embed.set_thumbnail(url=spot.album_cover_url)
          await ctx.send(embed=embed)
              print(f'{user.name} in {ctx.guild} called the command !spotify')```
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-02
        • 2016-11-25
        • 2011-12-21
        • 2021-09-27
        • 2012-09-21
        • 1970-01-01
        • 2021-10-10
        • 2017-05-13
        相关资源
        最近更新 更多