【问题标题】:lyrics command in discord.py keyerrordiscord.py keyerror 中的歌词命令
【发布时间】:2021-07-28 09:33:37
【问题描述】:

所以我最近做了一个歌词命令和它的工作!唯一的问题是,当我写胡言乱语时,它发送了错误“发生错误:命令引发异常:KeyError:'lyrics'”,我为此添加了一个除了 KeyError 错误处理程序,但它不起作用,知道为什么吗?

@commands.command()
async def lyrics(self, ctx,*, title):
     url = f"https://some-random-api.ml/lyrics?title={title}"
     response = requests.get(url)
     json_data = json.loads(response.content)
     lyrics = json_data['lyrics']
     try:
       if len(lyrics) > 2048:
          em = discord.Embed(title=title,description = f"I wasn't able to send the lyrics for that song since it exceeds 2000 characters. However, here's the file for the lyrics!",color=0xa3a3ff)
          await ctx.send(embed=em)
          file = open("lyrics.txt", "w")
          file.write(lyrics)
          file.close() 
          return await ctx.send(file=discord.File("lyrics.txt"))
       else:
          em = discord.Embed(title=title,description=lyrics,color=0xa3a3ff)
          await ctx.send(embed=em)
     except KeyError:
       em = discord.Embed(title="Aw Snap!",description="I wasn't able to find the lyrics of that song.",color = 0xa3a3ff)
       em.set_thumbnail(url='https://cdn.discordapp.com/attachments/830818408550629407/839555682436251698/aw_snap_large.png')
       await ctx.send(embed=em)

【问题讨论】:

标签: python discord discord.py


【解决方案1】:

您的 try-except 块位于导致错误的行之后。

# ...
lyrics = json_data['lyrics'] # Erroneous line
try:
    # ...

如果您希望异常发生并被捕获,就像 Python 的方式一样,您可以将 lyrics 声明行移动到 try 块中。

或者,您可以使用 get() 安全地获取值并将 try-except 块替换为 if-else:

# ...
lyrics = json_data.get('lyrics')
if lyrics:
    # Show lyrics
    if len(lyrics) > 2048:
        em = discord.Embed(title=title,description = f"I wasn't able to send the lyrics for that song since it exceeds 2000 characters. However, here's the file for the lyrics!",color=0xa3a3ff)
        await ctx.send(embed=em)
        # ...
else:
    # Show message
    em = discord.Embed(title="Aw Snap!",description="I wasn't able to find the lyrics of that song.",color = 0xa3a3ff)
    em.set_thumbnail(url='https://cdn.discordapp.com/attachments/830818408550629407/839555682436251698/aw_snap_large.png')
    await ctx.send(embed=em)

【讨论】:

  • 在尝试了“json_data.get('lyrics')”代码后,它工作了,但现在我在写乱码时遇到另一个错误:发生错误:命令引发异常:TypeError: object of type 'NoneType' 没有 len() @ankurbohra04
  • 我相信你没有把try:branch的内容放在if歌词中: 分支
  • 我做到了,它可以工作,但错误处理程序没有
  • 我明白了,这应该是替换错误处理程序,而不是帮助它。即使没有找到歌词,json_data.get() 也不会出错,因此不需要错误处理。我已经编辑了我的答案以指示分支。
  • if not len(lyrics): em = discord.Embed(title="Aw Snap!",description="我找不到那首歌的歌词。",color = 0xa3a3ff ) em.set_thumbnail(url='cdn.discordapp.com/attachments/830818408550629407/…) 等待 ctx.send(embed=em)
【解决方案2】:

错误发生在这一行:lyrics = json_data['lyrics']。你应该在它周围加上try-except。或处理丢失歌词的阻塞if 语句:

lyrics = json_data.get('lyrics', None)
if not lyrics:
    em = discord.Embed(title="Aw Snap!",description="I wasn't able to find the lyrics of that song.",color = 0xa3a3ff) 
    em.set_thumbnail(url='https://cdn.discordapp.com/attachments/830818408550629407/839555682436251698/aw_snap_large.png')
    await ctx.send(embed=em)
    return
...

【讨论】:

  • 你还在收到 KeyError 吗?
  • 不,我收到一个新错误:“发生错误:命令引发异常:TypeError:'NoneType' 类型的对象没有 len()”
  • 我已经将代码更改为 if else if 语句现在顺便说一句
猜你喜欢
  • 2021-07-27
  • 1970-01-01
  • 2022-12-10
  • 2021-03-26
  • 2020-09-17
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
相关资源
最近更新 更多