/
VoiceDispatchInterceptor.java
259 lines (234 loc) · 6.7 KB
/
VoiceDispatchInterceptor.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* 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.hooks;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.GuildVoiceState;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.managers.DirectAudioController;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.api.utils.data.SerializableData;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Interceptor used to handle critical voice dispatches.
* <br>This will make it impossible to connect to voice channels with
* the built-in {@link net.dv8tion.jda.api.managers.AudioManager AudioManager}.
* It is expected that the user has some other means of establishing voice connections when this is used.
*
* @since 4.0.0
*/
public interface VoiceDispatchInterceptor
{
/**
* Handles the <b>VOICE_SERVER_UPDATE</b>.
*
* @param update
* The {@link VoiceServerUpdate} to handle
*/
void onVoiceServerUpdate(@Nonnull VoiceServerUpdate update);
/**
* Handles the <b>VOICE_STATE_UPDATE</b>.
* <br>This indicates the user might have moved to a new voice channel.
*
* @param update
* The {@link VoiceStateUpdate} to handle
*
* @return True, if a connection was previously established
*/
boolean onVoiceStateUpdate(@Nonnull VoiceStateUpdate update);
/**
* Abstraction for all relevant voice updates
*
* @see VoiceServerUpdate
* @see VoiceStateUpdate
*/
interface VoiceUpdate extends SerializableData
{
/**
* The {@link Guild} for this update
*
* @return The guild
*/
@Nonnull
Guild getGuild();
/**
* The raw JSON object that was parsed from this update
*
* @return The raw JSON object
*/
@Nonnull
@Override
DataObject toData();
/**
* Shortcut to access the audio controller of this JDA instance
*
* @return The {@link DirectAudioController} for this JDA instance
*/
@Nonnull
default DirectAudioController getAudioController()
{
return getJDA().getDirectAudioController();
}
/**
* Shortcut to access the guild id
*
* @return The guild id
*/
default long getGuildIdLong()
{
return getGuild().getIdLong();
}
/**
* Shortcut to access the guild id
*
* @return The guild id
*/
@Nonnull
default String getGuildId()
{
return Long.toUnsignedString(getGuildIdLong());
}
/**
* Shortcut to access the JDA instance
*
* @return The JDA instance
*/
@Nonnull
default JDA getJDA()
{
return getGuild().getJDA();
}
/**
* Shortcut to access the shard info for this JDA instance
*
* @return The shard information, or null if this was not for a sharded client
*/
@Nullable
default JDA.ShardInfo getShardInfo()
{
return getJDA().getShardInfo();
}
}
/**
* Wrapper for a <a href="https://discord.com/developers/docs/topics/gateway#voice-server-update" target="_blank">Voice Server Update</a>
*/
class VoiceServerUpdate implements VoiceUpdate
{
private final Guild guild;
private final String endpoint;
private final String token;
private final String sessionId;
private final DataObject json;
public VoiceServerUpdate(Guild guild, String endpoint, String token, String sessionId, DataObject json)
{
this.guild = guild;
this.endpoint = endpoint;
this.token = token;
this.sessionId = sessionId;
this.json = json;
}
@Nonnull
@Override
public Guild getGuild()
{
return guild;
}
@Nonnull
@Override
public DataObject toData()
{
return json;
}
/**
* The voice server endpoint
*
* @return The endpoint
*/
@Nonnull
public String getEndpoint()
{
return endpoint;
}
/**
* The access token for the voice server connection
*
* @return The access token
*/
@Nonnull
public String getToken()
{
return token;
}
/**
* The session id for the voice server session
*
* @return The session id
*/
@Nonnull
public String getSessionId()
{
return sessionId;
}
}
/**
* Wrapper for a <a href="https://discord.com/developers/docs/topics/gateway#voice-state-update" target="_blank">Voice State Update</a>
*/
class VoiceStateUpdate implements VoiceUpdate
{
private final VoiceChannel channel;
private final GuildVoiceState voiceState;
private final DataObject json;
public VoiceStateUpdate(VoiceChannel channel, GuildVoiceState voiceState, DataObject json)
{
this.channel = channel;
this.voiceState = voiceState;
this.json = json;
}
@Nonnull
@Override
public Guild getGuild()
{
return voiceState.getGuild();
}
@Nonnull
@Override
public DataObject toData()
{
return json;
}
/**
* The update voice channel
*
* @return The updated voice channel, or null to signal disconnect
*/
@Nullable
public VoiceChannel getChannel()
{
return channel;
}
/**
* The voice state for the guild
*
* @return The voice state
*/
@Nonnull
public GuildVoiceState getVoiceState()
{
return voiceState;
}
}
}