Skip to content

Commit

Permalink
Fix music playback
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Feb 8, 2024
1 parent cc25c01 commit bc91550
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ plugins {

val pmdVersion = "7.0.0-rc4"

val numberVersion = "3.109.1"
val numberVersion = "3.109.2"

project.group = "me.duncte123.skybot"
project.version = "${numberVersion}_${getGitHash()}"
Expand Down
12 changes: 11 additions & 1 deletion bot/src/main/java/fredboat/audio/player/LavalinkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,17 @@ public boolean isConnected(Guild guild) {
}

public boolean isConnected(long guildId) {
return !isEnabled() || lavalink.getLink(guildId).getState() == LinkState.CONNECTED;
if (!isEnabled()) {
return false;
}

final var link = lavalink.getLinkIfCached(guildId);

if (link == null) {
return false;
}

return link.getState() == LinkState.CONNECTED;
}

@SuppressWarnings("ConstantConditions") // cache is enabled
Expand Down
14 changes: 10 additions & 4 deletions bot/src/main/java/me/duncte123/skybot/audio/GuildMusicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
Expand Down Expand Up @@ -81,14 +82,19 @@ public Optional<MessageChannel> getLatestChannel() {
);
}

@Nullable
public Link getLink() {
return LavalinkManager.INS.getLavalink().getLink(this.guildId);
return LavalinkManager.INS.getLavalink().getLinkIfCached(this.guildId);
}

public Optional<LavalinkPlayer> getPlayer() {
return Optional.ofNullable(
this.getLink().getCachedPlayer()
);
final var link = this.getLink();

if (link == null) {
return Optional.empty();
}

return Optional.ofNullable(link.getCachedPlayer());
}

public long getGuildId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class LyricsCommand : MusicCommand() {
return
}

loadLyricsFromLavalink(mng.link) {
loadLyricsFromLavalink(mng.link!!) {
if (it == null) {
sendMsg(ctx, "There where no lyrics found for `${playingTrack.info.title}`")
return@loadLyricsFromLavalink
Expand Down Expand Up @@ -106,7 +106,7 @@ class LyricsCommand : MusicCommand() {

event.deferReply().queue()

loadLyricsFromLavalink(mng.link) {
loadLyricsFromLavalink(mng.link!!) {
if (it == null) {
event.hook.sendMessage("There where no lyrics found for `${playingTrack.info.title}`")
.queue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ open class PlayCommand(private val skipParsing: Boolean = false) : MusicCommand(
}

override fun handleEvent(event: SlashCommandInteractionEvent, variables: Variables) {
if (!event.member!!.voiceState!!.inAudioChannel()) {
event.reply("Auto-join is not yet supported for slash commands. Sorry about that").queue()
return
}

var toPlay = event.getOption("item")!!.asString

if (toPlay.contains(pornhubRegex.toRegex()) && !event.channel.isNSFW) {
Expand Down
5 changes: 3 additions & 2 deletions bot/src/main/kotlin/me/duncte123/skybot/objects/AudioData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package me.duncte123.skybot.objects

import me.duncte123.skybot.Variables
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent

data class AudioData(
Expand All @@ -31,7 +31,8 @@ data class AudioData(
val jda: JDA,
val variables: Variables,
) {
fun getChannel() = jda.getChannelById(MessageChannelUnion::class.java, channelId)!!
val channel: MessageChannel
get() = jda.getChannelById(MessageChannel::class.java, channelId)!!

companion object {
fun fromSlash(event: SlashCommandInteractionEvent, variables: Variables): AudioData = AudioData(
Expand Down

0 comments on commit bc91550

Please sign in to comment.