我正在尝试在discord bot中创建自定义错误处理程序。因此,我有一个名为MyBot的Cog,我正在尝试创建一个错误处理程序类,该类将处理与该类相关的错误
我有下面的代码,它不工作
我希望在遇到错误时(当我调用roll命令时)调用roll_error(),但它只是在控制台上引发了错误,并且没有返回任何结果
# BaseErrorHandler.py
from discord.ext import commands
class ErrorHandler(commands.errors.CommandError):
# Create functions to handle errors globally
pass
# BotErrorHandler.py
import ErrorHandler
from discord.ext import commands
class BotErrorHandler(ErrorHandler):
# Create functions to handle errors related to MyBot
@commands.command()
async def roll(self):
pass
@roll.error
async def roll_error(self, ctx, error):
if isinstance(error, errors.BadArgument):
await ctx.send('Please enter a valid number')
# MyBotCog.py
import BotErrorHandler
from discord.ext import commands
class MyBot(commands.Cog, BotErrorHandler):
def __init__(self, bot, *args, **kwargs):
self.bot = bot
@commands.command()
async def roll(self, ctx, low: int, high: int):
await ctx.send(random.randint(low, high)
如何构造类以使此层次结构正常工作
您可以使用
Cog.cog_command_error
函数来处理cog中的所有错误参考资料:
相关问题 更多 >
编程相关推荐