我实际尝试处理Cog文件中的错误,并理解@命令在cog文件中是如何工作的,以及哪个事件需要哪个@
import discord
from discord.ext import commands
class Errors(commands.Cog):
def __init__(self, bot):
self.bot = bot
# Events
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('PASS PLS ALL ARGS')
print('THERE IS A ERROR!!')
# Commands
@commands.command()
async def ping2(self, ctx):
await ctx.send('Pong!')
@commands.clear.error()
async def clear_error(self, ctx, error):
await ctx.send('Pong!')
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Costum error message for clear event')
print('THERE IS A ERROR!!')
.
def setup(bot):
bot.add_cog(Errors(bot))
(将其添加到代码片段中,导致其他所有内容都以奇怪的格式显示)
所以,我不明白我现在如何在Cog文件中对自己的命令错误做出反应?这里是clear事件。
@commands.clear.error()
async def clear_error(self, ctx, error):
await ctx.send('Pong!')
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Costum error message for clear event')
print('THERE IS A ERROR!!')
这就是错误发生
AttributeError: module 'discord.ext.commands' has no attribute 'clear'
发布于 2020-08-30 02:41:04
每个Command
对象都有一个充当装饰器的error
属性:
class MyCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def echo(self, ctx, arg):
await ctx.send(arg)
@echo.error
async def echo_error(self, ctx, error):
await ctx.send("There was an error")
if isinstance(error, command.MissingRequiredArgument):
await ctx.send("MIssing Required Argument")
else:
raise error
https://stackoverflow.com/questions/63648797
复制相似问题