如何使python执行异常,而不是忽略异常(discord.py)
因此,我正在编写一个脚本,它允许我的discord机器人在发出命令后使用lyricgenius API返回歌词。然而,每当我故意输入胡言乱语,这样机器人就不会在AttributeError异常块中找到歌词并执行代码时,控制台就会说该异常被忽略。我使用的是discord.py版本1.01。有什么想法吗?提前谢谢如何使python执行异常,而不是忽略异常(discord.py),python,exception,events,discord.py,attributeerror,Python,Exception,Events,Discord.py,Attributeerror,因此,我正在编写一个脚本,它允许我的discord机器人在发出命令后使用lyricgenius API返回歌词。然而,每当我故意输入胡言乱语,这样机器人就不会在AttributeError异常块中找到歌词并执行代码时,控制台就会说该异常被忽略。我使用的是discord.py版本1.01。有什么想法吗?提前谢谢 def fetch_lyrics(array): song = genius.search_song(title=array[0],artist=array[1]) ret
def fetch_lyrics(array):
song = genius.search_song(title=array[0],artist=array[1])
return song.lyrics
@client.event
async def on_message(message):
if message.content.startswith("hello"):
await message.channel.send("hello")
if message.content.startswith("$lyrics"):
global lyrics
global searchstring
pre_search_array = message.content.split()
del pre_search_array[0]
for z in pre_search_array:
searchstring += z+" "
search_array = searchstring.split(",")
if len(fetch_lyrics(search_array)) > 2000:
pass
else :
try:
await message.channel.send(fetch_lyrics(search_array))
except AttributeError:
await message.channel.send("Could not find the song")
#resets the list
searchstring = ""
pre_search_array.clear()
search_array.clear()
Error Message:
Ignoring exception in on_message
Traceback (most recent call last):
File "/Users/kevinhadinata/Desktop/pythonProject2/venv/lib/python3.7/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "/Users/kevinhadinata/Desktop/pythonProject2/main.py", line 38, in on_message
if len(fetch_lyrics(search_array)) > 2000:
File "/Users/kevinhadinata/Desktop/pythonProject2/main.py", line 16, in fetch_lyrics
return song.lyrics
AttributeError: 'NoneType' object has no attribute 'lyrics'
Fatal read error on socket transport
protocol: <asyncio.sslproto.SSLProtocol object at 0x102ae0908>
transport: <_SelectorSocketTransport fd=11 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/asyncio/selector_events.py", line 801, in _read_ready__data_received
data = self._sock.recv(self.max_size)
TimeoutError: [Errno 60] Operation timed out
def fetch_歌词(数组):
歌曲=天才。搜索歌曲(标题=数组[0],艺术家=数组[1])
返回歌曲。歌词
@客户端事件
异步def on_消息(消息):
如果message.content.startswith(“hello”):
等待消息。频道。发送(“你好”)
如果message.content.startswith(“$歌词”):
全球歌词
全局搜索字符串
pre_search_array=message.content.split()
del pre_搜索_数组[0]
对于预搜索数组中的z:
searchstring+=z+“”
search\u array=searchstring.split(“,”)
如果len(获取歌词(搜索数组))>2000:
通过
其他:
尝试:
等待消息.channel.send(获取歌词(搜索数组))
除属性错误外:
等待message.channel.send(“找不到歌曲”)
#重置列表
searchstring=“”
预搜索数组。清除()
搜索_数组。清除()
错误消息:
忽略on_消息中的异常
回溯(最近一次呼叫最后一次):
文件“/Users/kevinhadinata/Desktop/pythonProject2/venv/lib/python3.7/site packages/discord/client.py”,第343行,在运行事件中
等待coro(*args,**kwargs)
文件“/Users/kevinhadinata/Desktop/pythonProject2/main.py”,第38行,在on_消息中
如果len(获取歌词(搜索数组))>2000:
文件“/Users/kevinhadinata/Desktop/pythonProject2/main.py”,第16行,在fetch_歌词中
返回歌曲。歌词
AttributeError:“非类型”对象没有属性“歌词”
套接字传输上出现致命的读取错误
协议:
运输:
回溯(最近一次呼叫最后一次):
文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/Python3.7/asyncio/selector\u events.py”,第801行,在“读取准备好的数据”中
数据=self.\u sock.recv(self.max\u大小)
TimeoutError:[Errno 60]操作超时
如果您在len(fetch_歌词(search_数组))>2000:
中使用fetch_歌词
(导致错误),您必须捕获该错误。直接方法是将其移动到try和except
试试看:
如果len(获取歌词(搜索数组))>2000:
通过
除属性错误外:
打印('不是有效的歌曲')
您还可以在
获取歌词中引发自定义错误并捕获它。您可以显示终端中打印的错误吗?@Ceres done。这是现在的问题啊,谢谢。我真是太蠢了,哈哈。我只是注意到上面说错误就在那个地方。应用了你的解决方案,效果很好。