Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback function for every event before it is json decoded #308

Open
hi117 opened this issue Feb 6, 2022 · 1 comment
Open

Callback function for every event before it is json decoded #308

hi117 opened this issue Feb 6, 2022 · 1 comment

Comments

@hi117
Copy link
Contributor

hi117 commented Feb 6, 2022

I'm wanting to migrate from discordgo to arikawa, and one of the features it had was hooking on something like this: func(_ *discordgo.Session, m *discordgo.Event). Is there an equilavent in Arikawa? I tried looking but there didn't seem to be a way to hook on every event before it was decoded. This is how discordgo handled the struct by the way:

type Event struct {
	Operation int             `json:"op"`
	Sequence  int64           `json:"s"`
	Type      string          `json:"t"`
	RawData   json.RawMessage `json:"d"`
	// Struct contains one of the other types in this file.
	Struct interface{} `json:"-"`
}
@diamondburned
Copy link
Owner

There is no straightforward way to hook on every event before it's decoded, but there are some roundabout ways to achieve this:

  • You can probably swap out gateway.OpUnmarshalers with something that is a copy of the existing OpUnmarshalers with all functions wrapped, or
  • You can create a wrapper around ws.Conn which will give you control over the <-chan ws.Op channel.

It might be worth it to also route every single ws.Op value through the handler, but that will introduce a lot more work for a case that not a lot of people will care about:

--- a/utils/ws/ophandler/ophandler.go
+++ b/utils/ws/ophandler/ophandler.go
@@ -17,6 +17,7 @@ func Loop(src <-chan ws.Op, dst *handler.Handler) <-chan struct{} {
        go func() {
                for op := range src {
                        dst.Call(op.Data)
+                       dst.Call(op)
                }
                close(done)
        }()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants