diff --git a/session/session.go b/session/session.go index ff0fc31..1be7137 100644 --- a/session/session.go +++ b/session/session.go @@ -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 diff --git a/state/state.go b/state/state.go index 49dae53..a4c9555 100644 --- a/state/state.go +++ b/state/state.go @@ -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.