Skip to content

Commit

Permalink
Add some comments (#2)
Browse files Browse the repository at this point in the history
* Add some comments
* Keep -f variants

Signed-off-by: jolheiser <[email protected]>
  • Loading branch information
jolheiser authored Jan 1, 2022
1 parent 97efaba commit 6c1bf2f
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 20 deletions.
2 changes: 1 addition & 1 deletion _example/bongo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func bongoHandler(w corde.ResponseWriter, i *corde.Interaction) {
}
defer resp.Body.Close()
w.Respond(corde.NewResp().
Attachements(corde.Attachment{
Attachments(corde.Attachment{
Body: resp.Body,
ID: corde.Snowflake(0),
Filename: "bongo.gif",
Expand Down
2 changes: 1 addition & 1 deletion _example/todo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var commands = []corde.Command{
corde.Command{
{
Name: "todo",
Description: "view edit and remove todos",
Type: corde.COMMAND_CHAT_INPUT,
Expand Down
1 change: 1 addition & 0 deletions ack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
)

// Validate is a middleware to validate Interaction payloads
func Validate(publicKey string) func(http.Handler) http.Handler {
pk, err := hex.DecodeString(publicKey)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
COMMAND_MESSAGE
)

// Command is a Discord application command
type Command struct {
Name string `json:"name,omitempty"`
ID Snowflake `json:"id,omitempty"`
Expand All @@ -39,6 +40,7 @@ type Command struct {
Version Snowflake `json:"version,omitempty"`
}

// Option is an option for an application Command
type Option struct {
Name string `json:"name"`
Type OptionType `json:"type"`
Expand All @@ -50,21 +52,25 @@ type Option struct {
Focused bool `json:"focused,omitempty"`
}

// Choice is an application Command choice
type Choice[T any] struct {
Name string `json:"name"`
Value T `json:"value"`
}

// CommandsOpt is an option for a Command
type CommandsOpt struct {
guildID Snowflake
}

// GuildOpt is an option for setting the guild of a Command
func GuildOpt(guildID Snowflake) func(*CommandsOpt) {
return func(opt *CommandsOpt) {
opt.guildID = guildID
}
}

// GetCommands returns a slice of Command from the Mux
func (m *Mux) GetCommands(options ...func(*CommandsOpt)) ([]Command, error) {
opt := &CommandsOpt{}
for _, option := range options {
Expand All @@ -86,6 +92,7 @@ func (m *Mux) GetCommands(options ...func(*CommandsOpt)) ([]Command, error) {
return commands, nil
}

// RegisterCommand registers a new Command to the Mux
func (m *Mux) RegisterCommand(c Command, options ...func(*CommandsOpt)) error {
opt := &CommandsOpt{}
for _, option := range options {
Expand All @@ -105,6 +112,7 @@ func (m *Mux) RegisterCommand(c Command, options ...func(*CommandsOpt)) error {
return rest.CodeBetween(resp, 200, 299)
}

// BulkRegisterCommand registers a slice of Command to the Mux
func (m *Mux) BulkRegisterCommand(c []Command, options ...func(*CommandsOpt)) error {
opt := &CommandsOpt{}
for _, option := range options {
Expand All @@ -124,6 +132,7 @@ func (m *Mux) BulkRegisterCommand(c []Command, options ...func(*CommandsOpt)) er
return rest.CodeBetween(resp, 200, 299)
}

// DeleteCommand deletes a Command from the Mux
func (m *Mux) DeleteCommand(ID Snowflake, options ...func(*CommandsOpt)) error {
opt := &CommandsOpt{}
for _, option := range options {
Expand Down
13 changes: 8 additions & 5 deletions components.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package corde

// ButtonStyle is the style of a button Component
type ButtonStyle int

const (
BUTTON_PRIMARY ButtonStyle = iota + 1
BUTTON_SECONDARY
BUTTON_SUCCESS
BUTTON_DANGER
BUTTON_LINK
BUTTON_PRIMARY ButtonStyle = iota + 1 // BUTTON_PRIMARY blurple
BUTTON_SECONDARY // BUTTON_SECONDARY grey
BUTTON_SUCCESS // BUTTON_SUCCESS green
BUTTON_DANGER // BUTTON_DANGER red
BUTTON_LINK // BUTTON_LINK grey, navigate to URL
)

// ComponentType
Expand Down Expand Up @@ -59,6 +60,8 @@ type Button struct {
Disabled bool `json:"disabled,omitempty"`
}

// Emoji
// https://discord.com/developers/docs/resources/emoji#emoji-object
type Emoji struct {
ID Snowflake `json:"id"`
Name string `json:"name"`
Expand Down
15 changes: 13 additions & 2 deletions embed-builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package corde

import "fmt"

// Embed builder
// https://regex101.com/r/gmVH2A/3
// EmbedB is an Embed builder
// https://regex101.com/r/gmVH2A/4
type EmbedB struct {
Embed
}
Expand All @@ -25,48 +25,58 @@ func NewEmbed() *EmbedB {
}
}

// B returns the built Embed
func (b *EmbedB) B() Embed { return b.Embed }

// Author adds the author to the Embed
func (b *EmbedB) Author(a Author) *EmbedB {
b.Embed.Author = a
return b
}

// Footer adds the footer to the Embed
func (b *EmbedB) Footer(f Footer) *EmbedB {
b.Embed.Footer = f
return b
}

// Title adds the title to the Embed
func (b *EmbedB) Title(s string) *EmbedB {
b.Embed.Title = s
return b
}

// Titlef adds the Title to the Embed
func (b *EmbedB) Titlef(format string, a ...any) *EmbedB {
b.Embed.Title = fmt.Sprintf(format, a...)
return b
}

// Description adds the description to the Embed
func (b *EmbedB) Description(s string) *EmbedB {
b.Embed.Description = s
return b
}

// Descriptionf adds the description to the Embed
func (b *EmbedB) Descriptionf(format string, a ...any) *EmbedB {
b.Embed.Description = fmt.Sprintf(format, a...)
return b
}

// Thumbnail adds the thumbnail to the Embed
func (b *EmbedB) Thumbnail(i Image) *EmbedB {
b.Embed.Thumbnail = i
return b
}

// Image adds the image to the Embed
func (b *EmbedB) Image(i Image) *EmbedB {
b.Embed.Image = i
return b
}

// URL adds the url to the Embed
func (b *EmbedB) URL(s string) *EmbedB {
b.Embed.URL = s
return b
Expand All @@ -77,6 +87,7 @@ func (b *EmbedB) Fields(f ...Field) *EmbedB {
return b
}

// Color adds the color to the Embed
func (b *EmbedB) Color(i int64) *EmbedB {
b.Embed.Color = i
return b
Expand Down
24 changes: 18 additions & 6 deletions interaction-response-builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package corde

import "fmt"

// ResponseBuilder
// https://regex101.com/r/tKfloG/1
// RespB is an InteractionRespData builder
// https://regex101.com/r/tKfloG/2
type RespB struct {
*InteractionRespData
}

// Returns a new response builder
// NewResp Returns a new response builder
func NewResp() *RespB {
return &RespB{
InteractionRespData: &InteractionRespData{
Expand All @@ -18,52 +18,63 @@ func NewResp() *RespB {
AllowedMentions: &AllowedMentions{},
Flags: 0,
Components: []Component{},
Attachements: []Attachment{},
Attachments: []Attachment{},
},
}
}

// B returns the build InteractionRespData
func (b *RespB) B() *InteractionRespData { return b.InteractionRespData }

// Content adds the content to the InteractionRespData
func (b *RespB) Content(s string) *RespB {
b.InteractionRespData.Content = s
return b
}

// Contentf adds the content to the InteractionRespData
func (b *RespB) Contentf(s string, args ...any) *RespB {
b.InteractionRespData.Content = fmt.Sprintf(s, args...)
return b
}

// TTS adds the tts to the InteractionRespData
func (b *RespB) TTS(tts bool) *RespB {
b.InteractionRespData.TTS = tts
return b
}

// Embeds adds embeds to the InteractionRespData
func (b *RespB) Embeds(e ...Embed) *RespB {
b.InteractionRespData.Embeds = append(b.InteractionRespData.Embeds, e...)
return b
}

// AllowedMentions adds the allowed mentions to the InteractionRespData
func (b *RespB) AllowedMentions(a *AllowedMentions) *RespB {
b.InteractionRespData.AllowedMentions = a
return b
}

// Flags adds the flags to the InteractionRespData
func (b *RespB) Flags(i IntResponseFlags) *RespB {
b.InteractionRespData.Flags = i
return b
}

// Ephemeral adds the ephemeral flag to the InteractionRespData
func (b *RespB) Ephemeral() *RespB {
b.InteractionRespData.Flags = RESPONSE_FLAGS_EPHEMERAL
return b
}

// Components adds components to the InteractionRespData
func (b *RespB) Components(c ...Component) *RespB {
b.InteractionRespData.Components = append(b.InteractionRespData.Components, c...)
return b
}

// ActionRow adds an action row to the InteractionRespData
func (b *RespB) ActionRow(c ...Component) *RespB {
b.InteractionRespData.Components = append(b.InteractionRespData.Components,
Component{
Expand All @@ -74,7 +85,8 @@ func (b *RespB) ActionRow(c ...Component) *RespB {
return b
}

func (b *RespB) Attachements(a ...Attachment) *RespB {
b.InteractionRespData.Attachements = append(b.InteractionRespData.Attachements, a...)
// Attachments adds attachments to the InteractionRespData
func (b *RespB) Attachments(a ...Attachment) *RespB {
b.InteractionRespData.Attachments = append(b.InteractionRespData.Attachments, a...)
return b
}
2 changes: 1 addition & 1 deletion interaction-responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type InteractionRespData struct {
AllowedMentions *AllowedMentions `json:"allowed_mentions,omitempty"`
Flags IntResponseFlags `json:"flags,omitempty"`
Components []Component `json:"components,omitempty"`
Attachements []Attachment `json:"attachments,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
}

type IntResponseFlags uint
Expand Down
Loading

0 comments on commit 6c1bf2f

Please sign in to comment.