Skip to content

Commit

Permalink
Add Default Permission to #943 (#1071)
Browse files Browse the repository at this point in the history
* Create edit application command permissions endpoint

* Add remaining command permissions endpoints

* Clean up naming a bit

* Add docs for application command permissions endpoints

* Update comments

* More comments

* :^)

* Put the verb in API method names at the end

* Review feedback

* Add default permissions

* feat(restapi): rewording of comments to application command permissions endpoints

* fix(rest): errors for application commands permissions endpoints

* style(interactions): changed order of fields in ApplicationCommand

Co-authored-by: NotUnlikeTheWaves <hleistra@gmail.com>
Co-authored-by: nitroflap <fe.lap.prog@gmail.com>
  • Loading branch information
3 people committed Feb 16, 2022
1 parent 0a0955c commit d5bacb5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 8 deletions.
6 changes: 6 additions & 0 deletions endpoints.go
Expand Up @@ -133,6 +133,12 @@ var (
EndpointApplicationGuildCommand = func(aID, gID, cID string) string {
return EndpointApplicationGuildCommands(aID, gID) + "/" + cID
}
EndpointApplicationCommandPermissions = func(aID, gID, cID string) string {
return EndpointApplicationGuildCommand(aID, gID, cID) + "/permissions"
}
EndpointApplicationCommandsGuildPermissions = func(aID, gID string) string {
return EndpointApplicationGuildCommands(aID, gID) + "/permissions"
}
EndpointInteraction = func(aID, iToken string) string {
return EndpointAPI + "interactions/" + aID + "/" + iToken
}
Expand Down
47 changes: 39 additions & 8 deletions interactions.go
Expand Up @@ -30,15 +30,17 @@ const (

// ApplicationCommand represents an application's slash command.
type ApplicationCommand struct {
ID string `json:"id,omitempty"`
ApplicationID string `json:"application_id,omitempty"`
Type ApplicationCommandType `json:"type,omitempty"`
Name string `json:"name"`
// NOTE: Chat commands only. Otherwise it mustn't be set.
Description string `json:"description,omitempty"`
Version string `json:"version,omitempty"`
ID string `json:"id,omitempty"`
ApplicationID string `json:"application_id,omitempty"`
Version string `json:"version,omitempty"`
Type ApplicationCommandType `json:"type,omitempty"`
Name string `json:"name"`
DefaultPermission *bool `json:"default_permission,omitempty"`

// NOTE: Chat commands only. Otherwise it mustn't be set.
Options []*ApplicationCommandOption `json:"options"`

Description string `json:"description,omitempty"`
Options []*ApplicationCommandOption `json:"options"`
}

// ApplicationCommandOptionType indicates the type of a slash command's option.
Expand Down Expand Up @@ -107,6 +109,35 @@ type ApplicationCommandOptionChoice struct {
Value interface{} `json:"value"`
}

// ApplicationCommandPermissions represents a single user or role permission for a command.
type ApplicationCommandPermissions struct {
ID string `json:"id"`
Type ApplicationCommandPermissionType `json:"type"`
Permission bool `json:"permission"`
}

// ApplicationCommandPermissionsList represents a list of ApplicationCommandPermissions, needed for serializing to JSON.
type ApplicationCommandPermissionsList struct {
Permissions []*ApplicationCommandPermissions `json:"permissions"`
}

// GuildApplicationCommandPermissions represents all permissions for a single guild command.
type GuildApplicationCommandPermissions struct {
ID string `json:"id"`
ApplicationID string `json:"application_id"`
GuildID string `json:"guild_id"`
Permissions []*ApplicationCommandPermissions `json:"permissions"`
}

// ApplicationCommandPermissionType indicates whether a permission is user or role based.
type ApplicationCommandPermissionType uint8

// Application command permission types.
const (
ApplicationCommandPermissionTypeRole ApplicationCommandPermissionType = 1
ApplicationCommandPermissionTypeUser ApplicationCommandPermissionType = 2
)

// InteractionType indicates the type of an interaction event.
type InteractionType uint8

Expand Down
56 changes: 56 additions & 0 deletions restapi.go
Expand Up @@ -2282,6 +2282,62 @@ func (s *Session) ApplicationCommands(appID, guildID string) (cmd []*Application
return
}

// GuildApplicationCommandsPermissions returns permissions for all application commands in a guild.
// appID : The application ID
// guildID : Guild ID to retrieve application commands permissions for.
func (s *Session) GuildApplicationCommandsPermissions(appID, guildID string) (permissions []*GuildApplicationCommandPermissions, err error) {
endpoint := EndpointApplicationCommandsGuildPermissions(appID, guildID)

var body []byte
body, err = s.RequestWithBucketID("GET", endpoint, nil, endpoint)
if err != nil {
return
}

err = unmarshal(body, &permissions)
return
}

// ApplicationCommandPermissions returns all permissions of an application command
// appID : The Application ID
// guildID : The guild ID containing the application command
// cmdID : The command ID to retrieve the permissions of
func (s *Session) ApplicationCommandPermissions(appID, guildID, cmdID string) (permissions *GuildApplicationCommandPermissions, err error) {
endpoint := EndpointApplicationCommandPermissions(appID, guildID, cmdID)

var body []byte
body, err = s.RequestWithBucketID("GET", endpoint, nil, endpoint)
if err != nil {
return
}

err = unmarshal(body, &permissions)
return
}

// ApplicationCommandPermissionsEdit edits the permissions of an application command
// appID : The Application ID
// guildID : The guild ID containing the application command
// cmdID : The command ID to edit the permissions of
// permissions : An object containing a list of permissions for the application command
func (s *Session) ApplicationCommandPermissionsEdit(appID, guildID, cmdID string, permissions *ApplicationCommandPermissionsList) (err error) {
endpoint := EndpointApplicationCommandPermissions(appID, guildID, cmdID)

_, err = s.RequestWithBucketID("PUT", endpoint, permissions, endpoint)
return
}

// ApplicationCommandPermissionsBatchEdit edits the permissions of a batch of commands
// appID : The Application ID
// guildID : The guild ID to batch edit commands of
// permissions : A list of permissions paired with a command ID, guild ID, and application ID per application command
func (s *Session) ApplicationCommandPermissionsBatchEdit(appID, guildID string, permissions []*GuildApplicationCommandPermissions) (err error) {
endpoint := EndpointApplicationCommandsGuildPermissions(appID, guildID)

_, err = s.RequestWithBucketID("PUT", endpoint, permissions, endpoint)
return
}

// InteractionRespond creates the response to an interaction.
// appID : The application ID.
// interaction : Interaction instance.
Expand Down

0 comments on commit d5bacb5

Please sign in to comment.