Compare commits

...

2 commits

Author SHA1 Message Date
9b51693a75
feat: make the bot leave, if they are the last one in the vc
Signed-off-by: moonleay <contact@moonleay.net>
2023-12-13 18:52:46 +01:00
8b6b7dc5d5
feat: leave vc, if there is no song next up and the current one failed to load 2023-12-13 18:50:38 +01:00
3 changed files with 52 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) {

View file

@ -36,6 +36,10 @@ import net.moonleay.botendo.util.*
object MusicManager {
private var musicManagerMap: MutableMap<Snowflake, GuildTrackScheduler> = mutableMapOf()
fun getMusicManager(guild: Guild): GuildTrackScheduler? {
return musicManagerMap[guild.id]
}
fun getGuildTrackScheduler(guild: Guild, player: Player): GuildTrackScheduler {
return musicManagerMap.computeIfAbsent(guild.id) {
GuildTrackScheduler(player)
@ -181,6 +185,10 @@ object MusicManager {
}
is LoadResult.NoMatches -> {
if(gts.isEmpty()) {
player.stopTrack()
link.destroy()
}
if (!silent)
ctx.respond {
this.embed {
@ -194,6 +202,10 @@ object MusicManager {
}
}
is LoadResult.LoadFailed -> {
if (gts.isEmpty()) {
player.stopTrack()
link.destroy()
}
if (!silent)
ctx.respond {
this.embed {