@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.
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"
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. 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
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.
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.