48 lines
1.8 KiB
Kotlin
48 lines
1.8 KiB
Kotlin
package nl.voidcorp.discord.music
|
|
|
|
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager
|
|
import net.dv8tion.jda.api.entities.Guild
|
|
import nl.voidcorp.discord.storage.GuildRepo
|
|
import nl.voidcorp.discord.storage.GuildStore
|
|
import org.springframework.stereotype.Service
|
|
|
|
@Service
|
|
class PlayerManager(val repo: GuildRepo) : DefaultAudioPlayerManager() {
|
|
val guildPlayMap = mutableMapOf<Long, TrackScheduler>()
|
|
fun getGuildPlayer(guild: Guild): TrackScheduler {
|
|
return if (guildPlayMap.containsKey(guild.idLong)) {
|
|
guildPlayMap[guild.idLong] ?: error("oof?")
|
|
} else {
|
|
val player = createPlayer()
|
|
val store = repo.findByGuildId(guild.idLong) ?: GuildStore(guild.idLong)
|
|
val channel = store.musicChannels.firstOrNull()
|
|
?: store.botChannels.firstOrNull()
|
|
?: guild.textChannels.firstOrNull {
|
|
it.name.contains("music", true) && it.name.contains(
|
|
"bot",
|
|
true
|
|
)
|
|
}?.idLong
|
|
?: guild.textChannels.firstOrNull { it.name.contains("music", true) }?.idLong
|
|
?: guild.textChannels.firstOrNull { it.name.contains("bot", true) }?.idLong
|
|
player.volume = 50
|
|
|
|
val ts = TrackScheduler(
|
|
player,
|
|
if (channel != null) MusicAnnouncerImpl(
|
|
guild.getTextChannelById(channel)!!,
|
|
guild
|
|
) else NullMusicAnnouncer(guild)
|
|
) {
|
|
delGuildPlayer(guild)
|
|
}
|
|
|
|
player.addListener(ts)
|
|
guild.audioManager.sendingHandler = AudioPlayerSendHandler(player)
|
|
guildPlayMap[guild.idLong] = ts
|
|
ts
|
|
}
|
|
}
|
|
|
|
fun delGuildPlayer(guild: Guild) = guildPlayMap.remove(guild.idLong)
|
|
} |