Python 取消命令
我正在发出命令以启用/禁用每个通道的bot命令。尽管在数据库中发现取消命令时遇到问题Python 取消命令,python,python-3.x,discord.py,discord.py-rewrite,Python,Python 3.x,Discord.py,Discord.py Rewrite,我正在发出命令以启用/禁用每个通道的bot命令。尽管在数据库中发现取消命令时遇到问题 @Nao.event 异步def on_命令(ctx): 如果ctx.command.parent为无: database=mysql.connector.connect( 主机=“”, 用户=“”, passwd=“”, 数据库=“” ) cursor=database.cursor() query=“从cmdsdisabled中选择命令,其中通道=%s和命令=%s” 值=(ctx.channel.id、ct
@Nao.event
异步def on_命令(ctx):
如果ctx.command.parent为无:
database=mysql.connector.connect(
主机=“”,
用户=“”,
passwd=“”,
数据库=“”
)
cursor=database.cursor()
query=“从cmdsdisabled中选择命令,其中通道=%s和命令=%s”
值=(ctx.channel.id、ctx.command.name)
cursor.execute(查询、值)
结果=cursor.fetchall()
对于结果中的命令:
等待ctx.send('该命令在此通道中被禁用')
cursor.close()
返回
代码工作,它检查命令是否在数据库中,如果在数据库中,bot会说“该命令在此通道中被禁用”。但是,它会继续执行实际的命令
我希望它不要继续执行实际命令,只说“该命令在此通道中被禁用”。如果在数据库中找到该命令。您可以编写一个引发自定义异常的检查,然后在自定义异常处理程序中跨所有命令处理该异常。下面的代码只要求您编写一些
命令\u被禁用
函数来实际确定命令是否被禁用
from discord.ext.commands import DisabledCommand, check, Bot
bot = Bot("!")
class DisabledInChannel(DisabledCommand):
pass
def check_disabled():
def predicate(ctx):
if command_is_disabled(ctx.command.name, ctx.channel.id):
raise DisabledInChannel(f"{ctx.command.name} is disabled in {ctx.channel.name}")
return True
return check(predicate)
@bot.command()
@check_disabled()
async def some_command(ctx):
...
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, DisabledInChannel):
await ctx.send(error.message)
else:
raise error