Skip to content

Commit

Permalink
Exp/interaction data union (#29)
Browse files Browse the repository at this point in the history
* exp with an interaction data union

* compiler panic

* comment out assignment

* upgrade routing, mostly stable

* fix Route field on Request

* remove migration to seperate folders

* feat: modals

* fix: constant naming, CI pipeline
  • Loading branch information
karitham authored Oct 8, 2022
1 parent cc9f3e1 commit c659cfe
Show file tree
Hide file tree
Showing 32 changed files with 1,249 additions and 885 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: "1.18.0-beta1"
stable: "false"
go-version: "1.18"
- name: Run Tests
run: go test -race -v ./...
- name: Run Vet
run: go vet ./...
- name: Example Compliance
run: |
go build ./0_example/bongo
go build ./0_example/moderate-myself
go build ./0_example/nft
go build ./0_example/todo
run: go build -v -a ./0_example/...
4 changes: 2 additions & 2 deletions 0_example/bongo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
}

m := corde.NewMux(pk, appID, token)
m.Command("bongo", bongoHandler)
m.SlashCommand("bongo", bongoHandler)

g := corde.GuildOpt(corde.SnowflakeFromString(os.Getenv("DISCORD_GUILD_ID")))
if err := m.RegisterCommand(command, g); err != nil {
Expand All @@ -38,7 +38,7 @@ func main() {
}
}

func bongoHandler(w corde.ResponseWriter, _ *corde.InteractionRequest) {
func bongoHandler(w corde.ResponseWriter, _ *corde.Request[corde.SlashCommandInteractionData]) {
resp, err := http.Get("https://cdn.discordapp.com/emojis/745709799890747434.gif?size=128")
if err != nil {
w.Respond(corde.NewResp().Content("couldn't retrieve bongo").Ephemeral())
Expand Down
59 changes: 59 additions & 0 deletions 0_example/modal/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"encoding/json"
"log"
"os"

"github.com/Karitham/corde"
)

var command = corde.NewSlashCommand("modal", "send a modal")

func main() {
token := os.Getenv("DISCORD_BOT_TOKEN")
if token == "" {
log.Fatalln("DISCORD_BOT_TOKEN not set")
}
appID := corde.SnowflakeFromString(os.Getenv("DISCORD_APP_ID"))
if appID == 0 {
log.Fatalln("DISCORD_APP_ID not set")
}
pk := os.Getenv("DISCORD_PUBLIC_KEY")
if pk == "" {
log.Fatalln("DISCORD_PUBLIC_KEY not set")
}

m := corde.NewMux(pk, appID, token)
m.SlashCommand("modal", respondModal)
m.Modal("pog-modal", func(w corde.ResponseWriter, r *corde.Request[corde.ModalInteractionData]) {
json.NewEncoder(os.Stderr).Encode(r)
w.DeferedUpdate()
})

g := corde.GuildOpt(corde.SnowflakeFromString(os.Getenv("DISCORD_GUILD_ID")))
if err := m.RegisterCommand(command, g); err != nil {
log.Fatalln("error registering command: ", err)
}

log.Println("serving on :8070")
if err := m.ListenAndServe(":8070"); err != nil {
log.Fatalln(err)
}
}

func respondModal(w corde.ResponseWriter, r *corde.Request[corde.SlashCommandInteractionData]) {
w.Modal(corde.Modal{
Title: "xoxo",
CustomID: "pog-modal",
Components: []corde.Component{
corde.TextInputComponent{
CustomID: "pog-component",
Style: corde.TEXT_PARAGRAPH,
Label: "label",
Required: false,
Placeholder: "placeholder",
}.Component(),
},
})
}
11 changes: 6 additions & 5 deletions 0_example/moderate-myself/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/Karitham/corde"

"github.com/Karitham/corde/owmock"
)

Expand All @@ -15,7 +16,7 @@ func Test_list(t *testing.T) {
tests := []struct {
name string
mock owmock.ResponseWriterMock
interaction *corde.InteractionRequest
interaction *corde.Request[corde.SlashCommandInteractionData]
}{
{
name: "list",
Expand All @@ -30,7 +31,7 @@ func Test_list(t *testing.T) {
}
},
},
interaction: &corde.InteractionRequest{},
interaction: &corde.Request[corde.SlashCommandInteractionData]{},
},
}
for _, tt := range tests {
Expand All @@ -45,8 +46,8 @@ func Test_btnNext(t *testing.T) {
tests := []struct {
name string
mock owmock.ResponseWriterMock
interaction *corde.InteractionRequest
fn func(corde.ResponseWriter, *corde.InteractionRequest)
interaction *corde.Request[corde.ButtonInteractionData]
fn func(corde.ResponseWriter, *corde.Request[corde.ButtonInteractionData])
}{
{
name: "btn next",
Expand All @@ -63,7 +64,7 @@ func Test_btnNext(t *testing.T) {
}
},
},
interaction: &corde.InteractionRequest{},
interaction: &corde.Request[corde.ButtonInteractionData]{},
fn: btnNext(&corde.Mux{Client: http.DefaultClient}, corde.GuildOpt(0), &sync.Mutex{}, &selectedID),
},
}
Expand Down
23 changes: 14 additions & 9 deletions 0_example/moderate-myself/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func main() {
m := corde.NewMux(pk, appID, token)
m.Route("cmd", func(m *corde.Mux) {
m.Route("list", func(m *corde.Mux) {
m.Command("", list(m, g))
m.Button("next", btnNext(m, g, mu, &selectedID))
m.Button("remove", btnRemove(m, g, mu, &selectedID))
m.SlashCommand("", list(m, g))
m.ButtonComponent("next", btnNext(m, g, mu, &selectedID))
m.ButtonComponent("remove", btnRemove(m, g, mu, &selectedID))
})
})

Expand Down Expand Up @@ -68,8 +68,8 @@ var delBtn = corde.Component{
Emoji: &corde.Emoji{Name: "🗑️"},
}

func list(m *corde.Mux, g func(*corde.CommandsOpt)) func(corde.ResponseWriter, *corde.InteractionRequest) {
return func(w corde.ResponseWriter, _ *corde.InteractionRequest) {
func list(m *corde.Mux, g func(*corde.CommandsOpt)) func(corde.ResponseWriter, *corde.Request[corde.SlashCommandInteractionData]) {
return func(w corde.ResponseWriter, _ *corde.Request[corde.SlashCommandInteractionData]) {
w.Respond(corde.NewResp().
ActionRow(nextBtn).
Ephemeral().
Expand All @@ -78,8 +78,8 @@ func list(m *corde.Mux, g func(*corde.CommandsOpt)) func(corde.ResponseWriter, *
}
}

func btnNext(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedID *int) func(corde.ResponseWriter, *corde.InteractionRequest) {
return func(w corde.ResponseWriter, _ *corde.InteractionRequest) {
func btnNext(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedID *int) func(corde.ResponseWriter, *corde.Request[corde.ButtonInteractionData]) {
return func(w corde.ResponseWriter, _ *corde.Request[corde.ButtonInteractionData]) {
mu.Lock()
defer mu.Unlock()
commands, err := m.GetCommands(g)
Expand All @@ -102,8 +102,8 @@ func btnNext(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedI
}
}

func btnRemove(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedID *int) func(corde.ResponseWriter, *corde.InteractionRequest) {
return func(w corde.ResponseWriter, _ *corde.InteractionRequest) {
func btnRemove(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedID *int) func(corde.ResponseWriter, *corde.Request[corde.ButtonInteractionData]) {
return func(w corde.ResponseWriter, _ *corde.Request[corde.ButtonInteractionData]) {
mu.Lock()
defer mu.Unlock()
commands, err := m.GetCommands(g)
Expand All @@ -116,6 +116,11 @@ func btnRemove(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selecte
m.DeleteCommand(c.ID, g)

commands, _ = m.GetCommands(g)
if len(commands) == 0 {
w.Update(corde.NewResp().Content("No commands found.").Ephemeral())
return
}

*selectedID = (*selectedID + 1) % len(commands)

w.Update(corde.NewResp().
Expand Down
17 changes: 8 additions & 9 deletions 0_example/nft/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func main() {
}

m.Route("nft", func(m *corde.Mux) {
m.Command("user", NFTuser)
m.Command("message", NFTmessage)
m.UserCommand("user", NFTuser)
m.MessageCommand("message", NFTmessage)
})

log.Println("serving on :8070")
Expand All @@ -49,37 +49,36 @@ func main() {
}
}

func NFTuser(w corde.ResponseWriter, i *corde.InteractionRequest) {
func NFTuser(w corde.ResponseWriter, i *corde.Request[corde.UserCommandInteractionData]) {
user := i.Data.Resolved.Users.First()
url := user.AvatarURL()
username := user.Username

if url == "" {
w.Respond(corde.NewResp().Contentf("error getting %s's profile pic", username).Ephemeral())
w.Respond(corde.NewResp().Contentf("error getting %s's profile pic", user.Username).Ephemeral())
return
}

resp, err := http.Get(url)
if err != nil {
w.Respond(corde.NewResp().Contentf("error getting %s's profile pic", username).Ephemeral())
w.Respond(corde.NewResp().Contentf("error getting %s's profile pic", user.Username).Ephemeral())
return
}
defer resp.Body.Close()

filename := filepath.Base(url)
w.Respond(corde.NewResp().
Contentf("Good job %s, you just minted %s's profile picture", i.User.Username, username).
Contentf("Good job %s, you just minted %s's profile picture", i.Member.User.Username, user.Username).
Attachment(resp.Body, filename),
)
}

func NFTmessage(w corde.ResponseWriter, i *corde.InteractionRequest) {
func NFTmessage(w corde.ResponseWriter, i *corde.Request[corde.MessageCommandInteractionData]) {
msg := i.Data.Resolved.Messages.First()
chanID := msg.ChannelID
msgID := msg.ID

message := fmt.Sprintf("https://discordapp.com/channels/%d/%d/%d", i.GuildID, chanID, msgID)
w.Respond(corde.NewResp().
Contentf("Good job %s, you just minted this message, here's the link %s", i.User.Username, message),
Contentf("Good job %s, you just minted this message, here's the link %s", i.Member.User.Username, message),
)
}
6 changes: 3 additions & 3 deletions 0_example/todo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func main() {

m := corde.NewMux(pk, appID, token)
m.Route("todo", func(m *corde.Mux) {
m.Command("add", t.addHandler)
m.Command("list", t.listHandler)
m.SlashCommand("add", t.addHandler)
m.SlashCommand("list", t.listHandler)
m.Route("rm", func(m *corde.Mux) {
m.Command("", t.removeHandler)
m.SlashCommand("", t.removeHandler)
m.Autocomplete("name", t.autoCompleteNames)
})
})
Expand Down
9 changes: 5 additions & 4 deletions 0_example/todo/todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"

"github.com/Karitham/corde"

"github.com/Karitham/corde/format"
)

Expand All @@ -19,7 +20,7 @@ type todoItem struct {
value string
}

func (t *todo) autoCompleteNames(w corde.ResponseWriter, _ *corde.InteractionRequest) {
func (t *todo) autoCompleteNames(w corde.ResponseWriter, _ *corde.Request[corde.AutocompleteInteractionData]) {
t.mu.Lock()
defer t.mu.Unlock()

Expand All @@ -36,7 +37,7 @@ func (t *todo) autoCompleteNames(w corde.ResponseWriter, _ *corde.InteractionReq
w.Autocomplete(resp)
}

func (t *todo) addHandler(w corde.ResponseWriter, i *corde.InteractionRequest) {
func (t *todo) addHandler(w corde.ResponseWriter, i *corde.Request[corde.SlashCommandInteractionData]) {
value, _ := i.Data.Options.String("value")
name, _ := i.Data.Options.String("name")

Expand All @@ -56,7 +57,7 @@ func (t *todo) addHandler(w corde.ResponseWriter, i *corde.InteractionRequest) {
w.Respond(corde.NewResp().Contentf("Successfully added %s", name).Ephemeral())
}

func (t *todo) listHandler(w corde.ResponseWriter, _ *corde.InteractionRequest) {
func (t *todo) listHandler(w corde.ResponseWriter, _ *corde.Request[corde.SlashCommandInteractionData]) {
t.mu.Lock()
defer t.mu.Unlock()

Expand All @@ -79,7 +80,7 @@ func (t *todo) listHandler(w corde.ResponseWriter, _ *corde.InteractionRequest)
)
}

func (t *todo) removeHandler(w corde.ResponseWriter, i *corde.InteractionRequest) {
func (t *todo) removeHandler(w corde.ResponseWriter, i *corde.Request[corde.SlashCommandInteractionData]) {
t.mu.Lock()
defer t.mu.Unlock()

Expand Down
Loading

0 comments on commit c659cfe

Please sign in to comment.