我是刚开始使用齿轮的人,刚刚发现了有关cog_command_error的信息。我试图将其应用于我的代码
@commands.Cog.listener()
async def cog_command_error(self, ctx, error):
"""Reports errors to users"""
m, s = divmod(error.retry_after, 60)
h, m = divmod(m, 60)
if isinstance(error, commands.errors.MissingRole):
await ctx.send("You do not have the correct role for this command.")
elif isinstance(error,commands.errors.CheckFailure):
await ctx.send("This command is blocked from this channel please use it in <#597918255188803588>")
elif isinstance(error, commands.CommandOnCooldown):
errMessage = f"{ctx.author.mention}, Try again in {round(h)} hours, {round(m)} minutes, and {round(s)} seconds."
await ctx.send(errMessage)
但是我仍然收到此错误:
File "C:\Users\Jimmy\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 596, in _load_from_module_spec
spec.loader.exec_module(lib)
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "c:\Users\Jimmy\Desktop\GovsM\cogs\fun.py", line 19, in <module>
class fun(commands.Cog):
File "C:\Users\Jimmy\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\cog.py", line 124, in __new__
raise TypeError(no_bot_cog.format(base, elem))
TypeError: Commands or listeners must not start with cog_ or bot_ (in method fun.cog_command_error)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Users\Jimmy\.vscode\extensions\ms-python.python-2020.2.63072\pythonFiles\ptvsd_launcher.py", line 48, in <module>
main(ptvsdArgs)
File "c:\Users\Jimmy\.vscode\extensions\ms-python.python-2020.2.63072\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
run()
File "c:\Users\Jimmy\.vscode\extensions\ms-python.python-2020.2.63072\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Users\Jimmy\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 263, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\Users\Jimmy\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\Users\Jimmy\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:\Users\Jimmy\Desktop\GovsM\Govs.py", line 438, in <module>
loadall()
File "c:\Users\Jimmy\Desktop\GovsM\Govs.py", line 436, in loadall
bot.load_extension(f'cogs.{filename[:-3]}')
File "C:\Users\Jimmy\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 653, in load_extension
self._load_from_module_spec(spec, name)
File "C:\Users\Jimmy\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 599, in _load_from_module_spec
raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.fun' raised an error: TypeError: Commands or listeners must not start with cog_ or bot_ (in method fun.cog_command_error)
如果能解决问题,我将不胜感激,我已经尝试了多种方法,而使它正常工作的唯一方法是处理on_command_error,这是我所不希望的。
答案 0 :(得分:1)
您需要删除装饰器。应该只是async def cog_command_error(self, ctx, error):
,不带修饰符。
这是一个小示例,如果您将字符串作为参数传递,则会向控制台显示“错误的参数错误”
@commands.command()
async def numError(self, ctx, test: int):
await ctx.send(test)
async def cog_command_error(self, ctx, error):
if isinstance(error, commands.BadArgument):
print("Bad Argument Error")