feat: make the bot leave, if they are the last one in the vc

Signed-off-by: moonleay <contact@moonleay.net>
This commit is contained in:
moonleay 2023-12-13 18:52:46 +01:00
parent 8b6b7dc5d5
commit 9b51693a75
Signed by: moonleay
GPG key ID: 82667543CCD715FB
2 changed files with 40 additions and 3 deletions

View file

@ -21,18 +21,25 @@ package net.moonleay.botendo
import com.kotlindiscord.kord.extensions.ExtensibleBot
import dev.kord.common.Color
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.getChannelOf
import dev.kord.core.behavior.interaction.response.respond
import dev.kord.core.entity.channel.VoiceChannel
import dev.kord.core.event.interaction.ButtonInteractionCreateEvent
import dev.kord.core.event.user.VoiceStateUpdateEvent
import dev.kord.core.on
import dev.kord.rest.builder.message.embed
import dev.schlaubi.lavakord.LavaKord
import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.kord.getLink
import dev.schlaubi.lavakord.kord.lavakord
import kotlinx.coroutines.flow.count
import net.moonleay.botendo.data.CredentialManager
import net.moonleay.botendo.extensions.music.*
import net.moonleay.botendo.extensions.music.components.MusicManager
import net.moonleay.botendo.extensions.util.InfoExtension
import net.moonleay.botendo.util.Logger
import net.moonleay.botendo.util.MessageUtil
import net.moonleay.botendo.util.UserUtil
object Bot {
//The kord object gets set at app launch
@ -69,7 +76,7 @@ object Bot {
}
this.presence {
this.streaming("music", "https://twitch.tv/limited_dev")
this.streaming("music", "https://twitch.tv/moonleaytv")
}
}
@ -91,6 +98,7 @@ object Bot {
b.onInteraction(response, g, u)
return@on
}
// Button not found
response.respond {
this.embed {
this.color = Color(0xE0311A)
@ -103,6 +111,34 @@ object Bot {
}
}
// Allow the bot to leave the VC if there are no users in the VC
bot.kordRef.on<VoiceStateUpdateEvent> {
if (this.state.userId == bot.kordRef.selfId) {
// Ignore self
return@on
}
val g = this.state.getGuild().asGuild()
val link = lava.getLink(g.id)
if (link.state != Link.State.CONNECTED) {
// Ignore if not connected
return@on
}
val myChannel = link.lastChannelId!!
if (g.getChannelOrNull(Snowflake(myChannel)) == null) {
// Ignore if channel cannot be found
return@on
}
val ch = g.getChannelOf<VoiceChannel>(Snowflake(myChannel))
if (ch.voiceStates.count() > 1) {
// Ignore if there are other users in the channel
return@on
}
// Stop playing if there are no users in the channel
val pl = MusicManager.getMusicManager(g)!!.pl
pl.stopTrack()
link.destroy()
}
//Start the bot
bot.start()
}

View file

@ -25,6 +25,7 @@ import dev.kord.common.Color
import dev.kord.rest.builder.message.embed
import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.kord.getLink
import net.moonleay.botendo.Bot
import net.moonleay.botendo.extensions.music.components.LinkArguments
import net.moonleay.botendo.extensions.music.components.MusicManager
import net.moonleay.botendo.util.MessageUtil
@ -41,7 +42,7 @@ class PlayExtension : Extension() {
description = "Play music"
this.action {
val guildId = this.guild!!.id
val link = net.moonleay.botendo.Bot.lava.getLink(guildId)
val link = Bot.lava.getLink(guildId)
val u = this.user
val vcsUser = u.asMember(guildId).getVoiceStateOrNull()
if (vcsUser == null) {