Skip to content

Commit

Permalink
Use InteractionRequest for incoming Interactions, attach request.Cont…
Browse files Browse the repository at this point in the history
…ext (#18)

Signed-off-by: jolheiser <[email protected]>
  • Loading branch information
jolheiser authored Jan 7, 2022
1 parent fc0fcd4 commit ccea574
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion 0_example/bongo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
}
}

func bongoHandler(w corde.ResponseWriter, _ *corde.Interaction) {
func bongoHandler(w corde.ResponseWriter, _ *corde.InteractionRequest) {
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
10 changes: 5 additions & 5 deletions 0_example/moderate-myself/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func Test_list(t *testing.T) {
tests := []struct {
name string
mock owmock.ResponseWriterMock
interaction *corde.Interaction
interaction *corde.InteractionRequest
}{
{
name: "list",
Expand All @@ -30,7 +30,7 @@ func Test_list(t *testing.T) {
}
},
},
interaction: &corde.Interaction{},
interaction: &corde.InteractionRequest{},
},
}
for _, tt := range tests {
Expand All @@ -45,8 +45,8 @@ func Test_btnNext(t *testing.T) {
tests := []struct {
name string
mock owmock.ResponseWriterMock
interaction *corde.Interaction
fn func(corde.ResponseWriter, *corde.Interaction)
interaction *corde.InteractionRequest
fn func(corde.ResponseWriter, *corde.InteractionRequest)
}{
{
name: "btn next",
Expand All @@ -63,7 +63,7 @@ func Test_btnNext(t *testing.T) {
}
},
},
interaction: &corde.Interaction{},
interaction: &corde.InteractionRequest{},
fn: btnNext(&corde.Mux{Client: http.DefaultClient}, corde.GuildOpt(0), &sync.Mutex{}, &selectedID),
},
}
Expand Down
12 changes: 6 additions & 6 deletions 0_example/moderate-myself/main.go
Original file line number Diff line number Diff line change
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.Interaction) {
return func(w corde.ResponseWriter, _ *corde.Interaction) {
func list(m *corde.Mux, g func(*corde.CommandsOpt)) func(corde.ResponseWriter, *corde.InteractionRequest) {
return func(w corde.ResponseWriter, _ *corde.InteractionRequest) {
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.Interaction) {
return func(w corde.ResponseWriter, _ *corde.Interaction) {
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) {
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.Interaction) {
return func(w corde.ResponseWriter, _ *corde.Interaction) {
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) {
mu.Lock()
defer mu.Unlock()
commands, err := m.GetCommands(g)
Expand Down
8 changes: 4 additions & 4 deletions 0_example/todo/todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type todoItem struct {
value string
}

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

Expand All @@ -36,7 +36,7 @@ func (t *todo) autoCompleteNames(w corde.ResponseWriter, i *corde.Interaction) {
w.Autocomplete(resp)
}

func (t *todo) addHandler(w corde.ResponseWriter, i *corde.Interaction) {
func (t *todo) addHandler(w corde.ResponseWriter, i *corde.InteractionRequest) {
value := i.Data.Options.String("value")
name := i.Data.Options.String("name")

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

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

Expand All @@ -82,7 +82,7 @@ func (t *todo) listHandler(w corde.ResponseWriter, _ *corde.Interaction) {
)
}

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

Expand Down
25 changes: 13 additions & 12 deletions router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package corde

import (
"context"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -113,7 +114,7 @@ func NewMux(publicKey string, appID Snowflake, botToken string) *Mux {
},
PublicKey: publicKey,
BasePath: "/",
OnNotFound: func(_ ResponseWriter, i *Interaction) {
OnNotFound: func(_ ResponseWriter, i *InteractionRequest) {
n := i.Data.Name
for r, o := range i.Data.Options {
n += fmt.Sprintf("/%s=%s", r, o.String())
Expand All @@ -130,7 +131,7 @@ func NewMux(publicKey string, appID Snowflake, botToken string) *Mux {
}

// Handler handles incoming requests
type Handler func(ResponseWriter, *Interaction)
type Handler func(ResponseWriter, *InteractionRequest)

// ResponseWriter handles responding to interactions
// https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-interaction-callback-type
Expand All @@ -143,6 +144,12 @@ type ResponseWriter interface {
Autocomplete(InteractionResponder)
}

// InteractionRequest is an incoming request Interaction
type InteractionRequest struct {
*Interaction
Context context.Context
}

// ListenAndServe starts the gateway listening to events
func (m *Mux) ListenAndServe(addr string) error {
validator := rest.Verify(m.PublicKey)
Expand All @@ -160,7 +167,9 @@ func (m *Mux) Handler() http.Handler {

// route handles routing the requests
func (m *Mux) route(w http.ResponseWriter, r *http.Request) {
i := &Interaction{}
i := &InteractionRequest{
Context: r.Context(),
}
if err := json.NewDecoder(r.Body).Decode(i); err != nil {
log.Println("Errors unmarshalling json: ", err)
w.WriteHeader(http.StatusBadRequest)
Expand All @@ -171,7 +180,7 @@ func (m *Mux) route(w http.ResponseWriter, r *http.Request) {
}

// routeReq is a recursive implementation to route requests
func (m *Mux) routeReq(r ResponseWriter, i *Interaction) {
func (m *Mux) routeReq(r ResponseWriter, i *InteractionRequest) {
m.rMu.RLock()
defer m.rMu.RUnlock()
switch i.Type {
Expand Down Expand Up @@ -224,14 +233,6 @@ func (m *Mux) routeReq(r ResponseWriter, i *Interaction) {
m.OnNotFound(r, i)
}

// reqOpts applies functions on an http request.
// useful for setting headers
func reqOpts(req *http.Request, h ...func(*http.Request)) {
for _, option := range h {
option(req)
}
}

// authorize adds the Authorization header to the request
func (m *Mux) authorize(req *http.Request) {
req.Header.Add("Authorization", "Bot "+m.BotToken)
Expand Down
10 changes: 5 additions & 5 deletions sample-component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
)

func TestComponentInteraction(t *testing.T) {
is := is.New(t)
assert := is.New(t)
pub, _ := owmock.GenerateKeys()
mux := corde.NewMux(pub, 0, "")

mux.Button("click_one", func(w corde.ResponseWriter, _ *corde.Interaction) {
mux.Button("click_one", func(w corde.ResponseWriter, _ *corde.InteractionRequest) {
w.Respond(&corde.InteractionRespData{
Content: "Hello World!",
})
Expand All @@ -30,13 +30,13 @@ func TestComponentInteraction(t *testing.T) {

s := httptest.NewServer(mux.Handler())
respPost, err := owmock.NewWithClient(s.URL, s.Client()).Post(SampleComponent)
is.NoErr(err)
assert.NoErr(err)

respV := &owmock.InteractionResponse{}
err = json.Unmarshal(respPost, respV)
is.NoErr(err)
assert.NoErr(err)

is.Equal(expect, respV)
assert.Equal(expect, respV)
}

const SampleComponent = `{
Expand Down

0 comments on commit ccea574

Please sign in to comment.