Search code examples
discorddiscord.pybotsnextcord

Nextcord @commands.check() not working. Premium slash command


I am trying to add a check to my slash commands to see if the user.id who is running the command matches the user.id that is in my .json list. I am able to add and remove users from this list with a commmand down below however, when I go to add the @commands.check(check_premium) to the beginning of my code the user is still able to run the code!

Here is a question that somebody else raised that I am referring my code to How to add premium command in discord bot

Here is the check function that I created that grabs the user.id from the .json file (This is also what I am struggling to get to work)

def check_premium(interaction: Interaction):
    with open("premium_users.json") as f:
        premium_users_list = json.load(f)
        if interaction.user.id not in premium_users_list:
            return False

    return True

For example here is a command that that I would like to apply the check to, however when I add it to the header of the command it still lets me run the command even if my user.id is not in the list.

@bot.slash_command(name="test",description="Just a Test Command")
@commands.check(check_premium) # <-- THIS IS WHATS NOT WORKING
async def test(ctx):
    embed = nextcord.Embed(title="Test Command",
                          description="(random words)",
                          color=nextcord.Color.red(),
                          timestamp=datetime.datetime.now())
    embed.add_field(name="This is a test Command",
                    value="*It does not matter what I put here*",
                    inline=False)

    await ctx.send(embed=embed)     

enter image description here

If anyone has any insight as to what I might be doing wrong I'd appreciate any help I could get. Thanks!


Solution

  • @commands.check() is for discord.py, on nextcord you need to use @application_checks.check(). For this you need to do the following import: from nextcord.ext import application_checks.

    Example:

    from nextcord.ext import application_checks
    
    @bot.slash_command(name="test",description="Just a Test Command")
    @application_checks.check(check_premium) # <-- Replcaced commands.check by application_checks.check 
    async def test(ctx):
        embed = nextcord.Embed(title="Test Command",
                              description="(random words)",
                              color=nextcord.Color.red(),
                              timestamp=datetime.datetime.now())
        embed.add_field(name="This is a test Command",
                        value="*It does not matter what I put here*",
                        inline=False)
    
        await ctx.send(embed=embed)
    

    An other option to do this, that is better in my opinion, is to set the commands permission directly in your discord server, to do this go in Server Settings>Integration

    Access Integration Settings

    Then select your bot by clicking manage next to him.

    Select your bot

    And once you're in there you'll be able to set permission for the commands of your choice like you'd do for a channel.

    ⚠️ Warning: This will only work with slash commands

    This is the best method because when you do this users that doesn't have access the command will not even be able to see it in commands list by doing "/" while with the other method they'll see it and it'll return an error when they use the commands.