#Have a decorator pass an additional parameter to my function

1 messages · Page 1 of 1 (latest)

short lake
#

How could I have a decorator on my command that runs before the command (like checks) but it also passes an additional parameter to my command function? Is this even possible with pycord's current implementation?

Example:


def load_guild_or_raise(func: Callable[[Any, ...], Coroutine]) -> Callable[[Any, ...], Coroutine]:
    async def wrapper(self, ctx: discord.ApplicationContext, *args, **kwargs):
        guild: GuildAdapter = GuildAdapter(ctx.guild.id)
        await guild.load()

        if not guild.exists:
            raise NotFoundError
        await func(self, ctx, *args, guild=guild, **kwargs)

    return wrapper

# this is in a cog
@commands.slash_command(name="ping")
@discord.option(name="enable",
        description="Enable or disable the ping feature",
        type=bool,
)
@load_guild_or_raise
async def(self, ctx, enable: bool, guild: GuildAdapter):
  await guild.disable_ping()

this dosen't work, it adds args and kwargs as options to my slash command

dense relic
#

well i was not really meaning like that but

#

i will try to do that from my side

short lake
#

Have a decorator pass an additional parameter to my function

#

ping me with replies

dense relic
#

you can jjust do

#
@commands.slash_command(name="ping")
@discord.option(name="enable",
        description="Enable or disable the ping feature",
        type=bool,
)
@load_guild_or_raise
async def(self, ctx, enable: bool):
  guild: GuildAdapter = GuildAdapter(ctx.guild.id)
  if guild is None: 
    error not found
  await guild.disable_ping()
#

i dont think you can uysing decorator and pass it like so inside rthe func

short lake
#

Yeah but that defeats the entire purpose.

#

I could subclass ctx

dense relic
#

what's ur purpose ?

#

yeah

#

that s the thing that i would do

short lake
#

to load the guild, but also to pass it trough different decorators and not load it every time