#before_slash_command_invoke

1 messages · Page 1 of 1 (latest)

toxic pivot
#

I need a little help with how to use this method, a pseudocode example would likely be enough. This is what I have, it's in the cog I have all of my listeners in:

@commands.Cog.listener()
async def before_slash_command_invoke(
    self,
    inter
):
    blacklist = self.cur.execute('SELECT * FROM blacklist WHERE (userID=? AND guildID=?)', (inter.author.id, inter.guild_id)).fetchall()
    commands = [inter.data.name]
    if hasattr(inter.data, 'options'):
        if inter.data.options != []:
            commands.append(inter.data.name + ' ' + inter.data.options[0].name)
            if hasattr(inter.data.options[0], 'options'):
                if inter.data.options[0].options != []:
                    commands.append(inter.data.name + ' ' + inter.data.options[0].name + ' ' + inter.data.options[0].options[0].name)
    for item in blacklist:
        for i in commands:
            if i == item[2]:
                await inter.send(embed=disnake.Embed(color=cmn.colors.red, description='You have been blacklisted from this command.'), ephemeral=True)
                return
    return await super().cog_before_invoke(inter)

I know this is probably not using it correctly, I thought it would be this simple, but it seems to not be, because this doesn't trigger when I run a command.

toxic pivot
jovial haven
#

What is the behavior you want?

toxic pivot
#

Correct me if I'm wrong, but a check would just prevent the command from being an option for the person at all right? Or would it let them attempt and throw CheckFailure?

#

Okay so it does allow the attempt and it does throw an error, so I could describe the response in the on_error and have the check do the behavior

#

I'll try it and see if it gives me more questions

latent ledge
#

yea thats exactly what you want

toxic pivot
#

If my check needs the interaction data, I can't just do:

@commands.slashcommand()
@blacklist()```
#

meh

#

Is there a way to get the interaction data inside of a check?

latent ledge
#

what interaction data

#

the interaction?

toxic pivot
#

The name of the command being ran

#

The parent and any sub command group or sub commands

latent ledge
#

if its for a specific command, you can define a check on that command

#

or use inter.application_command

toxic pivot
#

I have a means to add someone to a blacklist for any command, and I want to check on every command run if that person is blacklisted from that command

#

That's why I was using before_slash_command_invoke before but I don't think it'll work with a check now because it wants me to pass the interaction to it to be able to see the command names

#

I really am so close to just putting the same 17 lines of code in the beginning of every single one of my commands

#

Or making a check for every single command lmao god that sounds tedious and dumb

#

I was hoping there was a nice, more correct way of doing this

#

Tl;dr:

  • if I use a check, I need a way to get the command and sub command names inside the check
  • if I use before_slash_command_invoke, I need to know how to use it, and I need to be able to return out of the triggering command
latent ledge
#

yea, you can get the command name in the check

#

inter.application_command

toxic pivot
#

ooh, I'll try it

#

wait

#

How do I get inter in the check

#

If I don't do anything it says inter isn't defined

#
def blacklist():
    async def the_check(
        self,
        inter
    ):
        blacklist = self.cur.execute('SELECT * FROM blacklist WHERE (userID=? AND guildID=?)', (inter.author.id, inter.guild_id)).fetchall()
        commands = [inter.data.name]
        if hasattr(inter.data, 'options'):
            if inter.data.options != []:
                commands.append(inter.data.name + ' ' + inter.data.options[0].name)
                if hasattr(inter.data.options[0], 'options'):
                    if inter.data.options[0].options != []:
                        commands.append(inter.data.name + ' ' + inter.data.options[0].name + ' ' + inter.data.options[0].options[0].name)
        for item in blacklist:
            for i in commands:
                if i == item[2]:
                    return False
        return True
    return commands.check(the_check)```

```py
@commands.slash_command()
@blacklist()
async def test(
    self,
    inter
):
    await inter.send('Success.')```

`disnake.ext.commands.errors.ExtensionFailed: Extension 'cogs.moderation' raised an error: TypeError: InvokableApplicationCommand.__call__() missing 1 required positional argument: 'interaction'`
#

Does this only work for context then? I'm so confused

#

Whenever I try to do new stuff with this API it always makes me feel like an idiot lmao

#

That's more on me than the API though

latent ledge
#

I usually just do

#
@client.slash_command_check
async def all_slash_command_check(inter: ApplicationCommandInteraction):
    ...
toxic pivot
lime tulip
lofty spokeBOT
#

@before_slash_command_invoke```
Similar to [`Bot.before_invoke()`](https://docs.disnake.dev/en/stable/ext/commands/api.html#disnake.ext.commands.Bot.before_invoke "disnake.ext.commands.Bot.before_invoke") but for slash commands, and it takes an [`ApplicationCommandInteraction`](https://docs.disnake.dev/en/stable/api.html#disnake.ApplicationCommandInteraction "disnake.ApplicationCommandInteraction") as its only parameter.