python-3.x - 我需要关于 cog_command_error 的帮助
问题描述
我是使用 cogs 的新手,刚刚发现了 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 我不想要。
解决方案
您需要删除装饰器。它应该async def cog_command_error(self, ctx, error):
没有装饰器。
这是一个小例子,如果你传入一个字符串作为参数,它会打印“Bad argument 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")
推荐阅读
- javascript - 点击后HTML将div移动到文本旁边
- azure - 在 Sharepoint 中显示 Dynamics CRM 365 客户
- python - 从列表理解到 numpy.where()
- vue.js - Vue路由器-命名路由错误的缺少参数
- ios - UIImagePickerController isSourceTypeAvailable(type) 始终为真
- javascript - 如何使用 dataTable 过滤日期列
- apache-kafka - 多个分区上的 Spring Kafka 和轮询策略
- java - 如何修复 Object 无法转换为 javafx.scene.Node
- graalvm - Micronaut 数据、GraalVM、AWS 构建失败
- c - M_CACHE、M_DEVBUF、M_TEMP的区别