1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-03 12:23:02 +00:00

Compare commits

...

3 commits

Author SHA1 Message Date
diamondburned 10589eab14
gateway: Do not resume after 15m
Fixes #341.

Unsure if this is a good idea, but 15 minutes seems enough.
2022-09-13 19:50:42 -07:00
diamondburned 91b2c6c840
session: Add AddInteractionHandlerFunc
Also remove session.AddInteractionHandler (exported func).
2022-08-23 13:46:42 -07:00
diamondburned 75ff7342b1
api: Add InteractionHandlerFunc 2022-08-23 13:42:29 -07:00
3 changed files with 28 additions and 11 deletions

View file

@ -44,6 +44,15 @@ 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,6 +26,11 @@ 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
@ -345,8 +350,11 @@ func (g *gatewayImpl) OnOp(ctx context.Context, op ws.Op) bool {
g.gateway.QueueReconnect()
case *HelloEvent:
// Reset gateway times.
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.sentBeat = time.Time{}
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
// 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.
if err := g.sendIdentify(ctx); err != nil {
g.gateway.SendErrorWrap(err, "failed to send identify")

View file

@ -309,21 +309,21 @@ 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(f webhook.InteractionHandler) {
func (s *Session) AddInteractionHandler(h 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)
s.AddHandler(func(ev *gateway.InteractionCreateEvent) {
if resp := h.HandleInteraction(&ev.InteractionEvent); resp != nil {
s.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
// 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