user13405446
user13405446

Reputation: 23

How i can get a costum Error handling in Cog File? Python Discordbot

I actual try to handle Errors in a Cog file and to understand how the @commands works in a cog file and which @ is needed for which event

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))

(Added it into a Snippet cause everything else endet in a weird formatting)

So and i dont get how i now can react in the Cog File to an own Command Error? Here for the clear Event.

    @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!!')    

This is the Error actul

AttributeError: module 'discord.ext.commands' has no attribute 'clear'

Upvotes: 0

Views: 2105

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60974

Each Command object has an error attribute that acts as a decorator:

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

Upvotes: 2

Related Questions