Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
freyacodes committed Apr 3, 2018
2 parents 1bfb227 + 1362945 commit 36f2bf8
Show file tree
Hide file tree
Showing 30 changed files with 694 additions and 355 deletions.
10 changes: 10 additions & 0 deletions IMPLEMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ Set player volume. Volume may range from 0 to 150. 100 is default.
}
```

Tell the server to potentially disconnect from the voice server and potentially remove the player with all its data.
This is useful if you want to move to a new node for a voice connection. Calling this op does not affect voice state,
and you can send the same VOICE_SERVER_UPDATE to a new node.
```json
{
"op": "destroy",
"guildId": "..."
}
```

### Incoming messages
See
[LavalinkSocket.java](https://github.com/Frederikam/Lavalink/blob/dev/LavalinkClient/src/main/java/lavalink/client/io/LavalinkSocket.java)
Expand Down
54 changes: 41 additions & 13 deletions LavalinkClient/build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
description = 'JDA based client for the Lavalink-Server'
version System.getenv('dev') == 'true' ? '-SNAPSHOT' : '2.0'
version System.getenv('dev') == 'true' ? '-SNAPSHOT' : '2.0.1'
ext {
moduleName = 'Lavalink-Client'
}

apply plugin: 'maven-publish'

publishing {
publications {
mavenJava(MavenPublication) {
groupId rootProject.group
artifactId moduleName

from components.java

artifact sourceJar {
classifier "sources"
}
}
}
}

task install(dependsOn: 'publishToMavenLocal')
publishToMavenLocal.dependsOn 'jar'

test {
useJUnitPlatform()

systemProperty("TEST_TOKEN", System.getProperty("TEST_TOKEN"))
systemProperty("TEST_VOICE_CHANNEL", System.getProperty("TEST_VOICE_CHANNEL"))
}

dependencies {
compile group: 'com.sedmelluq', name: 'lavaplayer', version: '1.2.47'
compile group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.3.7'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
compile group: 'org.json', name: 'json', version: '20180130'
compile group: 'net.dv8tion', name: 'JDA', version: '3.5.0_334'
compileOnly group: 'io.prometheus', name: 'simpleclient', version: '0.1.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M4'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-M4'
testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.0.0-M4'
testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-M4'
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
testCompile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.4.9'
compile group: 'com.sedmelluq', name: 'lavaplayer', version: lavaplayerVersion
compile group: 'org.java-websocket', name: 'Java-WebSocket', version: javaWebSocketVersion
compile group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion
compile group: 'org.json', name: 'json', version: jsonOrgVersion
compile group: 'net.dv8tion', name: 'JDA', version: jdaVersion
compileOnly group: 'io.prometheus', name: 'simpleclient', version: prometheusVersion
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitJupiterVersion
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitJupiterVersion
testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: junitPlatformVersion
testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: junitPlatformVersion
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: logbackVersion
testCompile group: 'com.mashape.unirest', name: 'unirest-java', version: unirestVersion
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public void addNode(@Nonnull String name, @Nonnull URI serverUri, @Nonnull Strin
headers.put("Num-Shards", Integer.toString(numShards));
headers.put("User-Id", userId);

nodes.add(new LavalinkSocket(name, this, serverUri, new Draft_6455(), headers));
LavalinkSocket socket = new LavalinkSocket(name, this, serverUri, new Draft_6455(), headers);
socket.connect();
nodes.add(socket);
}

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public class LavalinkSocket extends ReusableWebSocket {
this.name = name;
this.lavalink = lavalink;
this.remoteUri = serverUri;
try {
this.connectBlocking();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
Expand Down
25 changes: 14 additions & 11 deletions LavalinkClient/src/main/java/lavalink/client/io/Link.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,17 @@ void connect(VoiceChannel channel, boolean checkChannel) {
if (checkChannel && channel.equals(channel.getGuild().getSelfMember().getVoiceState().getChannel()))
return;

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

setState(State.CONNECTING);
Expand Down Expand Up @@ -165,8 +167,9 @@ void onDisconnected() {
*/
@SuppressWarnings("unused")
public void destroy() {
boolean shouldDisconnect = state != State.DISCONNECTING && state != State.NOT_CONNECTED;
setState(State.DESTROYING);
if (state != State.DISCONNECTING && state != State.NOT_CONNECTED) {
if (shouldDisconnect) {
Guild g = getJda().getGuildById(guild);
if (g != null) getMainWs().queueAudioDisconnect(g);
}
Expand Down Expand Up @@ -199,7 +202,7 @@ public LavalinkSocket getNode() {
public LavalinkSocket getNode(boolean selectIfAbsent) {
if (selectIfAbsent && node == null) {
node = lavalink.loadBalancer.determineBestSocket(guild);
player.onNodeChange();
if (player != null) player.onNodeChange();
}
return node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import lavalink.client.LavalinkUtil;
import lavalink.client.io.LavalinkSocket;
import lavalink.client.io.Link;
import lavalink.client.player.event.IPlayerEventListener;
import lavalink.client.player.event.PlayerEvent;
Expand Down Expand Up @@ -92,7 +93,9 @@ public void playTrack(AudioTrack track) {
json.put("endTime", trackData.endPos);
}
json.put("pause", paused);
//noinspection ConstantConditions
link.getNode(true).send(json.toString());

updateTime = System.currentTimeMillis();
this.track = track;
emitEvent(new TrackStartEvent(this, track));
Expand All @@ -103,22 +106,27 @@ public void playTrack(AudioTrack track) {

@Override
public void stopTrack() {
track = null;

LavalinkSocket node = link.getNode(false);
if (node == null) return;
JSONObject json = new JSONObject();
json.put("op", "stop");
json.put("guildId", link.getGuildId());
link.getNode(true).send(json.toString());
track = null;
node.send(json.toString());
}

@Override
public void setPaused(boolean pause) {
if (pause == paused) return;

JSONObject json = new JSONObject();
json.put("op", "pause");
json.put("guildId", link.getGuildId());
json.put("pause", pause);
link.getNode(true).send(json.toString());
LavalinkSocket node = link.getNode(false);
if (node != null) {
JSONObject json = new JSONObject();
json.put("op", "pause");
json.put("guildId", link.getGuildId());
json.put("pause", pause);
node.send(json.toString());
}
paused = pause;

if (pause) {
Expand Down Expand Up @@ -156,19 +164,23 @@ public void seekTo(long position) {
json.put("op", "seek");
json.put("guildId", link.getGuildId());
json.put("position", position);
//noinspection ConstantConditions
link.getNode(true).send(json.toString());
}

@Override
public void setVolume(int volume) {
volume = Math.min(150, Math.max(0, volume)); // Lavaplayer bounds
this.volume = volume;

LavalinkSocket node = link.getNode(false);
if (node == null) return;

JSONObject json = new JSONObject();
json.put("op", "volume");
json.put("guildId", link.getGuildId());
json.put("volume", volume);
link.getNode(true).send(json.toString());
this.volume = volume;
node.send(json.toString());
}

@Override
Expand Down
Loading

0 comments on commit 36f2bf8

Please sign in to comment.