简体   繁体   中英

Having problems with if & else for Spotify discord.py api

For the last bit of the code elif isinstance(activity == None, Spotify) & await ctx.send(f'{user.name} is not listening to anything:shrug:') There's no error code when running, however if my spotify isn't playing it doesn't print it when i type in the command

Full code below:

@commands.command()
    async def spot(self, ctx, user: discord.Member = None):
        if user == None:
            user = ctx.author
            pass
        if user.activities:
            for activity in user.activities:
                if isinstance(activity, Spotify):

                    embed = discord.Embed(
                        title=f"{user.name}'s Spotify",
                        description="Currently listening to {}".format(activity.title),
                        color=user.color)

                    embed.set_thumbnail(url=activity.album_cover_url)
                    embed.add_field(name="Artist", 
                                    value=activity.artist,
                                    inline=False)
                    embed.add_field(name="Album", 
                                    value=activity.album,
                                    inline=False)
                    
                    m1, s1 = divmod(int(activity.duration.seconds), 60)
                    song_length = f'{m1}:{s1}'
                    embed.add_field(name="Song Duration",
                                    value=song_length,
                                    inline=True)                   
                    embed.add_field(name="Track Link", 
                                    value=f"[{activity.title}](https://open.spotify.com/track/{activity.track_id})",
                                    inline=True)

                    embed.set_footer(text=f'Requested by : {ctx.author}',
                                    icon_url=ctx.author.avatar_url)
                    await ctx.send(embed=embed)

                elif isinstance(activity == None, Spotify):
                    await ctx.send(f'{user.name} is not listening to anything :shrug:')

I copied your code and tried it for myself. Changing the line elif isinstance(activity == None, Spotify): to else: fixed it for me.

But i noticed an error in your code, it checks every activity of a user and for every activity it will send a message. So when you're listening to Spotify it will say that you're listening and not listening at the same time. So i reworked your code so it first checks if there is a Spotify activity and then sends a message based on that.

Here is the reworked code:

@commands.command()
  async def spot(self, ctx, user: discord.Member=None):
    user = user if user else ctx.author

    activity = None

    for act in user.activities:
      if isinstance(act, Spotify):
        activity = act
        break
      
    if activity:
      embed = discord.Embed(
        title=f"{user.name}'s Spotify",
        description="Currently listening to {}".format(activity.title),
        color=user.color
      )

      embed.set_thumbnail(url=activity.album_cover_url)

      embed.add_field(
        name="Artist", 
        value=activity.artist,
        inline=False
      )

      embed.add_field(
        name="Album", 
        value=activity.album,
        inline=False
      )
      
      m1, s1 = divmod(int(activity.duration.seconds), 60)

      song_length = f'{m1}:{s1}'

      embed.add_field(
        name="Song Duration",
        value=song_length,
        inline=True
      )                   
      embed.add_field(
        name="Track Link", 
        value=f"[{activity.title}](https://open.spotify.com/track/{activity.track_id})",
        inline=True
      )

      embed.set_footer(text=f'Requested by : {ctx.author}', icon_url=ctx.author.avatar_url)
      
      await ctx.send(embed=embed)

    else:
      await ctx.send(f'{user.name} is not listening to anything :shrug:')

You can just completely remove the elif you will save the msg in a variable and then check is a message is sent or not. This method is valid even if the user does not have user.activities

@commands.command()
    async def spot(self, ctx, user: discord.Member = None):
        if user == None:
            user = ctx.author
            pass
        if user.activities:
            for activity in user.activities:
                if isinstance(activity, Spotify):

                    embed = discord.Embed(
                        title=f"{user.name}'s Spotify",
                        description="Currently listening to {}".format(activity.title),
                        color=user.color)

                    # add fields here

                    msg = await ctx.send(embed=embed)
                    break

        if not msg:
            await ctx.send('nothing')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM