如何在 discord.py 中使用 Spotify class?

How to use the Spotify class in discord.py?

好吧,我正在写一个命令,它只显示一些关于用户正在听的歌曲的信息,例如 k!spotify @user 显示歌曲名称、艺术家、专辑、专辑封面等。但是,我我在使用 Spotify class.

时遇到问题

这是我第一次尝试使用 Spotify class,但我认为这不是问题所在。我希望这些变量是特定于用户的,而不是 <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

我知道 discord.Spotify.title 等不会给我一些特定于用户的东西,但我尝试了 user.Spotify.title 和类似的东西都无济于事。我确定这只是我的误会,但是我应该如何在这里使用 spotify class?

Spotify is a type of Activity. You need to get Member.activities 和 select 只有那些也是 Spotify。我不知道是否有可能超过一个特定 activity,因此您可能必须考虑到这一点:

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)