From 59e30da435115e99dd58c8edb5b8f517c7031e53 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Thu, 6 Jan 2022 16:42:12 -0600 Subject: [PATCH] Use InteractionRequest for incoming Interactions, attach request.Context Signed-off-by: jolheiser --- 0_example/bongo/main.go | 2 +- 0_example/moderate-myself/commands_test.go | 10 ++++----- 0_example/moderate-myself/main.go | 12 +++++------ 0_example/todo/todo.go | 8 +++---- router.go | 25 +++++++++++----------- sample-component_test.go | 10 ++++----- 6 files changed, 34 insertions(+), 33 deletions(-) diff --git a/0_example/bongo/main.go b/0_example/bongo/main.go index 0b7476d..d6bdb2b 100644 --- a/0_example/bongo/main.go +++ b/0_example/bongo/main.go @@ -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()) diff --git a/0_example/moderate-myself/commands_test.go b/0_example/moderate-myself/commands_test.go index 580f4af..331d466 100644 --- a/0_example/moderate-myself/commands_test.go +++ b/0_example/moderate-myself/commands_test.go @@ -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", @@ -30,7 +30,7 @@ func Test_list(t *testing.T) { } }, }, - interaction: &corde.Interaction{}, + interaction: &corde.InteractionRequest{}, }, } for _, tt := range tests { @@ -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", @@ -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), }, } diff --git a/0_example/moderate-myself/main.go b/0_example/moderate-myself/main.go index 57b4da3..a5930ec 100644 --- a/0_example/moderate-myself/main.go +++ b/0_example/moderate-myself/main.go @@ -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(). @@ -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) @@ -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) diff --git a/0_example/todo/todo.go b/0_example/todo/todo.go index cd71ea0..f4a3b90 100644 --- a/0_example/todo/todo.go +++ b/0_example/todo/todo.go @@ -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() @@ -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") @@ -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() @@ -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() diff --git a/router.go b/router.go index 398fe3f..38d716f 100644 --- a/router.go +++ b/router.go @@ -1,6 +1,7 @@ package corde import ( + "context" "encoding/json" "fmt" "log" @@ -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()) @@ -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 @@ -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) @@ -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) @@ -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 { @@ -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) diff --git a/sample-component_test.go b/sample-component_test.go index 07c74f1..e6be2da 100644 --- a/sample-component_test.go +++ b/sample-component_test.go @@ -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!", }) @@ -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 = `{