diff --git a/bot/ctx.go b/bot/ctx.go index a3b1cae..dc4415c 100644 --- a/bot/ctx.go +++ b/bot/ctx.go @@ -394,6 +394,12 @@ func (ctx *Context) Start() func() { }) } +// Close closes the gateway gracefully. Bots that need to preserve the session +// ID after closing should NOT use this method. +func (ctx *Context) Close() error { + return ctx.Session.CloseGracefully() +} + // Call should only be used if you know what you're doing. func (ctx *Context) Call(event interface{}) error { return ctx.callCmd(event) diff --git a/session/session.go b/session/session.go index c0d94ad..6473404 100644 --- a/session/session.go +++ b/session/session.go @@ -135,9 +135,24 @@ func (s *Session) WithContext(ctx context.Context) *Session { return &cpy } +// Close closes the gateway. The connection is still resumable with the given +// session ID. func (s *Session) Close() error { + return s.close(false) +} + +// CloseGracefully permanently closes the gateway. The session ID is invalidated +// afterwards. +func (s *Session) CloseGracefully() error { + return s.close(true) +} + +func (s *Session) close(gracefully bool) error { // Stop the event handler s.looper.Stop() // Close the websocket + if gracefully { + return s.Gateway.CloseGracefully() + } return s.Gateway.Close() }