mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-01-05 19:57:02 +00:00
webhook: Add session+state helpers
This commit is contained in:
parent
17c26bf488
commit
f00054bddf
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/diamondburned/arikawa/v3/api"
|
||||
"github.com/diamondburned/arikawa/v3/api/webhook"
|
||||
"github.com/diamondburned/arikawa/v3/gateway"
|
||||
"github.com/diamondburned/arikawa/v3/utils/handler"
|
||||
"github.com/diamondburned/arikawa/v3/utils/json/option"
|
||||
|
@ -305,6 +306,24 @@ func (s *Session) WithContext(ctx context.Context) *Session {
|
|||
return &cpy
|
||||
}
|
||||
|
||||
// AddInteractionHandler adds an interaction handler function to be handled with
|
||||
// the gateway and the API client. Use this as a compatibility layer for bots
|
||||
// that support both methods of hosting.
|
||||
func (s *Session) AddInteractionHandler(f webhook.InteractionHandler) {
|
||||
// State doesn't override this, but it doesn't touch
|
||||
// InteractionCreateEvents, so it shouldn't need to.
|
||||
AddInteractionHandler(s.Handler, s.Client, f)
|
||||
}
|
||||
|
||||
// AddInteractionHandler is used by (*Session).AddInteractionHandler.
|
||||
func AddInteractionHandler(h *handler.Handler, c *api.Client, f webhook.InteractionHandler) {
|
||||
h.AddHandler(func(ev *gateway.InteractionCreateEvent) {
|
||||
if resp := f.HandleInteraction(&ev.InteractionEvent); resp != nil {
|
||||
c.RespondInteraction(ev.ID, ev.Token, *resp)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Close closes the underlying Websocket connection, invalidating the session
|
||||
// ID. It will send a closing frame before ending the connection, closing it
|
||||
// gracefully. This will cause the bot to appear as offline instantly. To
|
||||
|
|
|
@ -153,6 +153,20 @@ func NewFromSession(s *session.Session, cabinet *store.Cabinet) *State {
|
|||
return state
|
||||
}
|
||||
|
||||
// NewAPIOnlyState creates a new State that only has API functions and no
|
||||
// gateway (or state caches). Use this as a drop-in for InteractionServer usage.
|
||||
//
|
||||
// This function may work for most use cases; however, it will not work for all
|
||||
// use cases. For example, bots that need the
|
||||
func NewAPIOnlyState(token string, h *handler.Handler) *State {
|
||||
return &State{
|
||||
Session: session.NewCustom(gateway.DefaultIdentifier(token), api.NewClient(token), h),
|
||||
Handler: h,
|
||||
Cabinet: store.NoopCabinet,
|
||||
StateLog: func(err error) {},
|
||||
}
|
||||
}
|
||||
|
||||
// WithContext returns a shallow copy of State with the context replaced in the
|
||||
// API client. All methods called on the State will use this given context. This
|
||||
// method is thread-safe.
|
||||
|
|
Loading…
Reference in a new issue