#invoke command on error
1 messages ยท Page 1 of 1 (latest)
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'
code ๐
it was here but i gonna update it since i fixed some things idk how
@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
so first off you're passing ctx twice
i need to do that its for prefix
we seen that with nelo yesterday
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
if i dont add the double i got that :
discord.ext.commands.errors.MemberNotFound: Member "test" not found.
can i see your command definition
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
async def add(
self,
ctx: LumabotContext,
member: discord.Member,
role: discord.Role,
*,
reason: str = None,
):
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
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
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
if you have a better alternative or idea im open
because i dont really have any other idea in mind
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
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
I'm not entirely sure how you have it set up
but finding a way to only apply it to prefix
my command is basically a bridge command
little bit different cos i create a multicog for it too
yeah idk
i will look at custom converter and try to find another way
there's a way, you'd just have to screw around with it
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
but if you apply that it would change the slash param type to string so it can use the converter
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
that would work, but you wouldn't get discord listing the roles for you
because the option type has changed from role to string
since it's getting handled by the converter instead of discord
also I would try the role converter first and use get_role as a fallback
like so ?
yes true
you'd need to try/except the role converter
and catch rolenotfound
it doesn't return None
True thanks
yeah issue is i dont have the whole thing by discord
maybe i cant do that using a converter
you'd just have to find a way to set the option type to role on the app command internally