Skip to content

Commit

Permalink
feat: command permissions (#212)
Browse files Browse the repository at this point in the history
* feat: command permissions

* Suggested changes
  • Loading branch information
thewilloftheshadow authored Jan 29, 2025
1 parent 4b8d474 commit 29c323c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-countries-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@buape/carbon": minor
---

feat: command permissions
11 changes: 11 additions & 0 deletions apps/cloudo/src/commands/testing/permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Command, type CommandInteraction, Permission } from "@buape/carbon"

export default class PermissionCommand extends Command {
name = "manage_server"
description = "You need the manage server permission to see this command"
permission = Permission.ManageGuild

async run(interaction: CommandInteraction): Promise<void> {
interaction.reply("You can see this command")
}
}
2 changes: 2 additions & 0 deletions apps/cloudo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import EverySelectCommand from "./commands/testing/every_select.js"
import MessageCommand from "./commands/testing/message_command.js"
import ModalCommand from "./commands/testing/modal.js"
import OptionsCommand from "./commands/testing/options.js"
import PermissionCommand from "./commands/testing/permissions.js"
import SubcommandsCommand from "./commands/testing/subcommand.js"
import SubcommandGroupsCommand from "./commands/testing/subcommandgroup.js"
import UserCommand from "./commands/testing/user_command.js"
Expand Down Expand Up @@ -54,6 +55,7 @@ const client = new Client(
new MessageCommand(),
new ModalCommand(),
new OptionsCommand(),
new PermissionCommand(),
new SubcommandsCommand(),
new SubcommandGroupsCommand(),
new UserCommand()
Expand Down
11 changes: 11 additions & 0 deletions apps/rocko/src/commands/testing/permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Command, type CommandInteraction, Permission } from "@buape/carbon"

export default class PermissionCommand extends Command {
name = "manage_server"
description = "You need the manage server permission to see this command"
permission = Permission.ManageGuild

async run(interaction: CommandInteraction): Promise<void> {
interaction.reply("You can see this command")
}
}
2 changes: 2 additions & 0 deletions apps/rocko/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import EverySelectCommand from "./commands/testing/every_select.js"
import MessageCommand from "./commands/testing/message_command.js"
import ModalCommand from "./commands/testing/modal.js"
import OptionsCommand from "./commands/testing/options.js"
import PermissionCommand from "./commands/testing/permissions.js"
import SubcommandsCommand from "./commands/testing/subcommand.js"
import SubcommandGroupsCommand from "./commands/testing/subcommandgroup.js"
import UserCommand from "./commands/testing/user_command.js"
Expand Down Expand Up @@ -58,6 +59,7 @@ const client = new Client(
new MessageCommand(),
new ModalCommand(),
new OptionsCommand(),
new PermissionCommand(),
new SubcommandsCommand(),
new SubcommandGroupsCommand(),
new UserCommand(),
Expand Down
24 changes: 21 additions & 3 deletions packages/carbon/src/abstracts/BaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
} from "discord-api-types/v10"
import {
ApplicationIntegrationType,
type ArrayOrSingle,
type BaseComponent,
InteractionContextType,
type Modal
type Modal,
type Permission
} from "../index.js"

/**
Expand Down Expand Up @@ -49,6 +51,12 @@ export abstract class BaseCommand {
InteractionContextType.PrivateChannel
]

/**
* The default permission that a user needs to have to use this command.
* This can be overridden by server admins.
*/
permission?: ArrayOrSingle<(typeof Permission)[keyof typeof Permission]>

/**
* The components that the command is able to use.
* You pass these here so the handler can listen for them..
Expand Down Expand Up @@ -77,7 +85,12 @@ export abstract class BaseCommand {
description: this.description,
options: this.serializeOptions(),
integration_types: this.integrationTypes,
contexts: this.contexts
contexts: this.contexts,
default_member_permissions: Array.isArray(this.permission)
? this.permission.reduce((a, p) => a | p, 0n).toString()
: this.permission
? `${this.permission}`
: null
}

return data
Expand All @@ -87,7 +100,12 @@ export abstract class BaseCommand {
type: this.type,
options: this.serializeOptions(),
integration_types: this.integrationTypes,
contexts: this.contexts
contexts: this.contexts,
default_member_permissions: Array.isArray(this.permission)
? this.permission.reduce((a, p) => a | p, 0n).toString()
: this.permission
? `${this.permission}`
: null
}

return data
Expand Down
4 changes: 4 additions & 0 deletions packages/carbon/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ export * from "./structures/User.js"
export * from "discord-api-types/v10"
export * from "./types.js"
export * from "./utils.js"

// ----- Re-exports -----
import { PermissionFlagsBits } from "discord-api-types/v10"
export const Permission = PermissionFlagsBits
2 changes: 2 additions & 0 deletions packages/carbon/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ export type MessagePayload =
tts?: boolean
}
| string

export type ArrayOrSingle<T> = T | T[]

0 comments on commit 29c323c

Please sign in to comment.