Skip to content

Commit

Permalink
Builder to write embeds and interaction responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Karitham committed Dec 25, 2021
1 parent 3e69c1c commit 43126b8
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 142 deletions.
18 changes: 8 additions & 10 deletions _example/bongo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ func main() {
func bongoHandler(w corde.ResponseWriter, i *corde.Interaction) {
resp, err := http.Get("https://cdn.discordapp.com/emojis/745709799890747434.gif?size=128")
if err != nil {
w.WithSource(&corde.InteractionRespData{Content: "couldn't retrieve bongo", Flags: corde.RESPONSE_FLAGS_EPHEMERAL})
w.Respond(corde.NewResp().Content("couldn't retrieve bongo").Ephemeral().B())
return
}
defer resp.Body.Close()
w.WithSource(&corde.InteractionRespData{
Attachements: []corde.Attachment{
{
Body: resp.Body,
ID: corde.Snowflake(0),
Filename: "bongo.gif",
},
},
})
w.Respond(corde.NewResp().
Attachements(corde.Attachment{
Body: resp.Body,
ID: corde.Snowflake(0),
Filename: "bongo.gif",
}).B(),
)
}
132 changes: 45 additions & 87 deletions _example/moderate-myself/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"log"
"os"
"sync"
Expand Down Expand Up @@ -69,57 +68,53 @@ func main() {
}
}

var nextBtn = corde.Component{
Type: corde.COMPONENT_BUTTON,
CustomID: "btn-cmd/list/next",
Style: corde.BUTTON_SECONDARY,
Label: "next",
Emoji: &corde.Emoji{Name: "➡️"},
}

var delBtn = corde.Component{
Type: corde.COMPONENT_BUTTON,
CustomID: "btn-cmd/list/remove",
Style: corde.BUTTON_DANGER,
Label: "remove",
Emoji: &corde.Emoji{Name: "🗑️"},
}

func list(m *corde.Mux, g func(*corde.CommandsOpt)) func(corde.ResponseWriter, *corde.Interaction) {
return func(w corde.ResponseWriter, i *corde.Interaction) {
w.WithSource(&corde.InteractionRespData{
Components: []corde.Component{
{
Type: corde.COMPONENT_ACTION_ROW,
Components: []corde.Component{
{
Type: corde.COMPONENT_BUTTON,
CustomID: "btn-cmd/list/next",
Style: corde.BUTTON_SECONDARY,
Label: "next",
Emoji: &corde.Emoji{Name: "➡️"},
},
},
},
},
Content: fmt.Sprintf("Click on the buttons to move between existing commands and or delete them."),
Flags: corde.RESPONSE_FLAGS_EPHEMERAL,
})
w.Respond(corde.NewResp().
ActionRow(nextBtn).
Ephemeral().
Content("Click on the buttons to move between existing commands and or delete them.").
B(),
)
}
}

func rm(m *corde.Mux, g func(*corde.CommandsOpt)) func(corde.ResponseWriter, *corde.Interaction) {
return func(w corde.ResponseWriter, i *corde.Interaction) {
n, ok := i.Data.Options["name"]
if !ok {
w.WithSource(&corde.InteractionRespData{
Content: "Please enter an actual command name.",
Flags: corde.RESPONSE_FLAGS_EPHEMERAL,
})
w.Respond(
corde.NewResp().Content("Please enter an actual command name.").Ephemeral().B(),
)
return
}

c, _ := m.GetCommands(g)
for _, c := range c {
if c.Name == n {
m.DeleteCommand(c.ID, g)
w.WithSource(&corde.InteractionRespData{
Content: fmt.Sprintf("Removed command %s.", n),
Flags: corde.RESPONSE_FLAGS_EPHEMERAL,
})

w.Respond(corde.NewResp().Content("No command named %s found.", n).Ephemeral().B())
return
}
}

w.WithSource(&corde.InteractionRespData{
Content: fmt.Sprintf("No command named %s found.", n),
Flags: corde.RESPONSE_FLAGS_EPHEMERAL,
})
w.Respond(corde.NewResp().Contentf("No command named %s found.", n).Ephemeral().B())
}
}

Expand All @@ -129,39 +124,18 @@ func btnNext(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedI
defer mu.Unlock()
commands, _ := m.GetCommands(g)
if len(commands) == 0 {
w.UpdateMessage(&corde.InteractionRespData{
Content: "No commands found.",
Flags: corde.RESPONSE_FLAGS_EPHEMERAL,
})
w.Update(corde.NewResp().Content("No commands found.").Ephemeral().B())
return
}
*selectedID = *selectedID + 1%(len(commands))

w.UpdateMessage(&corde.InteractionRespData{
Content: fmt.Sprintf("%s - %s", commands[*selectedID].Name, commands[*selectedID].Description),
Components: []corde.Component{
{
Type: corde.COMPONENT_ACTION_ROW,
Components: []corde.Component{
{
Type: corde.COMPONENT_BUTTON,
CustomID: "btn-cmd/list/next",
Style: corde.BUTTON_SECONDARY,
Label: "next",
Emoji: &corde.Emoji{Name: "➡️"},
},
{
Type: corde.COMPONENT_BUTTON,
CustomID: "btn-cmd/list/remove",
Style: corde.BUTTON_DANGER,
Label: "remove",
Emoji: &corde.Emoji{Name: "🗑️"},
},
},
},
},
Flags: corde.RESPONSE_FLAGS_EPHEMERAL,
})
*selectedID = (*selectedID + 1) % len(commands)

w.Update(corde.NewResp().
Contentf("%s - %s", commands[*selectedID].Name, commands[*selectedID].Description).
ActionRow(nextBtn, delBtn).
Ephemeral().
B(),
)
}
}

Expand All @@ -174,30 +148,14 @@ func btnRemove(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selecte

m.DeleteCommand(c.ID, g)

w.UpdateMessage(&corde.InteractionRespData{
Content: fmt.Sprintf("%s - %s", commands[*selectedID].Name, commands[*selectedID].Description),
Components: []corde.Component{
{
Type: corde.COMPONENT_ACTION_ROW,
Components: []corde.Component{
{
Type: corde.COMPONENT_BUTTON,
CustomID: "btn-cmd/list/next",
Style: corde.BUTTON_SECONDARY,
Label: "next",
Emoji: &corde.Emoji{Name: "➡️"},
},
{
Type: corde.COMPONENT_BUTTON,
CustomID: "btn-cmd/list/remove",
Style: corde.BUTTON_DANGER,
Label: "remove",
Emoji: &corde.Emoji{Name: "🗑️"},
},
},
},
},
Flags: corde.RESPONSE_FLAGS_EPHEMERAL,
})
commands, _ = m.GetCommands(g)
*selectedID = (*selectedID + 1) % len(commands)

w.Update(corde.NewResp().
Contentf("%s - %s", commands[*selectedID].Name, commands[*selectedID].Description).
ActionRow(nextBtn, delBtn).
Ephemeral().
B(),
)
}
}
55 changes: 25 additions & 30 deletions _example/todo/todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,51 @@ type todo struct {
func (t *todo) addHandler(w corde.ResponseWriter, i *corde.Interaction) {
value, ok := i.Data.Options["value"].(string)
if !ok {
ephemeral(w, "no value provided")
w.Respond(corde.NewResp().Content("no value provided").Ephemeral().B())
return
}

name, ok := i.Data.Options["name"].(string)
if !ok {
ephemeral(w, "no name provided")
w.Respond(corde.NewResp().Content("no name provided").Ephemeral().B())
return
}

t.mu.Lock()
defer t.mu.Unlock()
t.list[name] = value

ephemeral(w, fmt.Sprintf("Sucessfully added %s", name))
w.Respond(corde.NewResp().Contentf("Sucessfully added %s", name).Ephemeral().B())
}

func (t *todo) listHandler(w corde.ResponseWriter, i *corde.Interaction) {
t.mu.Lock()
defer t.mu.Unlock()

if len(t.list) == 0 {
ephemeral(w, "no todos")
w.Respond(corde.NewResp().Content("no todos").Ephemeral().B())
return
}

// build todo list
s := &strings.Builder{}
s.WriteString("```todo\n")
for k, v := range t.list {
s.WriteString(fmt.Sprintf("- %s: %s\n", k, v))
}
s.WriteString("```")

w.WithSource(&corde.InteractionRespData{
Embeds: []corde.Embed{
{
Title: "Todo list",
Description: s.String(),
},
},
})
w.Respond(corde.NewResp().
Embeds(corde.NewEmbed().
Title("Todo list").
// build todo list description
Description(func() string {
s := &strings.Builder{}
s.WriteString("```todo\n")
i := 0
for k, v := range t.list {
i++
s.WriteString(fmt.Sprintf("%d. %s: %s\n", i, k, v))
}
s.WriteString("```")
return s.String()
}()).
Color(0x69b00b).
B(),
).B(),
)
}

func (t *todo) removeHandler(w corde.ResponseWriter, i *corde.Interaction) {
Expand All @@ -66,18 +69,10 @@ func (t *todo) removeHandler(w corde.ResponseWriter, i *corde.Interaction) {

name, ok := i.Data.Options["name"].(string)
if !ok {
ephemeral(w, "no name provided")
w.Respond(corde.NewResp().Content("no name provided").Ephemeral().B())
return
}

delete(t.list, name)
ephemeral(w, "deleted todo")
}

// ephemeral returns an ephemeral response
func ephemeral(w corde.ResponseWriter, message string) {
w.WithSource(&corde.InteractionRespData{
Content: message,
Flags: corde.RESPONSE_FLAGS_EPHEMERAL,
})
w.Respond(corde.NewResp().Content("deleted todo").Ephemeral().B())
}
71 changes: 71 additions & 0 deletions embed-builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package corde

// Embed builder
// https://regex101.com/r/gmVH2A/3
type EmbedB struct {
Embed
}

// NewEmbed returns a new embed builder ready for use
func NewEmbed() *EmbedB {
return &EmbedB{
Embed: Embed{
Author: Author{},
Footer: Footer{},
Title: "",
Description: "",
Thumbnail: Image{},
Image: Image{},
URL: "",
Fields: []Field{},
Color: 0,
},
}
}

func (b *EmbedB) B() Embed { return b.Embed }

func (b *EmbedB) Author(a Author) *EmbedB {
b.Embed.Author = a
return b
}

func (b *EmbedB) Footer(f Footer) *EmbedB {
b.Embed.Footer = f
return b
}

func (b *EmbedB) Title(s string) *EmbedB {
b.Embed.Title = s
return b
}

func (b *EmbedB) Description(s string) *EmbedB {
b.Embed.Description = s
return b
}

func (b *EmbedB) Thumbnail(i Image) *EmbedB {
b.Embed.Thumbnail = i
return b
}

func (b *EmbedB) Image(i Image) *EmbedB {
b.Embed.Image = i
return b
}

func (b *EmbedB) URL(s string) *EmbedB {
b.Embed.URL = s
return b
}

func (b *EmbedB) Fields(f ...Field) *EmbedB {
b.Embed.Fields = append(b.Embed.Fields, f...)
return b
}

func (b *EmbedB) Color(i int64) *EmbedB {
b.Embed.Color = i
return b
}
Loading

0 comments on commit 43126b8

Please sign in to comment.