Skip to content

Commit

Permalink
Remove await call for request channel updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bombies committed Sep 16, 2023
1 parent dab4b7c commit cc9c33c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.sedmelluq.discord.lavaplayer.tools.FriendlyException
import dev.arbjerg.lavalink.protocol.v4.Playlist
import dev.arbjerg.lavalink.protocol.v4.Track
import dev.minn.jda.ktx.util.SLF4J
import kotlinx.coroutines.runBlocking
import main.audiohandlers.GuildMusicManager
import main.audiohandlers.loaders.MainAudioLoader.Companion.queueThenDelete
import main.audiohandlers.utils.identifier
Expand Down
83 changes: 44 additions & 39 deletions src/main/kotlin/main/events/VoiceChannelEvents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main.events

import dev.minn.jda.ktx.util.SLF4J
import main.audiohandlers.RobertifyAudioManager
import main.main.Robertify
import main.utils.json.guildconfig.GuildConfig
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent

Expand All @@ -12,48 +13,52 @@ class VoiceChannelEvents : AbstractEventController() {

private val onGuildVoiceUpdate =
onEvent<GuildVoiceUpdateEvent> { event ->
val guild = event.guild
val channelLeft = event.channelLeft
val channelJoined = event.channelJoined
val self = guild.selfMember
val selfVoiceState = self.voiceState!!
val guildMusicManager = RobertifyAudioManager[guild]

// If the bot has left voice channels entirely
if (event.member.id == self.id && (channelLeft != null && channelJoined == null))
return@onEvent guildMusicManager.clear()

if (!selfVoiceState.inAudioChannel())
return@onEvent

val guildConfig = GuildConfig(guild)
val guildDisconnector = guildMusicManager.scheduler.disconnectManager

/*
* If the user left voice channels entirely or
* switched and the channel left is empty we want to
* disconnect the bot unless 24/7 mode is enabled.
*/
if (
((channelJoined == null && channelLeft != null) || (channelJoined != null && channelLeft != null))
&& channelLeft.id == selfVoiceState.channel!!.id
) {
if (guildConfig.getTwentyFourSevenMode() || guildDisconnector.disconnectScheduled() || channelLeft.members.size > 1)
try {
val guild = event.guild
val channelLeft = event.channelLeft
val channelJoined = event.channelJoined
val self = guild.selfMember
val selfVoiceState = self.voiceState!!
val guildMusicManager = RobertifyAudioManager[guild]

// If the bot has left voice channels entirely
if (event.member.id == self.id && (channelLeft != null && channelJoined == null))
return@onEvent guildMusicManager.clear()

if (!selfVoiceState.inAudioChannel())
return@onEvent
guildMusicManager.player.pause(true)
guildDisconnector.scheduleDisconnect()
}

/*
* If the user is joining a voice channel for the
* first time and the bot is awaiting disconnect,
* cancel the disconnect and resume playing the song if
* the song is paused.
*/
else if (channelJoined != null && channelJoined.id == selfVoiceState.channel!!.id && guildDisconnector.disconnectScheduled()) {
guildDisconnector.cancelDisconnect()
guildMusicManager.player.pause(false)
val guildConfig = GuildConfig(guild)
val guildDisconnector = guildMusicManager.scheduler.disconnectManager

/*
* If the user left voice channels entirely or
* switched and the channel left is empty we want to
* disconnect the bot unless 24/7 mode is enabled.
*/
if (
((channelJoined == null && channelLeft != null) || (channelJoined != null && channelLeft != null))
&& channelLeft.id == selfVoiceState.channel!!.id
) {
if (guildConfig.getTwentyFourSevenMode() || guildDisconnector.disconnectScheduled() || channelLeft.members.size > 1)
return@onEvent
guildMusicManager.player.pause(true)
guildDisconnector.scheduleDisconnect()
}

/*
* If the user is joining a voice channel for the
* first time and the bot is awaiting disconnect,
* cancel the disconnect and resume playing the song if
* the song is paused.
*/
else if (channelJoined != null && channelJoined.id == selfVoiceState.channel!!.id && guildDisconnector.disconnectScheduled()) {
guildDisconnector.cancelDisconnect()
guildMusicManager.player.pause(false)

}
} catch (e: UninitializedPropertyAccessException) {
logger.warn("Tried handling voice channel events before LavaKord was setup!")
}
}
}
53 changes: 26 additions & 27 deletions src/main/kotlin/main/main/Robertify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.adamratzman.spotify.SpotifyAppApi
import com.adamratzman.spotify.spotifyAppApi
import com.google.common.util.concurrent.ThreadFactoryBuilder
import dev.minn.jda.ktx.events.CoroutineEventManager
import dev.minn.jda.ktx.events.getDefaultScope
import dev.minn.jda.ktx.events.listener
import dev.minn.jda.ktx.jdabuilder.injectKTX
import dev.minn.jda.ktx.util.SLF4J
Expand Down Expand Up @@ -158,6 +159,30 @@ object Robertify {
shardManagerBuilder.applyLavakord(lavakordShardManager)
shardManager = shardManagerBuilder.build()
logger.info("Successfully built shard manager")

logger.info("Setting up LavaKord...")
lavaKord = shardManager.lavakord(
lavakordShardManager, getDefaultScope().coroutineContext, options = MutableLavaKordOptions(
link = MutableLavaKordOptions.LinkConfig(
showTrace = true
)
)
) {
plugins {
install(LavaSrc)
}
}

Config.LAVA_NODES.forEach { node ->
lavaKord.addNode(
serverUri = node.uri.toString(),
password = node.password,
name = node.name
)
logger.info("Registered lava node with address: ${node.uri}")
}
logger.info("LavaKord ready")

shardManager.handleShardReady()
shardManager.handleGuildReady()

Expand Down Expand Up @@ -198,34 +223,8 @@ object Robertify {
BotDBCache.instance.lastStartup = System.currentTimeMillis()
jda.shardManager?.setPresence(OnlineStatus.ONLINE, Activity.listening("/help"))

if (!Robertify::lavaKord.isInitialized) {
logger.info("Setting up LavaKord...")
val (dispatcher, supervisor, handler) = GeneralUtils.generateHandleCoroutineContextComponents()
lavaKord = shardManager.lavakord(
lavakordShardManager, dispatcher + supervisor + handler, options = MutableLavaKordOptions(
link = MutableLavaKordOptions.LinkConfig(
showTrace = true
)
)
) {
plugins {
install(LavaSrc)
}
}

Config.LAVA_NODES.forEach { node ->
lavaKord.addNode(
serverUri = node.uri.toString(),
password = node.password,
name = node.name
)
logger.info("Registered lava node with address: ${node.uri}")
}
logger.info("LavaKord ready")
}

shardManager.guildCache.forEach { guild ->
RequestChannelConfig(guild).updateMessage()?.await()
RequestChannelConfig(guild).updateMessage()?.start()
}
}

Expand Down

0 comments on commit cc9c33c

Please sign in to comment.