#Disconnect event
1 messages · Page 1 of 1 (latest)
You can use the Event::GatewayClose variant to know when a Shard disconnects: https://docs.rs/twilight-model/0.15.0/twilight_model/gateway/event/enum.Event.html#variant.GatewayClose
~~Hi. This is more boilerplate-y than before (sorry!) and something we should document to make easier, but essentially what you want to do is switch to receiving WebSocket messages. This enum contains a Text variant containing the received JSON payload, and a Close variant1 with details about why the connection closed. this can be used as your point of disconnect. with the Text variant, you would use twilight_gateway::parse2 to parse it into a Gateway event. I've made this into a gist[3].
[3]: https://gist.github.com/zeylahellyer/371a264e0c8a0006dfd0484d1db90283~~
Hhmmm that seems a bit more boilerplate-y yeah 
I'll look more into it how I can use that!
Setup is a bit harder with the new gateway :/ but it should allow anyone to do pretty much anything they need to do at this point, since low-level concepts such as websocket messages are now public. an unfortunate trade-off that we need to iterate on and refine for these types of cases
Probably worth it for most people I guess? 
I barely use the gateway for anything atm, mostly using http interactions.
great question
i dont know
i'll check
oh yes
you can just use Event::GatewayClose
my bad
we really do need to document how to process disconnects
Ooohh great. 
Wasn't sure of it 
thanks for finding that, I'll make a github issue for documenting that Shard::next_event can return Event::GatewayClose if it disconnects
Thanks!
❤️
Should've included this in the migration guide too
Also note that disconnecting from the gateway (bot show as offline) does not disconnect you immediately from the underlying TCP or websocket connection. ConnectionStatus::Disconnected is a better way of figuring out that you're disconnected from the gateway than waiting for Messag::Close/Event::GatewayClose
For example, the shard will send a close message upon receiving Event::GatewayReconnect (which immediately disconnects it from the gateway) but the WebSocket and TCP connections will only be terminated upon receiving a confirmation close message from discord (which is returned to the user through Message::Close). That is, you're not allowed to send gateway commands after the shard sends a close message, but it may take a while for it to receive a WebSocket close message reply from discord as it'll process messages in order (FIFO). There may be hundreds of already received, but unprocessed, messages in the queue
Of course, this knowledge should be publicly documented and I'll get on it soon.
Part of the problem is that the terminology is quite weird. What I call the gateway is the protocol above the WebSocket implementation (Discord's gateway events, the one's encoded as {op: {}, t: {}, s: {}, d: {}}).
