mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-12-04 04:42:48 +00:00
Compare commits
3 commits
0f0c968634
...
10589eab14
Author | SHA1 | Date | |
---|---|---|---|
10589eab14 | |||
91b2c6c840 | |||
75ff7342b1 |
|
@ -44,6 +44,15 @@ type InteractionHandler interface {
|
||||||
HandleInteraction(*discord.InteractionEvent) *api.InteractionResponse
|
HandleInteraction(*discord.InteractionEvent) *api.InteractionResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InteractionHandlerFunc is a function type that implements the interface.
|
||||||
|
type InteractionHandlerFunc func(*discord.InteractionEvent) *api.InteractionResponse
|
||||||
|
|
||||||
|
var _ InteractionHandler = InteractionHandlerFunc(nil)
|
||||||
|
|
||||||
|
func (f InteractionHandlerFunc) HandleInteraction(ev *discord.InteractionEvent) *api.InteractionResponse {
|
||||||
|
return f(ev)
|
||||||
|
}
|
||||||
|
|
||||||
type alwaysDeferInteraction struct {
|
type alwaysDeferInteraction struct {
|
||||||
f func(*discord.InteractionEvent)
|
f func(*discord.InteractionEvent)
|
||||||
flags discord.MessageFlags
|
flags discord.MessageFlags
|
||||||
|
|
|
@ -26,6 +26,11 @@ var (
|
||||||
Encoding = "json"
|
Encoding = "json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// deadbeatDuration is the duration that limits whether the gateway should
|
||||||
|
// resume or restart entirely. If it's less than this duration, then it's deemed
|
||||||
|
// resumable.
|
||||||
|
const deadbeatDuration = 15 * time.Minute
|
||||||
|
|
||||||
// CodeInvalidSequence is the code returned by Discord to signal that the given
|
// CodeInvalidSequence is the code returned by Discord to signal that the given
|
||||||
// sequence number is invalid.
|
// sequence number is invalid.
|
||||||
const CodeInvalidSequence = 4007
|
const CodeInvalidSequence = 4007
|
||||||
|
@ -345,8 +350,11 @@ func (g *gatewayImpl) OnOp(ctx context.Context, op ws.Op) bool {
|
||||||
g.gateway.QueueReconnect()
|
g.gateway.QueueReconnect()
|
||||||
|
|
||||||
case *HelloEvent:
|
case *HelloEvent:
|
||||||
// Reset gateway times.
|
|
||||||
g.beatMutex.Lock()
|
g.beatMutex.Lock()
|
||||||
|
// Determine that we shouldn't reconnect if the last time we've received
|
||||||
|
// a heart beat was over (deadbeatDuration) ago.
|
||||||
|
resumable := g.echoBeat.IsZero() || time.Since(g.echoBeat) < deadbeatDuration
|
||||||
|
// Reset gateway times.
|
||||||
g.echoBeat = time.Time{}
|
g.echoBeat = time.Time{}
|
||||||
g.sentBeat = time.Time{}
|
g.sentBeat = time.Time{}
|
||||||
g.beatMutex.Unlock()
|
g.beatMutex.Unlock()
|
||||||
|
@ -356,7 +364,7 @@ func (g *gatewayImpl) OnOp(ctx context.Context, op ws.Op) bool {
|
||||||
|
|
||||||
// Send Discord either the Identify packet (if it's a fresh
|
// Send Discord either the Identify packet (if it's a fresh
|
||||||
// connection), or a Resume packet (if it's a dead connection).
|
// connection), or a Resume packet (if it's a dead connection).
|
||||||
if g.state.SessionID == "" || g.state.Sequence == 0 {
|
if !resumable || g.state.SessionID == "" || g.state.Sequence == 0 {
|
||||||
// SessionID is empty, so this is a completely new session.
|
// SessionID is empty, so this is a completely new session.
|
||||||
if err := g.sendIdentify(ctx); err != nil {
|
if err := g.sendIdentify(ctx); err != nil {
|
||||||
g.gateway.SendErrorWrap(err, "failed to send identify")
|
g.gateway.SendErrorWrap(err, "failed to send identify")
|
||||||
|
|
|
@ -309,21 +309,21 @@ func (s *Session) WithContext(ctx context.Context) *Session {
|
||||||
// AddInteractionHandler adds an interaction handler function to be handled with
|
// AddInteractionHandler adds an interaction handler function to be handled with
|
||||||
// the gateway and the API client. Use this as a compatibility layer for bots
|
// the gateway and the API client. Use this as a compatibility layer for bots
|
||||||
// that support both methods of hosting.
|
// that support both methods of hosting.
|
||||||
func (s *Session) AddInteractionHandler(f webhook.InteractionHandler) {
|
func (s *Session) AddInteractionHandler(h webhook.InteractionHandler) {
|
||||||
// State doesn't override this, but it doesn't touch
|
// State doesn't override this, but it doesn't touch
|
||||||
// InteractionCreateEvents, so it shouldn't need to.
|
// InteractionCreateEvents, so it shouldn't need to.
|
||||||
AddInteractionHandler(s.Handler, s.Client, f)
|
s.AddHandler(func(ev *gateway.InteractionCreateEvent) {
|
||||||
}
|
if resp := h.HandleInteraction(&ev.InteractionEvent); resp != nil {
|
||||||
|
s.RespondInteraction(ev.ID, ev.Token, *resp)
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddInteractionHandlerFunc is a function variant of AddInteractionHandler.
|
||||||
|
func (s *Session) AddInteractionHandlerFunc(f webhook.InteractionHandlerFunc) {
|
||||||
|
s.AddInteractionHandler(f)
|
||||||
|
}
|
||||||
|
|
||||||
// Close closes the underlying Websocket connection, invalidating the session
|
// Close closes the underlying Websocket connection, invalidating the session
|
||||||
// ID. It will send a closing frame before ending the connection, closing it
|
// 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
|
// gracefully. This will cause the bot to appear as offline instantly. To
|
||||||
|
|
Loading…
Reference in a new issue