1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-07-26 23:40:57 +00:00

Compare commits

..

No commits in common. "10589eab148d1c124910038cb2e7dafede51aaec" and "0f0c9686348aeda8e25cb715be7c05292cfa5455" have entirely different histories.

3 changed files with 11 additions and 28 deletions

View file

@ -44,15 +44,6 @@ type InteractionHandler interface {
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 {
f func(*discord.InteractionEvent)
flags discord.MessageFlags

View file

@ -26,11 +26,6 @@ var (
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
// sequence number is invalid.
const CodeInvalidSequence = 4007
@ -350,11 +345,8 @@ func (g *gatewayImpl) OnOp(ctx context.Context, op ws.Op) bool {
g.gateway.QueueReconnect()
case *HelloEvent:
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.beatMutex.Lock()
g.echoBeat = time.Time{}
g.sentBeat = time.Time{}
g.beatMutex.Unlock()
@ -364,7 +356,7 @@ func (g *gatewayImpl) OnOp(ctx context.Context, op ws.Op) bool {
// Send Discord either the Identify packet (if it's a fresh
// connection), or a Resume packet (if it's a dead connection).
if !resumable || g.state.SessionID == "" || g.state.Sequence == 0 {
if g.state.SessionID == "" || g.state.Sequence == 0 {
// SessionID is empty, so this is a completely new session.
if err := g.sendIdentify(ctx); err != nil {
g.gateway.SendErrorWrap(err, "failed to send identify")

View file

@ -309,19 +309,19 @@ func (s *Session) WithContext(ctx context.Context) *Session {
// 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(h webhook.InteractionHandler) {
func (s *Session) AddInteractionHandler(f webhook.InteractionHandler) {
// State doesn't override this, but it doesn't touch
// InteractionCreateEvents, so it shouldn't need to.
s.AddHandler(func(ev *gateway.InteractionCreateEvent) {
if resp := h.HandleInteraction(&ev.InteractionEvent); resp != nil {
s.RespondInteraction(ev.ID, ev.Token, *resp)
}
})
AddInteractionHandler(s.Handler, s.Client, f)
}
// AddInteractionHandlerFunc is a function variant of AddInteractionHandler.
func (s *Session) AddInteractionHandlerFunc(f webhook.InteractionHandlerFunc) {
s.AddInteractionHandler(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