首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Cog文件中获得客户错误处理?Python Discordbot

如何在Cog文件中获得客户错误处理?Python Discordbot
EN

Stack Overflow用户
提问于 2020-08-29 23:38:26
回答 1查看 897关注 0票数 0

我实际尝试处理Cog文件中的错误,并理解@命令在cog文件中是如何工作的,以及哪个事件需要哪个@

代码语言:javascript
运行
复制
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事件。

代码语言:javascript
运行
复制
    @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!!')    

这就是错误发生

代码语言:javascript
运行
复制
AttributeError: module 'discord.ext.commands' has no attribute 'clear'
EN

回答 1

Stack Overflow用户

发布于 2020-08-30 02:41:04

每个Command对象都有一个充当装饰器的error属性:

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63648797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档