Kord 0.14.0 Help

Implementing a music bot

@OptIn(KordVoice::class) suspend fun main(args: Array<String>) { val token = args.firstOrNull() ?: error("token required") val kord = Kord(token) val lavaplayerManager = DefaultAudioPlayerManager() // to use YouTube, we tell LavaPlayer to use remote sources, like YouTube. AudioSourceManagers.registerRemoteSources(lavaplayerManager) // here we keep track of active voice connections val connections: MutableMap<Snowflake, VoiceConnection> = mutableMapOf() kord.on<MessageCreateEvent> { if (message.content.startsWith("!play")) { val channel = member?.getVoiceState()?.getChannelOrNull() ?: return@on // lets close the old connection if there is one if (connections.contains(guildId)) { connections.remove(guildId)!!.shutdown() } // our lavaplayer audio player which will provide frames of audio val player = lavaplayerManager.createPlayer() // lavaplayer uses ytsearch: as an identifier to search for YouTube val query = "ytsearch: ${message.content.removePrefix("!play")}" val track = lavaplayerManager.playTrack(query, player) // here we actually connect to the voice channel val connection = channel.connect { // the audio provider should provide frames of audio audioProvider { AudioFrame.fromData(player.provide()?.data) } } connections[guildId!!] = connection message.reply { content = "playing track: ${track.info.title}" } } else if (message.content == "!stop") { if (guildId == null) return@on connections.remove(guildId)?.shutdown() } } kord.login() }
Last modified: 08 April 2023