Skip to content

Commit

Permalink
probably fix discord unmarshalling mess
Browse files Browse the repository at this point in the history
  • Loading branch information
plouis.pery committed Apr 30, 2024
1 parent e41cb86 commit ace7ba9
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 65 deletions.
3 changes: 2 additions & 1 deletion 0_example/bongo/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -38,7 +39,7 @@ func main() {
}
}

func bongoHandler(w corde.ResponseWriter, _ *corde.Request[corde.SlashCommandInteractionData]) {
func bongoHandler(ctx context.Context, w corde.ResponseWriter, _ *corde.Interaction[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
5 changes: 3 additions & 2 deletions 0_example/modal/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"log"
"os"
Expand All @@ -26,7 +27,7 @@ func main() {

m := corde.NewMux(pk, appID, token)
m.SlashCommand("modal", respondModal)
m.Modal("pog-modal", func(w corde.ResponseWriter, r *corde.Request[corde.ModalInteractionData]) {
m.Modal("pog-modal", func(ctx context.Context, w corde.ResponseWriter, r *corde.Interaction[corde.ModalInteractionData]) {
json.NewEncoder(os.Stderr).Encode(r)
w.DeferedUpdate()
})
Expand All @@ -42,7 +43,7 @@ func main() {
}
}

func respondModal(w corde.ResponseWriter, r *corde.Request[corde.SlashCommandInteractionData]) {
func respondModal(ctx context.Context, w corde.ResponseWriter, r *corde.Interaction[corde.SlashCommandInteractionData]) {
w.Modal(corde.Modal{
Title: "xoxo",
CustomID: "pog-modal",
Expand Down
15 changes: 8 additions & 7 deletions 0_example/moderate-myself/commands_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"net/http"
"sync"
"testing"
Expand All @@ -16,7 +17,7 @@ func Test_list(t *testing.T) {
tests := []struct {
name string
mock owmock.ResponseWriterMock
interaction *corde.Request[corde.SlashCommandInteractionData]
interaction *corde.Interaction[corde.SlashCommandInteractionData]
}{
{
name: "list",
Expand All @@ -31,12 +32,12 @@ func Test_list(t *testing.T) {
}
},
},
interaction: &corde.Request[corde.SlashCommandInteractionData]{},
interaction: &corde.Interaction[corde.SlashCommandInteractionData]{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(_ *testing.T) {
list(tt.mock, tt.interaction)
list(context.Background(), tt.mock, tt.interaction)
})
}
}
Expand All @@ -46,8 +47,8 @@ func Test_btnNext(t *testing.T) {
tests := []struct {
name string
mock owmock.ResponseWriterMock
interaction *corde.Request[corde.ButtonInteractionData]
fn func(corde.ResponseWriter, *corde.Request[corde.ButtonInteractionData])
interaction *corde.Interaction[corde.ButtonInteractionData]
fn func(context.Context, corde.ResponseWriter, *corde.Interaction[corde.ButtonInteractionData])
}{
{
name: "btn next",
Expand All @@ -64,13 +65,13 @@ func Test_btnNext(t *testing.T) {
}
},
},
interaction: &corde.Request[corde.ButtonInteractionData]{},
interaction: &corde.Interaction[corde.ButtonInteractionData]{},
fn: btnNext(&corde.Mux{Client: http.DefaultClient}, corde.GuildOpt(0), &sync.Mutex{}, &selectedID),
},
}
for _, tt := range tests {
t.Run(tt.name, func(_ *testing.T) {
tt.fn(tt.mock, tt.interaction)
tt.fn(context.Background(), tt.mock, tt.interaction)
})
}
}
13 changes: 7 additions & 6 deletions 0_example/moderate-myself/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"log"
"os"
"sync"
Expand Down Expand Up @@ -68,8 +69,8 @@ var delBtn = corde.Component{
Emoji: &corde.Emoji{Name: "🗑️"},
}

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]) {
func list(m *corde.Mux, g func(*corde.CommandsOpt)) func(context.Context, corde.ResponseWriter, *corde.Interaction[corde.SlashCommandInteractionData]) {
return func(ctx context.Context, w corde.ResponseWriter, _ *corde.Interaction[corde.SlashCommandInteractionData]) {
w.Respond(corde.NewResp().
ActionRow(nextBtn).
Ephemeral().
Expand All @@ -78,8 +79,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.Request[corde.ButtonInteractionData]) {
return func(w corde.ResponseWriter, _ *corde.Request[corde.ButtonInteractionData]) {
func btnNext(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedID *int) func(context.Context, corde.ResponseWriter, *corde.Interaction[corde.ButtonInteractionData]) {
return func(ctx context.Context, w corde.ResponseWriter, _ *corde.Interaction[corde.ButtonInteractionData]) {
mu.Lock()
defer mu.Unlock()
commands, err := m.GetCommands(g)
Expand All @@ -102,8 +103,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.Request[corde.ButtonInteractionData]) {
return func(w corde.ResponseWriter, _ *corde.Request[corde.ButtonInteractionData]) {
func btnRemove(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedID *int) func(context.Context, corde.ResponseWriter, *corde.Interaction[corde.ButtonInteractionData]) {
return func(ctx context.Context, w corde.ResponseWriter, _ *corde.Interaction[corde.ButtonInteractionData]) {
mu.Lock()
defer mu.Unlock()
commands, err := m.GetCommands(g)
Expand Down
5 changes: 3 additions & 2 deletions 0_example/nft/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -49,7 +50,7 @@ func main() {
}
}

func NFTuser(w corde.ResponseWriter, i *corde.Request[corde.UserCommandInteractionData]) {
func NFTuser(ctx context.Context, w corde.ResponseWriter, i *corde.Interaction[corde.UserCommandInteractionData]) {
user := i.Data.Resolved.Users.First()
url := user.AvatarURL()

Expand All @@ -72,7 +73,7 @@ func NFTuser(w corde.ResponseWriter, i *corde.Request[corde.UserCommandInteracti
)
}

func NFTmessage(w corde.ResponseWriter, i *corde.Request[corde.MessageCommandInteractionData]) {
func NFTmessage(ctx context.Context, w corde.ResponseWriter, i *corde.Interaction[corde.MessageCommandInteractionData]) {
msg := i.Data.Resolved.Messages.First()
chanID := msg.ChannelID
msgID := msg.ID
Expand Down
9 changes: 5 additions & 4 deletions 0_example/todo/todo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"strings"
"sync"
Expand All @@ -20,7 +21,7 @@ type todoItem struct {
value string
}

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

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

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

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

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

Expand All @@ -80,7 +81,7 @@ func (t *todo) listHandler(w corde.ResponseWriter, _ *corde.Request[corde.SlashC
)
}

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

Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
devShell = pkgs.mkShell {
name = "corde";
packages = with pkgs; [
go_1_21
go_1_22
gofumpt
gotools
];
};
});
Expand Down
1 change: 0 additions & 1 deletion messages-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ func (m *Mux) CreateMessage(channelID Snowflake, data Message) (*Message, error)
msg := &Message{}
json.NewDecoder(resp.Body).Decode(msg)
return msg, nil

}
14 changes: 8 additions & 6 deletions mux-routes.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package corde

import "context"

// ButtonComponent mounts a button route on the mux
func (m *Mux) ButtonComponent(route string, handler func(ResponseWriter, *Request[ButtonInteractionData])) {
func (m *Mux) ButtonComponent(route string, handler func(context.Context, ResponseWriter, *Interaction[ButtonInteractionData])) {
m.Mount(ButtonInteraction, route, handler)
}

// Autocomplete mounts an autocomplete route on the mux
func (m *Mux) Autocomplete(route string, handler func(ResponseWriter, *Request[AutocompleteInteractionData])) {
func (m *Mux) Autocomplete(route string, handler func(context.Context, ResponseWriter, *Interaction[AutocompleteInteractionData])) {
m.Mount(AutocompleteInteraction, route, handler)
}

// SlashCommand mounts a slash command route on the mux
func (m *Mux) SlashCommand(route string, handler func(ResponseWriter, *Request[SlashCommandInteractionData])) {
func (m *Mux) SlashCommand(route string, handler func(context.Context, ResponseWriter, *Interaction[SlashCommandInteractionData])) {
m.Mount(SlashCommandInteraction, route, handler)
}

// UserCommand mounts a user command on the mux
func (m *Mux) UserCommand(route string, handler func(ResponseWriter, *Request[UserCommandInteractionData])) {
func (m *Mux) UserCommand(route string, handler func(context.Context, ResponseWriter, *Interaction[UserCommandInteractionData])) {
m.Mount(UserCommandInteraction, route, handler)
}

// MessageCommand mounts a message command on the mux
func (m *Mux) MessageCommand(route string, handler func(ResponseWriter, *Request[MessageCommandInteractionData])) {
func (m *Mux) MessageCommand(route string, handler func(context.Context, ResponseWriter, *Interaction[MessageCommandInteractionData])) {
m.Mount(MessageCommandInteraction, route, handler)
}

// Modal mounts a modal interaction response on the mux
func (m *Mux) Modal(route string, handler func(ResponseWriter, *Request[ModalInteractionData])) {
func (m *Mux) Modal(route string, handler func(context.Context, ResponseWriter, *Interaction[ModalInteractionData])) {
m.Mount(ModalInteraction, route, handler)
}
5 changes: 3 additions & 2 deletions mux.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package corde

import (
"context"
"fmt"
"log"
"net/http"
Expand All @@ -19,7 +20,7 @@ type Mux struct {
routes *radix.Tree[Handlers]
PublicKey string // the hex public key provided by discord
BasePath string // base route path, default is "/"
OnNotFound func(ResponseWriter, *Request[JsonRaw])
OnNotFound func(context.Context, ResponseWriter, *Interaction[JsonRaw])
Client *http.Client
AppID Snowflake
BotToken string
Expand Down Expand Up @@ -47,7 +48,7 @@ func NewMux(publicKey string, appID Snowflake, botToken string) *Mux {
routes: radix.New[Handlers](),
PublicKey: publicKey,
BasePath: "/",
OnNotFound: func(_ ResponseWriter, i *Request[JsonRaw]) {
OnNotFound: func(_ context.Context, _ ResponseWriter, i *Interaction[JsonRaw]) {
log.Printf("No handler for registered command: %s\n", i.Route)
},
Client: &http.Client{
Expand Down
Loading

0 comments on commit ace7ba9

Please sign in to comment.