-
-
Notifications
You must be signed in to change notification settings - Fork 730
/
VoiceStateUpdateHandler.java
273 lines (249 loc) · 11.8 KB
/
VoiceStateUpdateHandler.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright 2015-2016 Austin Keener & Michael Ritter
*
* 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.core.handle;
import net.dv8tion.jda.client.entities.*;
import net.dv8tion.jda.client.entities.impl.CallImpl;
import net.dv8tion.jda.client.entities.impl.CallVoiceStateImpl;
import net.dv8tion.jda.client.entities.impl.JDAClientImpl;
import net.dv8tion.jda.client.events.call.voice.CallVoiceJoinEvent;
import net.dv8tion.jda.client.events.call.voice.CallVoiceLeaveEvent;
import net.dv8tion.jda.client.events.call.voice.CallVoiceSelfDeafenEvent;
import net.dv8tion.jda.client.events.call.voice.CallVoiceSelfMuteEvent;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.impl.GuildVoiceStateImpl;
import net.dv8tion.jda.core.entities.impl.JDAImpl;
import net.dv8tion.jda.core.entities.impl.MemberImpl;
import net.dv8tion.jda.core.entities.impl.VoiceChannelImpl;
import net.dv8tion.jda.core.events.guild.voice.*;
import net.dv8tion.jda.core.requests.GuildLock;
import net.dv8tion.jda.core.requests.WebSocketClient;
import org.json.JSONObject;
import java.util.Objects;
public class VoiceStateUpdateHandler extends SocketHandler
{
public VoiceStateUpdateHandler(JDAImpl api)
{
super(api);
}
@Override
protected String handleInternally(JSONObject content)
{
String guildId = content.has("guild_id") ? content.getString("guild_id") : null;
if (guildId != null && GuildLock.get(api).isLocked(guildId))
{
return guildId;
}
if (guildId != null)
{
handleGuildVoiceState(content);
}
else
{
handleCallVoiceState(content);
}
return null;
}
private void handleGuildVoiceState(JSONObject content)
{
String userId = content.getString("user_id");
String guildId = content.getString("guild_id");
String channelId = !content.isNull("channel_id ") ? content.getString("channel_id") : null;
String sessionId = !content.isNull("session_id") ? content.getString("session_id") : null;
boolean selfMuted = content.getBoolean("self_mute");
boolean selfDeafened = content.getBoolean("self_deaf");
boolean guildMuted = content.getBoolean("mute");
boolean guildDeafened = content.getBoolean("deaf");
boolean suppressed = content.getBoolean("suppress");
Guild guild = api.getGuildById(guildId);
if (guild == null)
{
EventCache.get(api).cache(EventCache.Type.GUILD, content.getString("guild_id"), () ->
{
handle(responseNumber, allContent);
});
EventCache.LOG.debug("Received a VOICE_STATE_UPDATE for a Guild that has yet to be cached. JSON: " + content);
return;
}
VoiceChannelImpl channel = (VoiceChannelImpl) guild.getVoiceChannelById(channelId);
if (channel == null && channelId != null)
{
EventCache.get(api).cache(EventCache.Type.CHANNEL, channelId, () ->
{
handle(responseNumber, allContent);
});
EventCache.LOG.debug("Received VOICE_STATE_UPDATE for a VoiceChannel that has yet to be cached. JSON: " + content);
return;
}
MemberImpl member = (MemberImpl) guild.getMemberById(userId);
if (member == null)
{
//Caching of this might not be valid. It is possible that we received this
// update due to this Member leaving the guild while still connected to a voice channel.
// In that case, we should not cache this because it could cause problems if they rejoined.
//However, we can't just ignore it completely because it could be a user that joined off of
// an invite to a VoiceChannel, so the GUILD_MEMBER_ADD and the VOICE_STATE_UPDATE may have
// come out of order. Not quite sure what to do. Going to cache for now however.
//At the worst, this will just cause a few events to fire with bad data if the member rejoins the guild if
// in fact the issue was that the VOICE_STATE_UPDATE was sent after they had left, however, by caching
// it we will preserve the integrity of the cache in the event that it was actually a mis-ordering of
// GUILD_MEMBER_ADD and VOICE_STATE_UPDATE. I'll take some bad-data events over an invalid cache.
EventCache.get(api).cache(EventCache.Type.USER, userId, () ->
{
handle(responseNumber, allContent);
});
EventCache.LOG.debug("Received VOICE_STATE_UPDATE for a Member that has yet to be cached. JSON: " + content);
return;
}
GuildVoiceStateImpl vState = (GuildVoiceStateImpl) member.getVoiceState();
vState.setSessionId(sessionId); //Cant really see a reason for an event for this
if (!Objects.equals(channel, vState.getChannel()))
{
VoiceChannelImpl oldChannel = (VoiceChannelImpl) vState.getChannel();
vState.setConnectedChannel(channel);
if (oldChannel == null)
{
channel.getConnectedMembersMap().put(userId, member);
api.getEventManager().handle(
new GuildVoiceJoinEvent(
api, responseNumber,
member));
}
else if (channel == null)
{
oldChannel.getConnectedMembersMap().remove(userId);
api.getEventManager().handle(
new GuildVoiceLeaveEvent(
api, responseNumber,
member, oldChannel));
}
else
{
channel.getConnectedMembersMap().put(userId, member);
oldChannel.getConnectedMembersMap().remove(userId);
api.getEventManager().handle(
new GuildVoiceMoveEvent(
api, responseNumber,
member, oldChannel));
}
}
if (selfMuted != vState.isSelfMuted())
{
vState.setSelfMuted(selfMuted);
api.getEventManager().handle(new GuildVoiceSelfMuteEvent(api, responseNumber, member));
}
if (selfDeafened != vState.isSelfDeafened())
{
vState.setSelfDeafened(selfDeafened);
api.getEventManager().handle(new GuildVoiceSelfDeafenEvent(api, responseNumber, member));
}
if (guildMuted != vState.isGuildMuted())
{
vState.setGuildMuted(guildMuted);
api.getEventManager().handle(new GuildVoiceGuildMuteEvent(api, responseNumber, member));
}
if (guildDeafened != vState.isGuildDeafened())
{
vState.setGuildDeafened(guildDeafened);
api.getEventManager().handle(new GuildVoiceGuildDeafenEvent(api, responseNumber, member));
}
if (suppressed != vState.isSuppressed())
{
vState.setSuppressed(suppressed);
api.getEventManager().handle(new GuildVoiceSuppressEvent(api, responseNumber, member));
}
}
private void handleCallVoiceState(JSONObject content)
{
String userId = content.getString("user_id");
String channelId = !content.isNull("channel_id") ? content.getString("channel_id") : null;
String sessionId = !content.isNull("session_id") ? content.getString("session_id") : null;
boolean selfMuted = content.getBoolean("self_mute");
boolean selfDeafened = content.getBoolean("self_deaf");
//Joining a call
CallableChannel channel;
CallVoiceStateImpl vState;
if (channelId != null)
{
channel = api.asClient().getGroupById(channelId);
if (channel == null)
channel = api.getPrivateChannelMap().get(channelId);
if (channel == null)
{
EventCache.get(api).cache(EventCache.Type.CHANNEL, channelId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a VOICE_STATE_UPDATE for a Group/PrivateChannel that was not yet cached! JSON: " + content);
return;
}
CallImpl call = (CallImpl) channel.getCurrentCall();
if (call == null)
{
EventCache.get(api).cache(EventCache.Type.CALL, channelId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a VOICE_STATE_UPDATE for a Call that is not yet cached. JSON: " + content);
return;
}
CallUser cUser = ((JDAClientImpl) api.asClient()).getCallUserMap().get(userId);
if (cUser != null && !channelId.equals(cUser.getCall().getCallableChannel().getId()))
{
WebSocketClient.LOG.fatal("Received a VOICE_STATE_UPDATE for a user joining a call, but the user was already in a different call! Big error! JSON: " + content);
((CallVoiceStateImpl) cUser.getVoiceState()).setInCall(false);
}
cUser = call.getCallUserMap().get(userId);
if (cUser == null)
{
EventCache.get(api).cache(EventCache.Type.USER, userId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a VOICE_STATE_UPDATE for a user that is not yet a a cached CallUser for the call. (groups only). JSON: " + content);
return;
}
((JDAClientImpl) api.asClient()).getCallUserMap().put(userId, cUser);
vState = (CallVoiceStateImpl) cUser.getVoiceState();
vState.setSessionId(sessionId);
vState.setInCall(true);
api.getEventManager().handle(
new CallVoiceJoinEvent(
api, responseNumber,
cUser));
}
else //Leaving a call
{
CallUser cUser = ((JDAClientImpl) api.asClient()).getCallUserMap().remove(userId);
if (cUser == null)
{
EventCache.get(api).cache(EventCache.Type.USER, userId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a VOICE_STATE_UPDATE for a User leaving a Call, but the Call was not yet cached! JSON: " + content);
return;
}
Call call = cUser.getCall();
channel = call.getCallableChannel();
vState = (CallVoiceStateImpl) cUser.getVoiceState();
vState.setSessionId(sessionId);
vState.setInCall(false);
api.getEventManager().handle(
new CallVoiceLeaveEvent(
api, responseNumber,
cUser));
}
//Now that we're done dealing with the joins and leaves, we can deal with the mute/deaf changes.
if (selfMuted != vState.isSelfMuted())
{
vState.setSelfMuted(selfMuted);
api.getEventManager().handle(new CallVoiceSelfMuteEvent(api, responseNumber, vState.getCallUser()));
}
if (selfDeafened != vState.isSelfDeafened())
{
vState.setSelfDeafened(selfDeafened);
api.getEventManager().handle(new CallVoiceSelfDeafenEvent(api, responseNumber, vState.getCallUser()));
}
}
}