[英]How can i error handle the spotify command in discord.py?
@commands.command()
async def spotify(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)== True:
embed = discord.Embed(
title=f"{user.name}'s Spotify",
description="Listening to {}".format(activity.title),
color=activity.colour)
duration = str(activity.duration)
finalduration = duration[3:7]
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.add_field(name="Song Duration", value=finalduration)
embed.set_footer(text="Song started at {}".format(activity.created_at.strftime("%H:%M.%p")))
embed.url = (f"https://open.spotify.com/embed/track/{activity.track_id}")
await ctx.send(embed=embed)
this is my spotify command and although it works fine I cant figure out how to error handle it.这是我的 spotify 命令,虽然它工作正常,但我不知道如何错误处理它。
For example, if the user is not listening to Spotify I want to make it send a message like "You are not listening to Spotify"例如,如果用户没有在听 Spotify,我想让它发送一条消息,比如“你没有在听 Spotify”
Any help is appreciated :)任何帮助表示赞赏:)
First of all welcome to stack overflow.Maybe try something like首先欢迎堆栈溢出。也许尝试类似的东西
for activity in user.activities:
if activity!= spotify :
await ctx.send("You are not listening to Spotify ")
break
Just add await ctx.send("You are not listening to Spotify ")
at the end of your command, and add a return
statement at the end of your if instance(activity, Spotify) == True
statement.只需在命令末尾添加
await ctx.send("You are not listening to Spotify ")
,并在if instance(activity, Spotify) == True
语句末尾添加return
语句。 It will exit once it finds what the user is listening and sends the embeded message, and otherwise it will keep going to the end and send You are not listening to spotify
一旦找到用户正在收听的内容并发送嵌入消息,它将退出,否则它将继续到最后并发送
You are not listening to spotify
intents = discord.Intents().all()
client = commands.Bot(command_prefix='.', intents=intents)
@client.command(pass_context=True)
async def spotify(ctx, user: discord.Member = None):
if user == None:
user = ctx.author
pass
if user.activities:
for activity in user.activities:
if str(activity).lower() == "spotify":
embed = discord.Embed(
title=f"{user.name}'s Spotify",
description="Listening to {}".format(activity.title),
color=activity.colour)
duration = str(activity.duration)
finalduration = duration[3:7]
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.add_field(name="Song Duration", value=finalduration)
embed.set_footer(text="Song started at {}".format(activity.created_at.strftime("%H:%M.%p")))
embed.url = (f"https://open.spotify.com/embed/track/{activity.track_id}")
await ctx.send(embed=embed)
return
await ctx.send("User is not doing anything")
return
Note: I modify the if isinstance(activity, Spotify)== True:
in my code because it wasn't working for me, but you can keep it since you said it was working for you.注意:我在我的代码中修改了
if isinstance(activity, Spotify)== True:
因为它对我不起作用,但你可以保留它,因为你说它对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.