Browse Source

Add permissions and CI (#3)

Adds permissions for slash commands, Drone, and vet checks

Reviewed-on: #3
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 6 months ago
parent
commit
6467681bdc
  1. 17
      .drone.yml
  2. 6
      Makefile
  3. 68
      slash/guild.go
  4. 120
      slash/structs.go

17
.drone.yml

@ -0,0 +1,17 @@
---
kind: pipeline
name: compliance
trigger:
event:
- pull_request
steps:
- name: test
pull: always
image: golang:1.16
commands:
- make test
- name: check
pull: always
image: golang:1.16
commands:
- make vet

6
Makefile

@ -6,4 +6,8 @@ fmt:
.PHONY: test
test:
$(GO) test -v -race ./...
$(GO) test -v -race ./...
.PHONY: vet
vet:
$(GO) vet ./...

68
slash/guild.go

@ -134,3 +134,71 @@ func (c *Client) DeleteGuildApplicationCommand(ctx context.Context, guildID, cmd
}
return nil
}
// GetApplicationCommandPermissions gets permissions for a slash command in a guild
func (c *Client) GetApplicationCommandPermissions(ctx context.Context, guildID, cmdID string) (*ApplicationCommandPermissions, error) {
endpoint := baseEndpoint + fmt.Sprintf("applications/%s/guilds/%s/commands/%s/permissions", c.clientID, guildID, cmdID)
req, err := c.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
resp, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("GetApplicationCommandPermissions: returned non-200 status code: %s\n%s", resp.Status, errMsg(resp.Body))
}
var perms *ApplicationCommandPermissions
return perms, json.NewDecoder(resp.Body).Decode(&perms)
}
// EditApplicationCommandPermissions edits permissions for a slash command in a guild
func (c *Client) EditApplicationCommandPermissions(ctx context.Context, guildID, cmdID string, perms []*ApplicationCommandPermissions) error {
endpoint := baseEndpoint + fmt.Sprintf("applications/%s/guilds/%s/commands/%s/permissions", c.clientID, guildID, cmdID)
wrap := struct {
Permissions []*ApplicationCommandPermissions `json:"permissions"`
}{
Permissions: perms,
}
buf, err := newBuffer(wrap)
if err != nil {
return err
}
req, err := c.newRequest(ctx, http.MethodPut, endpoint, &buf)
if err != nil {
return err
}
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("EditApplicationCommandPermissions: returned non-200 status code: %s\n%s", resp.Status, errMsg(resp.Body))
}
return nil
}
// BatchEditApplicationCommandPermissions batch edits permissions for a slash command in a guild
func (c *Client) BatchEditApplicationCommandPermissions(ctx context.Context, guildID string, perms []*GuildApplicationCommandPermissions) error {
endpoint := baseEndpoint + fmt.Sprintf("applications/%s/guilds/%s/commands/permissions", c.clientID, guildID)
buf, err := newBuffer(perms)
if err != nil {
return err
}
req, err := c.newRequest(ctx, http.MethodPut, endpoint, &buf)
if err != nil {
return err
}
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("BatchEditApplicationCommandPermissions: returned non-200 status code: %s\n%s", resp.Status, errMsg(resp.Body))
}
return nil
}

120
slash/structs.go

@ -6,9 +6,10 @@ import (
// CreateApplicationCommand https://discord.com/developers/docs/interactions/slash-commands#applicationcommand
type CreateApplicationCommand struct {
Name string `json:"name"`
Description string `json:"description"`
Options []*ApplicationCommandOption `json:"options,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Options []*ApplicationCommandOption `json:"options,omitempty"`
DefaultPermission bool `json:"default_permission"`
}
// ApplicationCommand https://discord.com/developers/docs/interactions/slash-commands#applicationcommand
@ -31,6 +32,7 @@ const (
UserACOT
ChannelACOT
RoleACOT
MentionableACOT
)
// ApplicationCommandOption https://discord.com/developers/docs/interactions/slash-commands#applicationcommandoption
@ -49,6 +51,30 @@ type ApplicationCommandOptionChoice struct {
Value interface{} `json:"value"`
}
// ApplicationCommandPermissionType (ACPT)
type ApplicationCommandPermissionType int
// https://discord.com/developers/docs/interactions/slash-commands#applicationcommandpermissiontype
const (
RoleACPT ApplicationCommandPermissionType = iota + 1
UserACPT
)
// ApplicationCommandPermissions https://discord.com/developers/docs/interactions/slash-commands#applicationcommandpermissions
type ApplicationCommandPermissions struct {
ID string `json:"id"`
Type ApplicationCommandPermissionType `json:"type"`
Permission bool `json:"permission"`
}
// GuildApplicationCommandPermissions https://discord.com/developers/docs/interactions/slash-commands#guildapplicationcommandpermissions
type GuildApplicationCommandPermissions struct {
ID string `json:"id"`
ApplicationID string `json:"application_id"`
GuildID string `json:"guild_id"`
Permissions []*ApplicationCommandPermissions `json:"permissions"`
}
// InteractionType (IT)
type InteractionType int
@ -58,17 +84,18 @@ const (
ApplicationCommandIT
)
// https://discord.com/developers/docs/interactions/slash-commands#interaction
// Interaction https://discord.com/developers/docs/interactions/slash-commands#interaction
type Interaction struct {
ID string `json:"id"`
Type InteractionType `json:"type"`
Data *ApplicationCommandInteractionData `json:"data"`
GuildID string `json:"guild_id"`
ChannelID string `json:"channel_id"`
Member *Member `json:"member"`
User *User `json:"user"`
Token string `json:"token"`
Version int `json:"version"`
ID string `json:"id"`
ApplicationID string `json:"application_id"`
Type InteractionType `json:"type"`
Data *ApplicationCommandInteractionData `json:"data"`
GuildID string `json:"guild_id"`
ChannelID string `json:"channel_id"`
Member *Member `json:"member"`
User *User `json:"user"`
Token string `json:"token"`
Version int `json:"version"`
}
// ApplicationCommandInteractionData https://discord.com/developers/docs/interactions/slash-commands#interaction-applicationcommandinteractiondata
@ -78,9 +105,18 @@ type ApplicationCommandInteractionData struct {
Options []*ApplicationCommandInteractionDataOption `json:"options"`
}
// ApplicationCommandInteractionDataResolved https://discord.com/developers/docs/interactions/slash-commands#interaction-applicationcommandinteractiondataresolved
type ApplicationCommandInteractionDataResolved struct {
Users map[string]*User `json:"users"`
Members map[string]*PartialMember `json:"members"`
Roles map[string]*Role `json:"roles"`
Channels map[string]*PartialChannel `json:"channels"`
}
// ApplicationCommandInteractionDataOption https://discord.com/developers/docs/interactions/slash-commands#interaction-applicationcommandinteractiondataoption
type ApplicationCommandInteractionDataOption struct {
Name string `json:"name"`
Type ApplicationCommandOptionType `json:"type"`
Value interface{} `json:"value"`
Options []*ApplicationCommandInteractionDataOption `json:"options"`
}
@ -111,13 +147,18 @@ func (o ApplicationCommandInteractionDataOption) ValueBool() bool {
// Member https://discord.com/developers/docs/resources/guild#guild-member-object
type Member struct {
User *User `json:"user"`
PartialMember
User *User `json:"user"`
Deaf bool `json:"deaf"`
Mute bool `json:"mute"`
}
// PartialMember https://discord.com/developers/docs/resources/guild#guild-member-object
type PartialMember struct {
Nick string `json:"nick"`
Roles []string `json:"roles"`
JoinedAt string `json:"joined_at"`
PremiumSince string `json:"premium_since"`
Deaf bool `json:"deaf"`
Mute bool `json:"mute"`
Pending bool `json:"pending"`
Permissions string `json:"permissions"`
}
@ -180,6 +221,53 @@ type User struct {
PublicFlags UserFlags `json:"public_flags"`
}
// Role https://discord.com/developers/docs/topics/permissions#role-object
type Role struct {
ID string `json:"id"`
Name string `json:"name"`
Color int `json:"color"`
Hoist bool `json:"hoist"`
Position int `json:"position"`
Permissions int `json:"permissions"`
Managed bool `json:"managed"`
Mentionable bool `json:"mentionable"`
Tags RoleTags `json:"tags"`
}
// RoleTags https://discord.com/developers/docs/topics/permissions#role-object-role-tags-structure
type RoleTags struct {
BotID string `json:"bot_id"`
IntegrationID string `json:"integration_id"`
PremiumSubscriber bool `json:"premium_subscriber"`
}
// PartialChannel https://discord.com/developers/docs/resources/channel#channel-object
type PartialChannel struct {
ID string `json:"id"`
Type PartialChannelType `json:"type"`
Name string `json:"name"`
}
// PartialChannelType (PCT)
type PartialChannelType int
const (
GuildTextPCT PartialChannelType = iota
DMPCT
GuildVoicePCT
GroupDMPCT
GuildCategoryPCT
GuildNewsPCT
GuildStorePCT
_
_
_
GuildNewsThreadPCT
GuildPublicThreadPCT
GuildPrivateThreadPCT
GuildStageVoicePCT
)
// InteractionResponseType (IRT)
type InteractionResponseType int

Loading…
Cancel
Save