-
-
Notifications
You must be signed in to change notification settings - Fork 723
/
DisconnectEvent.java
111 lines (102 loc) · 3.75 KB
/
DisconnectEvent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.api.events;
import com.neovisionaries.ws.client.WebSocketFrame;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.requests.CloseCode;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.OffsetDateTime;
/**
* Indicates that JDA has been disconnected from the remote server.
* <br>When this event is fired JDA will try to reconnect if possible
* unless {@link net.dv8tion.jda.api.JDABuilder#setAutoReconnect(boolean) JDABuilder.setAutoReconnect(Boolean)}
* has been provided {@code false} or the disconnect was too fatal in which case a {@link ShutdownEvent} is fired.
*
* <p>When reconnecting was successful either a {@link net.dv8tion.jda.api.events.ReconnectedEvent ReconnectEvent}
* <b>or</b> {@link net.dv8tion.jda.api.events.ResumedEvent ResumedEvent} is fired.
*/
public class DisconnectEvent extends Event
{
protected final WebSocketFrame serverCloseFrame;
protected final WebSocketFrame clientCloseFrame;
protected final boolean closedByServer;
protected final OffsetDateTime disconnectTime;
public DisconnectEvent(
@Nonnull JDA api,
@Nullable WebSocketFrame serverCloseFrame, @Nullable WebSocketFrame clientCloseFrame,
boolean closedByServer, @Nonnull OffsetDateTime disconnectTime)
{
super(api);
this.serverCloseFrame = serverCloseFrame;
this.clientCloseFrame = clientCloseFrame;
this.closedByServer = closedByServer;
this.disconnectTime = disconnectTime;
}
/**
* Possibly-null {@link net.dv8tion.jda.api.requests.CloseCode CloseCode}
* representing the meaning for this DisconnectEvent
*
* <p><b>This is {@code null} if this disconnect did either not happen because the Service closed the session
* (see {@link #isClosedByServer()}) or if there is no mapped CloseCode enum constant for the service close code!</b>
*
* @return Possibly-null {@link net.dv8tion.jda.api.requests.CloseCode CloseCode}
*/
@Nullable
public CloseCode getCloseCode()
{
return serverCloseFrame != null ? CloseCode.from(serverCloseFrame.getCloseCode()) : null;
}
/**
* The close frame discord sent to us
*
* @return The {@link com.neovisionaries.ws.client.WebSocketFrame WebSocketFrame} discord sent as closing handshake
*/
@Nullable
public WebSocketFrame getServiceCloseFrame()
{
return serverCloseFrame;
}
/**
* The close frame we sent to discord
*
* @return The {@link com.neovisionaries.ws.client.WebSocketFrame WebSocketFrame} we sent as closing handshake
*/
@Nullable
public WebSocketFrame getClientCloseFrame()
{
return clientCloseFrame;
}
/**
* Whether the connection was closed by discord
*
* @return True, if discord closed our connection
*/
public boolean isClosedByServer()
{
return closedByServer;
}
/**
* Time at which we noticed the disconnection
*
* @return Time of closure
*/
@Nonnull
public OffsetDateTime getTimeDisconnected()
{
return disconnectTime;
}
}