Herue
Herue

Reputation: 1

How to check error on load cog with discord rewrite.py

I am trying to do a robust error checking with discord rewrite.py for my cog loading function.

Here is my code :

from discord.ext import commands
import discord

@bot.command()
@commands.is_owner()
async def load(ctx, extension):
   bot.load_extension(f"module.{extension}")


@load.error
async def load_error(ctx, error):
   if isinstance(error, commands.NotOwner):
        await ctx.channel.send("You must be the owner to use this command.")
        print(error)
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.channel.send("You must tell me which extension to load")
        print(error)
    if isinstance(error, commands.ExtensionAlreadyLoaded):
        await ctx.channel.send("Extension Already loaded")
        print(error)
    else:
        await ctx.channel.send("An error as occured, please contact the bot owner")
        print(error)

bot.run("NeverWriteYourTokenOnInternet")

I have some wierd interaction with my error handler, the not owner work flawlessly so i guess i have the right syntax.

The missing required argument doesn't work quit well, although it is triggering this error, it also trigger the else statement. I have two error messages (which is bothering) my best guess is that the function raise two exceptions but i couldn't find anything on the API references.

The third one is even wierder, i checked the API references multiple times and the load_extension raise the ExtensionAlreadyLoaded exception. But it only trigger the global error, the printed error is the following. Command raised an exception: ExtensionAlreadyLoaded: Extension 'module.tts' is already loaded.

I don't need to say i have the same problem with the unloading function.

If anyone know what is happening, it would be a lof helpful.

Upvotes: 0

Views: 551

Answers (3)

Herue
Herue

Reputation: 1

Thank your for you help, it work flawlessly now.

If you are trying to do what i did here is how.

@bot.command()
@commands.is_owner()
async def load(ctx, extension):
   try:
      bot.load_extension(f"module.{extension}")
   except commands.ExtensionAlreadyLoaded:
      await ctx.channel.send("This extension is already loaded")
   except commands.ExtensionNotFound:
      await ctx.channel.send("I could not find the extension you tried to load")
   except commands.ExtensionFailed:
      await ctx.channel.send("I failed to load the extension !")
   except Exception as e:
      await ctx.send("An unexpected error as occured")
      print(e)

@load.error
async def load_error(ctx, error):
   if isinstance(error, commands.NotOwner):
      await ctx.channel.send("You must be the owner to use this command")
   elif isinstance(error, commands.MissingRequiredArgument):
      await ctx.channel.send("You must tell me which extension to load")
   else:
      await ctx.channel.send("An error as occured, please contact the bot owner")
      print(error)

It should handle most error on a extension load. The first exception cannot be part of the load_error function since they don't inherit from commands.CommandError, they have to be handled appart.

Upvotes: 0

Ali Hakan Kurt
Ali Hakan Kurt

Reputation: 1037

@bot.command()
@commands.is_owner()
async def load(ctx, extension):
    try:
        bot.load_extension(f"module.{extension}")
        await ctx.send(f"{extension} loaded successfully".)
    except Exception as e:
        await ctx.send(str(e))

This will send error message if any error has occured like syntax, unexpected indent, extension already loaded and other errors.

Upvotes: 0

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15689

commands.ExtensionAlreadyLoaded doesn't inherit from commands.CommandError, that means that you do not handle it in the error handler, simply with a try-except block

@bot.command()
@commands.is_owner()
async def load(ctx, extension):
    try:
       bot.load_extension(f"module.{extension}")
    except commands.ExtensionAlreadyLoaded:
        await ctx.channel.send("Extension Already loaded")

Upvotes: 1

Related Questions