Skip to content

Commit 36f2bf8

Browse files
committed
Merge branch 'dev'
2 parents 1bfb227 + 1362945 commit 36f2bf8

30 files changed

+694
-355
lines changed

IMPLEMENTATION.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ Set player volume. Volume may range from 0 to 150. 100 is default.
104104
}
105105
```
106106

107+
Tell the server to potentially disconnect from the voice server and potentially remove the player with all its data.
108+
This is useful if you want to move to a new node for a voice connection. Calling this op does not affect voice state,
109+
and you can send the same VOICE_SERVER_UPDATE to a new node.
110+
```json
111+
{
112+
"op": "destroy",
113+
"guildId": "..."
114+
}
115+
```
116+
107117
### Incoming messages
108118
See
109119
[LavalinkSocket.java](https://github.com/Frederikam/Lavalink/blob/dev/LavalinkClient/src/main/java/lavalink/client/io/LavalinkSocket.java)

LavalinkClient/build.gradle

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
11
description = 'JDA based client for the Lavalink-Server'
2-
version System.getenv('dev') == 'true' ? '-SNAPSHOT' : '2.0'
2+
version System.getenv('dev') == 'true' ? '-SNAPSHOT' : '2.0.1'
33
ext {
44
moduleName = 'Lavalink-Client'
55
}
6+
7+
apply plugin: 'maven-publish'
8+
9+
publishing {
10+
publications {
11+
mavenJava(MavenPublication) {
12+
groupId rootProject.group
13+
artifactId moduleName
14+
15+
from components.java
16+
17+
artifact sourceJar {
18+
classifier "sources"
19+
}
20+
}
21+
}
22+
}
23+
24+
task install(dependsOn: 'publishToMavenLocal')
25+
publishToMavenLocal.dependsOn 'jar'
26+
27+
test {
28+
useJUnitPlatform()
29+
30+
systemProperty("TEST_TOKEN", System.getProperty("TEST_TOKEN"))
31+
systemProperty("TEST_VOICE_CHANNEL", System.getProperty("TEST_VOICE_CHANNEL"))
32+
}
33+
634
dependencies {
7-
compile group: 'com.sedmelluq', name: 'lavaplayer', version: '1.2.47'
8-
compile group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.3.7'
9-
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
10-
compile group: 'org.json', name: 'json', version: '20180130'
11-
compile group: 'net.dv8tion', name: 'JDA', version: '3.5.0_334'
12-
compileOnly group: 'io.prometheus', name: 'simpleclient', version: '0.1.0'
13-
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M4'
14-
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-M4'
15-
testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.0.0-M4'
16-
testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-M4'
17-
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
18-
testCompile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.4.9'
35+
compile group: 'com.sedmelluq', name: 'lavaplayer', version: lavaplayerVersion
36+
compile group: 'org.java-websocket', name: 'Java-WebSocket', version: javaWebSocketVersion
37+
compile group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion
38+
compile group: 'org.json', name: 'json', version: jsonOrgVersion
39+
compile group: 'net.dv8tion', name: 'JDA', version: jdaVersion
40+
compileOnly group: 'io.prometheus', name: 'simpleclient', version: prometheusVersion
41+
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitJupiterVersion
42+
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitJupiterVersion
43+
testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: junitPlatformVersion
44+
testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: junitPlatformVersion
45+
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: logbackVersion
46+
testCompile group: 'com.mashape.unirest', name: 'unirest-java', version: unirestVersion
1947
}

LavalinkClient/src/main/java/lavalink/client/io/Lavalink.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ public void addNode(@Nonnull String name, @Nonnull URI serverUri, @Nonnull Strin
108108
headers.put("Num-Shards", Integer.toString(numShards));
109109
headers.put("User-Id", userId);
110110

111-
nodes.add(new LavalinkSocket(name, this, serverUri, new Draft_6455(), headers));
111+
LavalinkSocket socket = new LavalinkSocket(name, this, serverUri, new Draft_6455(), headers);
112+
socket.connect();
113+
nodes.add(socket);
112114
}
113115

114116
@SuppressWarnings("unused")

LavalinkClient/src/main/java/lavalink/client/io/LavalinkSocket.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ public class LavalinkSocket extends ReusableWebSocket {
6666
this.name = name;
6767
this.lavalink = lavalink;
6868
this.remoteUri = serverUri;
69-
try {
70-
this.connectBlocking();
71-
} catch (InterruptedException e) {
72-
throw new RuntimeException(e);
73-
}
7469
}
7570

7671
@Override

LavalinkClient/src/main/java/lavalink/client/io/Link.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,17 @@ void connect(VoiceChannel channel, boolean checkChannel) {
106106
if (checkChannel && channel.equals(channel.getGuild().getSelfMember().getVoiceState().getChannel()))
107107
return;
108108

109-
final int userLimit = channel.getUserLimit(); // userLimit is 0 if no limit is set!
110-
if (!self.isOwner() && !self.hasPermission(Permission.ADMINISTRATOR)) {
111-
final long perms = PermissionUtil.getExplicitPermission(channel, self);
112-
final long voicePerm = Permission.VOICE_MOVE_OTHERS.getRawValue();
113-
if (userLimit > 0 // If there is a userlimit
114-
&& userLimit <= channel.getMembers().size() // if that userlimit is reached
115-
&& (perms & voicePerm) != voicePerm) // If we don't have voice move others permissions
116-
throw new InsufficientPermissionException(Permission.VOICE_MOVE_OTHERS, // then throw exception!
117-
"Unable to connect to VoiceChannel due to userlimit! Requires permission VOICE_MOVE_OTHERS to bypass");
109+
if (channel.getGuild().getSelfMember().getVoiceState().inVoiceChannel()) {
110+
final int userLimit = channel.getUserLimit(); // userLimit is 0 if no limit is set!
111+
if (!self.isOwner() && !self.hasPermission(Permission.ADMINISTRATOR)) {
112+
final long perms = PermissionUtil.getExplicitPermission(channel, self);
113+
final long voicePerm = Permission.VOICE_MOVE_OTHERS.getRawValue();
114+
if (userLimit > 0 // If there is a userlimit
115+
&& userLimit <= channel.getMembers().size() // if that userlimit is reached
116+
&& (perms & voicePerm) != voicePerm) // If we don't have voice move others permissions
117+
throw new InsufficientPermissionException(Permission.VOICE_MOVE_OTHERS, // then throw exception!
118+
"Unable to connect to VoiceChannel due to userlimit! Requires permission VOICE_MOVE_OTHERS to bypass");
119+
}
118120
}
119121

120122
setState(State.CONNECTING);
@@ -165,8 +167,9 @@ void onDisconnected() {
165167
*/
166168
@SuppressWarnings("unused")
167169
public void destroy() {
170+
boolean shouldDisconnect = state != State.DISCONNECTING && state != State.NOT_CONNECTED;
168171
setState(State.DESTROYING);
169-
if (state != State.DISCONNECTING && state != State.NOT_CONNECTED) {
172+
if (shouldDisconnect) {
170173
Guild g = getJda().getGuildById(guild);
171174
if (g != null) getMainWs().queueAudioDisconnect(g);
172175
}
@@ -199,7 +202,7 @@ public LavalinkSocket getNode() {
199202
public LavalinkSocket getNode(boolean selectIfAbsent) {
200203
if (selectIfAbsent && node == null) {
201204
node = lavalink.loadBalancer.determineBestSocket(guild);
202-
player.onNodeChange();
205+
if (player != null) player.onNodeChange();
203206
}
204207
return node;
205208
}

LavalinkClient/src/main/java/lavalink/client/player/LavalinkPlayer.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
2626
import lavalink.client.LavalinkUtil;
27+
import lavalink.client.io.LavalinkSocket;
2728
import lavalink.client.io.Link;
2829
import lavalink.client.player.event.IPlayerEventListener;
2930
import lavalink.client.player.event.PlayerEvent;
@@ -92,7 +93,9 @@ public void playTrack(AudioTrack track) {
9293
json.put("endTime", trackData.endPos);
9394
}
9495
json.put("pause", paused);
96+
//noinspection ConstantConditions
9597
link.getNode(true).send(json.toString());
98+
9699
updateTime = System.currentTimeMillis();
97100
this.track = track;
98101
emitEvent(new TrackStartEvent(this, track));
@@ -103,22 +106,27 @@ public void playTrack(AudioTrack track) {
103106

104107
@Override
105108
public void stopTrack() {
109+
track = null;
110+
111+
LavalinkSocket node = link.getNode(false);
112+
if (node == null) return;
106113
JSONObject json = new JSONObject();
107114
json.put("op", "stop");
108115
json.put("guildId", link.getGuildId());
109-
link.getNode(true).send(json.toString());
110-
track = null;
116+
node.send(json.toString());
111117
}
112118

113119
@Override
114120
public void setPaused(boolean pause) {
115121
if (pause == paused) return;
116-
117-
JSONObject json = new JSONObject();
118-
json.put("op", "pause");
119-
json.put("guildId", link.getGuildId());
120-
json.put("pause", pause);
121-
link.getNode(true).send(json.toString());
122+
LavalinkSocket node = link.getNode(false);
123+
if (node != null) {
124+
JSONObject json = new JSONObject();
125+
json.put("op", "pause");
126+
json.put("guildId", link.getGuildId());
127+
json.put("pause", pause);
128+
node.send(json.toString());
129+
}
122130
paused = pause;
123131

124132
if (pause) {
@@ -156,19 +164,23 @@ public void seekTo(long position) {
156164
json.put("op", "seek");
157165
json.put("guildId", link.getGuildId());
158166
json.put("position", position);
167+
//noinspection ConstantConditions
159168
link.getNode(true).send(json.toString());
160169
}
161170

162171
@Override
163172
public void setVolume(int volume) {
164173
volume = Math.min(150, Math.max(0, volume)); // Lavaplayer bounds
174+
this.volume = volume;
175+
176+
LavalinkSocket node = link.getNode(false);
177+
if (node == null) return;
165178

166179
JSONObject json = new JSONObject();
167180
json.put("op", "volume");
168181
json.put("guildId", link.getGuildId());
169182
json.put("volume", volume);
170-
link.getNode(true).send(json.toString());
171-
this.volume = volume;
183+
node.send(json.toString());
172184
}
173185

174186
@Override

0 commit comments

Comments
 (0)