#invoke command on error

1 messages ยท Page 1 of 1 (latest)

meager oracle
#

here my current issue:

2024-08-15 10:42:11 [LOG] : +role add @tranquil lodge 1262863546182668504 test
Ignoring exception in on_hybrid_command_error
Traceback (most recent call last):
  File "/home/container/.local/lib/python3.11/site-packages/discord/client.py", line 409, in _run_event
    await coro(*args, **kwargs)
  File "/home/container/Events/commands.py", line 67, in on_hybrid_command_error
    return await ctx.command(ctx, ctx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/core.py", line 503, in __call__
    return await self.callback(self.cog, context, *args, **kwargs)  # type: ignore
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: RoleAdd.add() missing 1 required positional argument: 'role'
terse saddle
#

code ๐Ÿ‘

meager oracle
#
    @commands.Cog.listener()
    async def on_hybrid_command_error(
        self, ctx: LumabotContext, error: discord.DiscordException
    ):
        if isinstance(error, commands.errors.RoleNotFound):
            if not ctx.is_app:
                role_name = error.argument
                if isinstance(role_name, str):
                    roles = await get_role(self.bot, ctx.guild, role_name=role_name)
                    role = roles[0] if roles else None
                    new_content = ctx.message.content.replace(
                        role_name, str(role.id), 1
                    )
                    ctx.message.content = new_content
                    if role:
                        return await ctx.command(ctx, ctx)

#

here it is, my assumption is the ctx that is used to invoke the ctx.command is not the ctx that i pass

terse saddle
#

so first off you're passing ctx twice

meager oracle
#

we seen that with nelo yesterday

terse saddle
#

you only need it once though

#

this is a function call

#

you're directly calling the command

#

so you just pass params like you're calling your command as a function

meager oracle
#

if i dont add the double i got that :
discord.ext.commands.errors.MemberNotFound: Member "test" not found.

terse saddle
#

can i see your command definition

meager oracle
#

you might want to check this part, nelo was saying that prefix are wrapped differently so they need double ctx even if one is always ignore #1132206148309749830 message

meager oracle
terse saddle
#

so that's all you need is those parameters

#

pass a context, member object, a role object, and optional reason

#

just call it like it's a normal function

meager oracle
#

the thing is im trying to handle that globally, for all command so how can i do. My thing was to replace the content to then invoke the command for it to parse all arg since i cant just append the role arg

terse saddle
#

if you want to do that then you'll have to pass it back to process_commands

#

if you're changing the message content

#

i discourage modifying the message object though since it can lead to some undesired results

meager oracle
#

if you have a better alternative or idea im open

#

because i dont really have any other idea in mind

terse saddle
#

you might be better off changing the parameter type to also accept strings

#

and then processing that inside the command

#

and raising RoleNotFound yourself if you still can't do it

#

or again, write a custom converter

meager oracle
#

is that possible to me to make a custom converter that will for slash command accept only discord.Role and for prefix command str ?

#

also thanks for the process_commands idea it works perfectly

terse saddle
#

but finding a way to only apply it to prefix

meager oracle
#

my command is basically a bridge command

#

little bit different cos i create a multicog for it too

terse saddle
#

yeah idk

meager oracle
#

i will look at custom converter and try to find another way

terse saddle
#

there's a way, you'd just have to screw around with it

meager oracle
#

yep i found it

#

because convert take a ctx arg

#

so i can do if ctx.is_app convert it has role else as str

terse saddle
#

but if you apply that it would change the slash param type to string so it can use the converter

meager oracle
#

i was thinking at something like so :

class RoleConverter:
    def __init__(self, role):
        self.role = role

    @classmethod
    async def convert(cls, ctx: LumabotContext, argument):
          role = await commands.RoleConverter().convert(ctx, argument)
          if not role and not ctx.is_app:
            roles = await get_role(ctx.bot, ctx.guild, role_name=argument)
            role = roles[0] if roles else None

          if not role:
              raise commands.RoleNotFound(argument)

          return cls(role)
#

cant test rn because i gotta go but you have the idea

terse saddle
#

that would work, but you wouldn't get discord listing the roles for you

meager oracle
#

ooooo

#

thats an issue

terse saddle
#

since it's getting handled by the converter instead of discord

terse saddle
terse saddle
#

and catch rolenotfound

#

it doesn't return None

meager oracle
#

True thanks

#

yeah issue is i dont have the whole thing by discord

#

maybe i cant do that using a converter

terse saddle
#

you'd just have to find a way to set the option type to role on the app command internally