[英]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对于代码的最后一点elif isinstance(activity == None, Spotify)
& await ctx.send(f'{user.name} is not listening to anything:shrug:')
运行时没有错误代码,但是如果我当我输入命令时,Spotify 不播放它不打印它
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.将行elif isinstance(activity == None, Spotify):
更改为else:
为我修复了它。
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.因此,当您收听 Spotify 时,它会说您同时在听和不在听。 So i reworked your code so it first checks if there is a Spotify activity and then sends a message based on that.所以我修改了你的代码,所以它首先检查是否有 Spotify 活动,然后根据它发送消息。
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.您可以完全删除将消息保存在变量中的elif
,然后检查消息是否已发送。 This method is valid even if the user does not have user.activities
即使用户没有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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.