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.
func (ctx *Context) Call(event interface{}) error {
return ctx.callCmd(event)

View File

@ -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()
}