Compare commits
4 Commits
master
...
feat/laval
Author | SHA1 | Date | |
---|---|---|---|
740166f79f | |||
ef0c5f5d29 | |||
b1431a165a | |||
bf17ae800c |
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,3 +5,4 @@
|
||||
build
|
||||
.vscode
|
||||
.env
|
||||
lavanodes.json
|
||||
|
@ -14,9 +14,8 @@ plugins {
|
||||
repositories {
|
||||
// Use Maven Central for resolving dependencies.
|
||||
mavenCentral()
|
||||
maven {
|
||||
url = uri("https://git.tomatentum.net/api/packages/tueem/maven")
|
||||
}
|
||||
maven("https://maven.lavalink.dev/releases")
|
||||
maven("https://git.tomatentum.net/api/packages/tueem/maven")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@ -32,6 +31,8 @@ dependencies {
|
||||
implementation(libs.logback)
|
||||
implementation(libs.log4jtoslf4j)
|
||||
implementation(libs.jultoslf4j)
|
||||
implementation(libs.jackson)
|
||||
implementation(libs.lavalink)
|
||||
|
||||
implementation(libs.marinaralib)
|
||||
implementation(libs.marinarajavacord)
|
||||
|
@ -6,6 +6,8 @@ import org.javacord.api.entity.intent.Intent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import io.github.cdimascio.dotenv.Dotenv;
|
||||
@ -19,6 +21,7 @@ public class App {
|
||||
new App().connect();
|
||||
}
|
||||
|
||||
private JsonFactory jsonFactory;
|
||||
private Config config;
|
||||
private DiscordApi client;
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
@ -26,6 +29,7 @@ public class App {
|
||||
private Marinara marinara;
|
||||
|
||||
private App() {
|
||||
this.jsonFactory = JsonFactory.builder().build();
|
||||
Dotenv env = Dotenv.configure().ignoreIfMissing().load();
|
||||
this.config = new Config(env);
|
||||
|
||||
@ -50,4 +54,5 @@ public class App {
|
||||
|
||||
marinara.getRegistry().registerCommands();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
package net.tomatentum.tomatenmusic3.lavalink;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JacksonException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.Version;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
|
||||
import dev.arbjerg.lavalink.client.loadbalancing.IRegionFilter;
|
||||
import dev.arbjerg.lavalink.client.loadbalancing.RegionGroup;
|
||||
|
||||
public class IRegionFilterDeserializer extends StdDeserializer<IRegionFilter> {
|
||||
|
||||
public static ObjectMapper register(ObjectMapper mapper) {
|
||||
SimpleModule module =
|
||||
new SimpleModule("IRegionFilterDeserializer", new Version(1, 0, 0, null, null, null));
|
||||
module.addDeserializer(IRegionFilter.class, new IRegionFilterDeserializer());
|
||||
mapper.registerModule(module);
|
||||
return mapper;
|
||||
}
|
||||
|
||||
public IRegionFilterDeserializer() {
|
||||
super(IRegionFilter.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IRegionFilter deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JacksonException {
|
||||
String regionString = parser.getValueAsString();
|
||||
return RegionGroup.INSTANCE.valueOf(regionString);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package net.tomatentum.tomatenmusic3.lavalink;
|
||||
|
||||
import dev.arbjerg.lavalink.client.NodeOptions;
|
||||
import dev.arbjerg.lavalink.client.loadbalancing.IRegionFilter;
|
||||
|
||||
public record LavalinkNodeOptions(String name, String host, int port, String password, IRegionFilter regionGroup) {
|
||||
|
||||
public NodeOptions toNodeOptions() {
|
||||
return new NodeOptions.Builder()
|
||||
.setName(name)
|
||||
.setServerUri("ws://{}:{}".formatted(host, port))
|
||||
.setPassword(password)
|
||||
.setRegionFilter(regionGroup)
|
||||
.build();
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package net.tomatentum.tomatenmusic3.lavalink;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.javacord.api.DiscordApi;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import dev.arbjerg.lavalink.client.Helpers;
|
||||
import dev.arbjerg.lavalink.client.LavalinkClient;
|
||||
import net.tomatentum.tomatenmusic3.Config;
|
||||
|
||||
public class LavalinkWrapper {
|
||||
|
||||
private static final String NODES_FILE_NAME = "lavanodes.json";
|
||||
private static final String NODES_INFO_FORMAT = "Node {} info:\n\tURI: {}\n\tRegion: {}";
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
private DiscordApi client;
|
||||
private JsonFactory jsonFactory;
|
||||
|
||||
private LavalinkClient lavaClient;
|
||||
|
||||
public LavalinkWrapper(Config config, DiscordApi client, JsonFactory jsonFactory) {
|
||||
this.client = client;
|
||||
this.jsonFactory = jsonFactory;
|
||||
|
||||
this.lavaClient = new LavalinkClient(Helpers.getUserIdFromToken(config.token()));
|
||||
getNodes().forEach(node -> {
|
||||
lavaClient.addNode(node.toNodeOptions());
|
||||
logger.info("Registered node {}", node.name());
|
||||
logger.debug(NODES_INFO_FORMAT, node.name(), node.toNodeOptions().getServerUri(), node.regionGroup());
|
||||
});
|
||||
}
|
||||
|
||||
protected Set<LavalinkNodeOptions> getNodes() {
|
||||
File nodesFile = new File(
|
||||
new File(getClass().getProtectionDomain().getCodeSource().getLocation().getFile()).getParent(),
|
||||
NODES_FILE_NAME);
|
||||
ObjectMapper objectMapper = new ObjectMapper(jsonFactory);
|
||||
IRegionFilterDeserializer.register(objectMapper);
|
||||
|
||||
try {
|
||||
List<LavalinkNodeOptions> nodes = objectMapper.readValue(nodesFile, new TypeReference<List<LavalinkNodeOptions>>(){});
|
||||
return new HashSet<>(nodes);
|
||||
} catch (IOException e) {
|
||||
logger.error("lavanodes.json seems to not exist.", e);
|
||||
return Collections.emptySet();
|
||||
}
|
||||
}
|
||||
|
||||
public LavalinkClient client() {
|
||||
return this.lavaClient;
|
||||
}
|
||||
|
||||
public DiscordApi discordClient() {
|
||||
return this.client;
|
||||
}
|
||||
}
|
@ -9,7 +9,9 @@ logback = "1.5.15"
|
||||
slf4j = "2.0.16"
|
||||
log4jtoslf4j = "2.24.3"
|
||||
jultoslf4j = "2.0.16"
|
||||
jackson = "2.18.2"
|
||||
marinara = "1.0.0-RC1-9d88ca9"
|
||||
lavalink = "3.1.0"
|
||||
|
||||
[libraries]
|
||||
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
|
||||
@ -19,6 +21,8 @@ logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback"}
|
||||
slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j"}
|
||||
log4jtoslf4j = { module = "org.apache.logging.log4j:log4j-to-slf4j", version.ref = "log4jtoslf4j"}
|
||||
jultoslf4j = { module = "org.slf4j:jul-to-slf4j", version.ref = "jultoslf4j"}
|
||||
jackson = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson"}
|
||||
lavalink = { module = "dev.arbjerg:lavalink-client", version.ref = "lavalink"}
|
||||
|
||||
marinaralib = { module = "net.tomatentum.Marinara:lib-dev", version.ref = "marinara"}
|
||||
marinarajavacord = { module = "net.tomatentum.Marinara:wrapper-javacord-dev", version.ref = "marinara"}
|
||||
|
9
lavanodes.example.json
Normal file
9
lavanodes.example.json
Normal file
@ -0,0 +1,9 @@
|
||||
[
|
||||
{
|
||||
"name": "node1",
|
||||
"host": "localhost",
|
||||
"port": 2333,
|
||||
"password": "youshallnotpass",
|
||||
"regionGroup": "EUROPE"
|
||||
}
|
||||
]
|
Loading…
x
Reference in New Issue
Block a user