Skip to content

Commit 1bee560

Browse files
committed
fix(voice): fixed disconnects not actually removing the bot from the channel
This is because the main gateway, not the voice gateway, wasn't being informed of the leaving with a voice state update payload. This is unfortuantley not very well documented in the Discord API docs...
1 parent ff45371 commit 1bee560

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

src/main/java/com/seailz/discordjar/gateway/Gateway.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,19 @@ public void sendVoicePayload(String guildId, String channelId, boolean selfMute,
482482
queueMessage(payload);
483483
}
484484

485+
// Leaves the voice channel
486+
public void sendVoicePayloadToLeave(String guildId) {
487+
JSONObject payload = new JSONObject();
488+
JSONObject dPayload = new JSONObject();
489+
dPayload.put("guild_id", guildId);
490+
dPayload.put("channel_id", JSONObject.NULL);
491+
dPayload.put("self_mute", false);
492+
dPayload.put("self_deaf", false);
493+
payload.put("op", OpCodes.VOICE_STATE_UPDATE.opCode);
494+
payload.put("d", dPayload);
495+
queueMessage(payload);
496+
}
497+
485498
public void onVoiceStateUpdate(Consumer<VoiceState> consumer) {
486499
onVoiceStateUpdateListeners.add(consumer);
487500
}

src/main/java/com/seailz/discordjar/model/channel/internal/AudioChannelImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ public void connect(VoiceProvider vp, boolean mute, boolean deafen) {
7575
}
7676

7777
if (receivedVoiceServerUpdate.get() && receivedVoiceStateUpdate.get()) {
78-
System.out.println("Establishing Voice WS Connection");
7978
try {
80-
VoiceGatewayFactory voiceGateway = new VoiceGatewayFactory(guild().id(), discordJv().getSelfUser().id(), sessionId.get(), token.get(), endpoint.get(), vp);
79+
VoiceGatewayFactory voiceGateway = new VoiceGatewayFactory(guild().id(), discordJv().getSelfUser().id(), sessionId.get(), token.get(), endpoint.get(), vp, discordJv());
8180
break;
8281
} catch (ExecutionException | InterruptedException e) {
8382
throw new RuntimeException(e);

src/main/java/com/seailz/discordjar/voice/udp/VoiceUDP.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ public void start() throws IOException {
123123
}
124124

125125
public void disconnect() {
126-
voiceGateway.close(1001);
126+
voiceGateway.close(1000);
127127
socket.disconnect();
128+
voiceGateway.getJar().getGateway().sendVoicePayloadToLeave(voiceGateway.getServerId());
128129
}
129130

130131
public void stop() {

src/main/java/com/seailz/discordjar/voice/ws/VoiceGatewayFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.seailz.discordjar.DiscordJar;
56
import com.seailz.discordjar.voice.model.packet.AudioPacket;
67
import com.seailz.discordjar.voice.model.provider.VoiceProvider;
78
import com.seailz.discordjar.voice.udp.VoiceUDP;
9+
import lombok.Getter;
810
import org.json.JSONArray;
911
import org.json.JSONObject;
1012
import org.springframework.web.socket.CloseStatus;
@@ -28,6 +30,7 @@
2830
// TODO: at some point this should be converted to use the WebSocket class
2931
public class VoiceGatewayFactory extends TextWebSocketHandler {
3032

33+
@Getter
3134
private final String serverId;
3235
private final String userId;
3336
private final String sessionId;
@@ -43,13 +46,17 @@ public class VoiceGatewayFactory extends TextWebSocketHandler {
4346
private boolean speaking = true;
4447
private VoiceUDP socket;
4548

46-
public VoiceGatewayFactory(String serverId, String userId, String sessionId, String token, String endpoint, VoiceProvider prov) throws ExecutionException, InterruptedException {
49+
@Getter
50+
private DiscordJar jar;
51+
52+
public VoiceGatewayFactory(String serverId, String userId, String sessionId, String token, String endpoint, VoiceProvider prov, DiscordJar jar) throws ExecutionException, InterruptedException {
4753
this.serverId = serverId;
4854
this.userId = userId;
4955
this.sessionId = sessionId;
5056
this.token = token;
5157
this.provider = prov;
5258
connect(endpoint);
59+
this.jar = jar;
5360
}
5461

5562
public void connect(String endpoint) throws ExecutionException, InterruptedException {

0 commit comments

Comments
 (0)