我无法在discord.py中的自定义检查中引发错误

2024-10-01 00:15:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须在discord bot中的自定义检查中引发自定义错误

我有以下代码:

class CommandDisabled(commands.CheckFailure):
    pass
def check_enabled(ctx):
    db = firebase.database()
    isEnabled = db.child('Disabled').child(str(ctx.guild.id)).child(ctx.command).get()
    if isEnabled.val() is None:
        return isEnabled.val() is None
    else:
        raise CommandDisabled
        return isEnabled.val() is None

@commands.command(name="seen",aliases=["lastseen","last"])
@commands.check(check_enabled)
async def _seen(self,ctx:commands.Context,user:discord.Member):
    db = firebase.database()
    seen_data = db.child("Last Seen").child(str(user.id)).get()
    if seen_data.val() is None:
        await ctx.send("**I have not seen that user so far.**")
    else:
        time = seen_data.val()["Time"]
        time_seen = datetime.strptime(time, "%Y-%m-%d %H:%M:%S.%f")
        final_time = datetime.utcnow()-time_seen
        total_min = round((final_time.total_seconds())/60)
        temp = 0
        while total_min >= 60:
            total_min -= 60
            temp += 1
        minutes = total_min-temp*(60)
        if temp == 0:
            em = discord.Embed(description=f"**`{user.name}` was last seen `{minutes} minutes` ago.**",color=discord.Color.random())
            await ctx.send(embed=em)
        else:
            em = discord.Embed(description=f"**`{user.name}` was last seen more than `{temp} hours` ago.**",color=discord.Color.random())
            await ctx.send(embed=em)

现在我在check_enabled函数中提出CommandDisabled错误。然后我按如下方式处理:

@commands.Cog.listener()
async def on_command_error(self,ctx,error):
    if isinstance(error, CommandDisabled):
        em = discord.Embed(description="This command is disabled in your      server. Ask admin to enable it",color=discord.Color.random())
        await ctx.send(embed=em)

但是它不工作,我想发送的嵌入不工作。命令只是停止工作,但通知它已禁用的嵌入不会被发送

这个代码在一个齿轮内

请帮帮我


Tags: childdbtimeischeckvaltempcommands