Discord.py重写自定义错误 - python

我对编码非常陌生,我想知道如何在此处实现自定义错误,例如“缺少开发人员角色”:

    @bot.command()
@commands.has_any_role("Temp.", "Owner")
async def sh(ctx):
    await ctx.message.add_reaction(':true:508022488093949973')
    await ctx.send("<a:siren:507952050181636098> `Shutting down` <a:siren:507952050181636098>")
    await bot.logout()

我有一个像这样的简单处理程序

@bot.event
async def on_command_error(ctx, error):
    await ctx.message.add_reaction(':false:508021839981707304')
    await ctx.send("<a:siren:507952050181636098> `Invalid command` <a:siren:507952050181636098>")

但它总是输出无效的命令

python参考方案

您可以检查error的类以确定要处理的错误类型。如果将其与特定于命令的error handler配对,则可以编写一个响应来告诉用户缺少的内容:

@sh.error
async def sh_error(ctx, error):
    if isinstance(error, commands.CheckFailure):
        await ctx.send("You do not have the correct roles Temp. or Owner")

@bot.event
async def on_command_error(ctx, error):
    if not isinstance(error, commands.CheckFailure): 
        await ctx.message.add_reaction(':false:508021839981707304')
        await ctx.send("<a:siren:507952050181636098> `Invalid command` <a:siren:507952050181636098>")

Discord.py重写所有命令的收集列表 - python

我正在尝试获取Discord机器人中所有命令的列表以进行重写。我正在使用Python 3.6编写此我试图通过执行打印命令列表print(bot.commands)这仅为我提供以下回报:{<discord.ext.commands.core.Command object at 0x00000209EE6AD4E0>, <discord.ext…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

如何检查ctx是否为私人频道(dm频道) - python

本质上,我希望此命令仅在DMS中运行,并且不能在我的机器人所在的服务器中激活,我知道有一个检查器,但我不确定如何准确地使用它,感谢您的帮助class verify: def check(): #something @bot.command() @commands.check(check) async def verify(ctx): python大神给出的解…

Coverage.py:如何为从外壳程序脚本调用的多个python脚本附加结果 - python

我正在使用带有参数调用多个python代码的shell脚本。现在,我的要求是收集整个项目的报告。任何想法如何在整个代码上运行coverage.py并生成合并的报告。以下是我正在使用的shell脚本的高级模板。如果有人可以指导如何实现上述要求。#!/bin/bash variable=$1 /usr/bin/python python1.py $variabl…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…