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

时间:2019-05-07 18:10:15

标签: python discord.py-rewrite

好的,所以我正在写一个命令,该命令仅显示一些有关用户正在收听的歌曲的信息,例如k!spotify @user显示了歌曲名称,艺术家,专辑,专辑封面等。但是, Spotify类出现问题。

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

2 个答案:

答案 0 :(得分:1)

SpotifyActivity的一种。您需要获取Member.activities并仅选择也是Spotify的那些。我不知道是否可能有一项以上的特定活动,因此您可能必须考虑到这一点:

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}")

答案 1 :(得分:0)

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)