1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-27 09:12:53 +00:00

Bot: Close gracefully by default

This commit is contained in:
diamondburned 2021-01-29 23:25:15 -08:00
parent a969b11709
commit ac2f3ba68a
2 changed files with 21 additions and 0 deletions

View file

@ -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. // Call should only be used if you know what you're doing.
func (ctx *Context) Call(event interface{}) error { func (ctx *Context) Call(event interface{}) error {
return ctx.callCmd(event) return ctx.callCmd(event)

View file

@ -135,9 +135,24 @@ func (s *Session) WithContext(ctx context.Context) *Session {
return &cpy return &cpy
} }
// Close closes the gateway. The connection is still resumable with the given
// session ID.
func (s *Session) Close() error { 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 // Stop the event handler
s.looper.Stop() s.looper.Stop()
// Close the websocket // Close the websocket
if gracefully {
return s.Gateway.CloseGracefully()
}
return s.Gateway.Close() return s.Gateway.Close()
} }