简体   繁体   中英

How to use the Spotify class in discord.py?

Okay so I am writing a command which just shows some info about what song a user is listening to, for example k!spotify @user shows the song name, artist, album, album cover etc etc. However, I am having issues with the Spotify class.

This is my first time trying to use the Spotify class, but I don't think that's the problem. I would like these variables to be user-specific and not <property object at 0x0460B7E0> :

async def spotify(ctx, user: discord.Member=None):
    if not user:
        user = ctx.message.author.id
    else:
        user = user.id

    sname = discord.Spotify.title
    sartists = discord.Spotify.artists
    album = discord.Spotify.album
    palbum = discord.Spotify.album_cover_url
    duration = discord.Spotify.duration

I am aware that discord.Spotify.title etc won't give me something user-specific, but I tried user.Spotify.title and things along those lines to no avail. I am sure this is just a misunderstanding from me, but how should I use the spotify class here?

Spotify is a type of Activity . You need to get Member.activities and select only the ones that are also Spotify . I don't know if it's possible to have more than one of a particular activity, so you might have to account for that:

from discord import Spotify

@bot.command()
async def spotify(ctx, user: discord.Member=None):
    user = user or ctx.author
    for activity in user.activities:
        if isinstance(activity, Spotify):
            await ctx.send(f"{user} is listening to {activity.title} by {activity.artist}")
from discord import Spotify

@bot.command()
async def spotify(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 = "Listening to {}".format(activity.title),
                    color = 0xC902FF)
                embed.set_thumbnail(url=activity.album_cover_url)
                embed.add_field(name="Artist", value=activity.artist)
                embed.add_field(name="Album", value=activity.album)
                embed.set_footer(text="Song started at {}".format(activity.created_at.strftime("%H:%M")))
                await ctx.send(embed=embed)

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