Python 如何在discord.py中错误处理spotify命令?
这是我的spotify命令,虽然它工作正常,但我不知道如何处理它 例如,如果用户没有收听Spotify,我想让它发送一条类似“您没有收听Spotify”的消息Python 如何在discord.py中错误处理spotify命令?,python,discord,discord.py,Python,Discord,Discord.py,这是我的spotify命令,虽然它工作正常,但我不知道如何处理它 例如,如果用户没有收听Spotify,我想让它发送一条类似“您没有收听Spotify”的消息 非常感谢您的帮助:)首先,欢迎使用堆栈溢出。可以尝试以下方法 @commands.command() async def spotify(self, ctx, user: discord.Member = None): if user == None: user = ctx.author pass
非常感谢您的帮助:)首先,欢迎使用堆栈溢出。可以尝试以下方法
@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)
只需在命令末尾添加
wait ctx.send(“您没有收听Spotify”)
,并在if instance(activity,Spotify)=True
语句末尾添加return
语句。一旦找到用户正在收听的内容并发送嵌入的消息,它将退出,否则它将继续走到末尾并发送您没有收听的spotify
for activity in user.activities:
if activity!= spotify :
await ctx.send("You are not listening to Spotify ")
break
注意:我在我的代码中修改了
if isinstance(activity,Spotify)=True:
,因为它对我不起作用,但你可以保留它,因为你说它对你起作用。你有没有试过在你的代码末尾使用else
语句?我还有其他想法,我会尝试。tyit因为某些原因不起作用,它只是不再把Spotify作为一种活动来阅读,即使我让它听音乐。首先ty。如果是Instance(activity,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