Skip to content

Commit

Permalink
Add a filter builder to aid users in setting filters
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Dec 20, 2023
1 parent c1d5321 commit 8e47675
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ Alternatively, you can use `Discord4JUtils.leave(gatewayClient, guildId);` as th
## Examples
The following examples are minimal implementations but show how the library works.
- Java examples
- JDA: [link](src/test/java/JavaJDAExample.java)
- JDA (simple): [link](src/test/java/JavaJDAExample.java)
- JDA (more complex example): [link](testbot/src/main/java/me/duncte123/testbot/Main.java)
- Kotlin examples
- JDA: [link](src/test/kotlin/testScript.kt)
- Discord4J: [link](src/test/kotlin/d4jTestScript.kt)
Expand Down
20 changes: 11 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,22 @@ group = "dev.arbjerg"
version = gitVersion
val archivesBaseName = "lavalink-client"

allprojects {
repositories {
mavenCentral()
maven("https://maven.lavalink.dev/releases")
maven("https://maven.lavalink.dev/snapshots")
maven("https://maven.topi.wtf/releases")
// Note to self: jitpack always comes last
maven("https://jitpack.io")
}
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

repositories {
mavenCentral()
maven("https://maven.lavalink.dev/releases")
maven("https://maven.lavalink.dev/snapshots")
maven("https://maven.topi.wtf/releases")
// Note to self: jitpack always comes last
maven("https://jitpack.io")
}

dependencies {
// package libraries
api(kotlin("stdlib"))
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ rootProject.name = "lavalink-client"

enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

include(":testbot")

dependencyResolutionManagement {
versionCatalogs {
create("libs") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package dev.arbjerg.lavalink.client.protocol

import dev.arbjerg.lavalink.internal.toJsonElement
import dev.arbjerg.lavalink.protocol.v4.*
import kotlinx.serialization.json.JsonElement

class FilterBuilder {
private var volume: Omissible<Float> = Omissible.Omitted()
private var equalizer: Omissible<List<Band>> = Omissible.Omitted()
private var karaoke: Omissible<Karaoke?> = Omissible.Omitted()
private var timescale: Omissible<Timescale?> = Omissible.Omitted()
private var tremolo: Omissible<Tremolo?> = Omissible.Omitted()
private var vibrato: Omissible<Vibrato?> = Omissible.Omitted()
private var distortion: Omissible<Distortion?> = Omissible.Omitted()
private var rotation: Omissible<Rotation?> = Omissible.Omitted()
private var channelMix: Omissible<ChannelMix?> = Omissible.Omitted()
private var lowPass: Omissible<LowPass?> = Omissible.Omitted()
private var pluginFilters: MutableMap<String, JsonElement> = mutableMapOf()

fun setVolume(volume: Float) = apply {
this.volume = volume.toOmissible()
}

fun setEqualizer(equalizer: List<Band>) = apply {
this.equalizer = equalizer.toOmissible()
}

fun setKaraoke(karaoke: Karaoke?) = apply {
this.karaoke = Omissible.of(karaoke)
}

fun setTimescale(timescale: Timescale?) = apply {
this.timescale = Omissible.of(timescale)
}

fun setTremolo(tremolo: Tremolo?) = apply {
this.tremolo = Omissible.of(tremolo)
}

fun setVibrato(vibrato: Vibrato?) = apply {
this.vibrato = Omissible.of(vibrato)
}

fun setDistortion(distortion: Distortion?) = apply {
this.distortion = Omissible.of(distortion)
}

fun setRotation(rotation: Rotation?) = apply {
this.rotation = Omissible.of(rotation)
}

fun setChannelMix(channelMix: ChannelMix?) = apply {
this.channelMix = Omissible.of(channelMix)
}

fun setLowPass(lowPass: LowPass?) = apply {
this.lowPass = Omissible.of(lowPass)
}

fun setPluginFilter(name: String, filter: Any) = apply {
pluginFilters[name] = toJsonElement(filter)
}

fun setPluginFilter(name: String, filter: JsonElement) = apply {
pluginFilters[name] = filter
}

fun removePluginFilter(name: String) = apply {
pluginFilters.remove(name)
}

fun build() = Filters(
volume,
equalizer,
karaoke,
timescale,
tremolo,
vibrato,
distortion,
rotation,
channelMix,
lowPass,
pluginFilters
)
}
21 changes: 21 additions & 0 deletions testbot/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
java
application
}

group = "me.duncte123"
version = "1.0-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

dependencies {
// Include the lavalink client
implementation(projects.lavalinkClient)

// other libs such as a discord client and a logger
implementation(libs.jda)
implementation(libs.logger.impl)
}
77 changes: 77 additions & 0 deletions testbot/src/main/java/me/duncte123/testbot/AudioLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package me.duncte123.testbot;

import dev.arbjerg.lavalink.client.AbstractAudioLoadResultHandler;
import dev.arbjerg.lavalink.client.Link;
import dev.arbjerg.lavalink.client.protocol.*;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class AudioLoader extends AbstractAudioLoadResultHandler {
private final Link link;
private final SlashCommandInteractionEvent event;

public AudioLoader(Link link, SlashCommandInteractionEvent event) {
this.link = link;
this.event = event;
}

@Override
public void ontrackLoaded(@NotNull TrackLoaded result) {
final Track track = result.getTrack();

// Inner class at the end of this file
var userData = new MyUserData(event.getUser().getIdLong());

track.setUserData(userData);

link.createOrUpdatePlayer()
.setTrack(track)
.setVolume(35)
.subscribe((player) -> {
final Track playingTrack = player.getTrack();
final var trackTitle = playingTrack.getInfo().getTitle();
final MyUserData customData = playingTrack.getUserData(MyUserData.class);

event.getHook().sendMessage("Now playing: " + trackTitle + "\nRequested by: <@" + customData.requester() + '>').queue();
});
}

@Override
public void onPlaylistLoaded(@NotNull PlaylistLoaded result) {
final int trackCount = result.getTracks().size();
event.getHook()
.sendMessage("This playlist has " + trackCount + " tracks!")
.queue();
}

@Override
public void onSearchResultLoaded(@NotNull SearchResult result) {
final List<Track> tracks = result.getTracks();

if (tracks.isEmpty()) {
event.getHook().sendMessage("No tracks found!").queue();
return;
}

final Track firstTrack = tracks.get(0);

// This is a different way of updating the player! Choose your preference!
// This method will also create a player if there is not one in the server yet
link.updatePlayer((update) -> update.setTrack(firstTrack).setVolume(35))
.subscribe((ignored) -> {
event.getHook().sendMessage("Now playing: " + firstTrack.getInfo().getTitle()).queue();
});
}

@Override
public void noMatches() {
event.getHook().sendMessage("No matches found for your input!").queue();
}

@Override
public void loadFailed(@NotNull LoadFailed result) {
event.getHook().sendMessage("Failed to load track! " + result.getException().getMessage()).queue();
}
}
Loading

0 comments on commit 8e47675

Please sign in to comment.