如何解决如何处理自定义 NoneType 错误?
我已经在我的机器人中实现了我自己的功能/检查。无论如何,控制台中没有正常的AttributeError
作为输出,但是例如:'NoneType' object has no attribute 'requested_by'
,所以没有AttributeError
,因为它是我自己的函数。
有没有办法也通过机器人输出这个错误,并可能用 try/except
语句处理它? (我确实尝试过没有成功)
这是一个例子:
@commands.command(aliases=["disconnect"])
@commands.guild_only()
@commands.check(is_audio_requester) # check what it is about
async def leave(self,ctx):
"""Leaves the voice channel,if currently in one."""
client = ctx.guild.voice_client
state = self.get_state(ctx.guild)
if client and client.channel:
await client.disconnect()
del self.states[ctx.guild.id]
else:
raise commands.CommandError("Not in a voice channel.")
await ctx.send(
embed=discord.Embed(title=":wave::skin-tone-3: I left the voice channel.",color=self.bot.get_embed_color(ctx.guild)))
无论出于何种原因,部分我仍然得到:
'NoneType' object has no attribute 'requested_by'
导致错误的函数:
async def is_audio_requester(ctx):
"""Checks that the command sender is the song requester."""
music = ctx.bot.get_cog("Music")
state = music.get_state(ctx.guild)
permissions = ctx.channel.permissions_for(ctx.author)
if permissions.administrator or state.is_requester(ctx.author):
return True
else:
raise commands.CommandError(
"You need to be the song requester to do that.")
(没有打印!)
我无法处理此错误,然后我可能必须从频道中删除机器人或重新启动它。如果我在没有 leave
的情况下执行命令 commands.check
,它当然可以工作。 有什么办法可以解决这个问题吗?
我已经通过 leave.error
尝试过我自己的自定义错误,但这似乎不起作用。正如预期的那样,即使传递像 if XXX is None
这样的东西也不起作用。在这种情况下,我也不能使用 Cog.cog.command_error
文档,至少看起来是这样。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。