Is there a way to hide a slash command from everyone except some users - discord.py?

Viewed 250

I have created some commands which can only be used by specific users. I do not want those commands to show in the drop-down list of the slash commands for everyone.

How to hide those commands?

I am using discord.py v2, hybrid commands

import discord
from discord import app_commands
from discord.ext import commands
from utils.constants import owner_list

discord.http._set_api_version(9)

class owner_commands(commands.Cog):

    def __init__(self, bot):
        self.bot = bot

    @commands.hybrid_command(name='sc', with_app_command=True)
    async def sc(self, ctx: commands.Context):

        # SENDS A MESSAGE TO THE CHANNEL USING THE CONTEXT OBJECT.
        if(str(ctx.message.author.id) in owner_list):
            embed = discord.Embed(title=str(len(self.bot.guilds))+" servers!", color=0xCC0066)
            
            await ctx.send(embed=embed, ephemeral=True)

        else:
            await ctx.send("Command is not available")


async def setup(bot: commands.Bot) -> None:
    await bot.add_cog(owner_commands(bot))
    await bot.tree.sync()
2 Answers

Yes it is possible, but you have to do this on discord directly as the server owner.

go to server settings -> integrations -> click on your bot (manage)

Then in the "commands" tab select your command. There you can add roles, members or channels and set the permissions for them. Only users who have the permission to use the slash command will see it in the list of slash commands.

Related