Search code examples
c#discorddsharp+

How do i disable a D#+ Command To not show for users who dont have certain permissions?


how do i disable a D#+ Command to not show for users who don't have specific permissions such as Administrator, Or Owner?

a reply would be appreciated, thanks.


Solution

  • The easiest way to hide a command from someone who should not have access to it is with attribution.

    [SlashCommand("hello", "hello world")]
    [RequirePermissions(DSharpPlus.Permissions.Administrator)] //Must have admin perm to see/use
    public static async Task HelloWorld(InteractionContext ctx)
    {
        await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
          new DiscordInteractionResponseBuilder().WithContent("Hello, World!"));
    }
    

    You can change the attribution to any of the permissions found in DSharpPlus.Permissions

    You can also use roles for this as well:

    [SlashCommand("hello", "hello world")]
    [RequireRoles(RoleCheckMode.Any, new ulong[] { 99999 })] // must have role with ID 99999
    public static async Task HelloWorld(InteractionContext ctx)
    {
        await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
          new DiscordInteractionResponseBuilder().WithContent("Hello, World!"));
    }
    

    RoleCheckMode can be used to define parameters for when you have multiple roles involved, and you can find more about this attribution here