Skip to content

Commit

Permalink
Add support for user bots
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Aug 25, 2024
1 parent e17c75a commit eaa13ce
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 26 deletions.
10 changes: 10 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.schlaubi.mikbot.plugin.api.settings

import com.kotlindiscord.kord.extensions.commands.application.slash.SlashCommand
import dev.kord.common.entity.InteractionContextType
import dev.kord.common.entity.Permission
import dev.schlaubi.mikbot.plugin.api.*

Expand All @@ -19,5 +20,5 @@ public interface SettingsExtensionPoint : ModuleExtensionPoint<SettingsModule> {

public fun SlashCommand<*, *, *>.guildAdminOnly() {
requirePermission(Permission.ManageGuild)
allowInDms = false
allowedContexts.add(InteractionContextType.Guild)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.schlaubi.mikbot.gradle
import dev.schlaubi.mikbot.gradle.extension.mikbotPluginExtension
import dev.schlaubi.mikbot.gradle.extension.pluginId
import org.gradle.api.Project
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.distribution.DistributionContainer
import org.gradle.api.distribution.plugins.DistributionPlugin
import org.gradle.api.file.DuplicatesStrategy
Expand Down Expand Up @@ -68,13 +69,15 @@ private fun TaskContainer.createAssembleBotTask(
into(installedPluginsName) {
from({
val dependencies =
(configurations.getByName("plugin").resolvedConfiguration.resolvedArtifacts +
configurations.getByName("optionalPlugin").resolvedConfiguration.resolvedArtifacts)
(configurations.getByName("plugin").allDependencies +
configurations.getByName("optionalPlugin").allDependencies)
.filterIsInstance<ModuleDependency>()
.filterNot { it.isTransitive }
.map {
dependencies.create(
mapOf(
"name" to it.moduleVersion.id.name,
"version" to it.moduleVersion.id.version,
"name" to it.name,
"version" to it.version,
"ext" to "zip"
)
)
Expand Down
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[versions]
kotlin = "2.0.0"
kordex = "1.8.1-SNAPSHOT"
kordex = "1.9.0-mikbot-SNAPSHOT"
kmongo = "5.1.0"
coroutines = "1.8.1"
serialization = "1.7.1"
ktor = "2.3.11"
kord = "0.14.0"
api = "3.34.0"
kord = "feature-user-apps-SNAPSHOT"
api = "3.35.0"
ksp = "2.0.0-1.0.22"
lavakord = "6.4.0-SNAPSHOT"

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion music/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
subprojects {
version = "3.8.10-SNAPSHOT"
version = "3.9.0-SNAPSHOT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ suspend fun MusicModule.fixCommand() = ephemeralControlSlashCommand {
val user = event.interaction.user

val event = channel.kord.waitFor<GuildComponentInteractionCreateEvent>(timeout = null as Long?) {
this.interaction.message.interaction?.id == interaction.id && this.interaction.user == user
this.interaction.message.interactionMetadata?.id == interaction.id && this.interaction.user == user
}

event?.interaction?.deferEphemeralMessageUpdate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.kotlindiscord.kord.extensions.extensions.ephemeralMessageCommand
import dev.schlaubi.mikbot.plugin.api.util.attachmentOrContentQuery
import dev.schlaubi.mikmusic.checks.joinSameChannelCheck
import dev.schlaubi.mikmusic.core.MusicModule
import dev.schlaubi.mikmusic.core.musicControlContexts
import dev.schlaubi.mikmusic.player.MusicPlayer
import dev.schlaubi.mikmusic.player.queue.QueueOptions
import dev.schlaubi.mikmusic.player.queue.SearchQuery
import dev.schlaubi.mikmusic.player.queue.queueTracks

Expand All @@ -15,6 +15,8 @@ const val playActionName = "Play as track"
suspend fun MusicModule.playMessageAction() = ephemeralMessageCommand {
name = playActionName

musicControlContexts()

check {
joinSameChannelCheck(bot)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ suspend fun SettingsModule.djModeCommand() {

guildAdminOnly()


action {
val role = arguments.djRole

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.schlaubi.mikmusic.core.settings.commands

import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand
import dev.kord.common.entity.InteractionContextType
import dev.schlaubi.mikbot.plugin.api.settings.SettingsModule
import dev.schlaubi.mikbot.plugin.api.util.safeGuild
import dev.schlaubi.mikmusic.core.settings.MusicSettingsDatabase
Expand All @@ -9,8 +10,7 @@ import dev.schlaubi.mikmusic.util.musicModule
suspend fun SettingsModule.fixMusicChannel() = ephemeralSlashCommand {
name = "fix-music-channel"
description = "Force-updates the music channel status"
allowInDms = false

allowedContexts.add(InteractionContextType.Guild)

action {
val guildSettings = MusicSettingsDatabase.findGuild(safeGuild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fun PlaylistModule.addCommand() = ephemeralSubCommand(::PlaylistAddArguments) {

action {
checkPermissions { playlist ->
val result = findTracks(musicPlayer, arguments.search) ?: return@action
val result = findTracks(node, arguments.search) ?: return@action
val tracks = result.tracks.mapToEncoded()

PlaylistDatabase.collection.save(playlist.copy(songs = playlist.songs + tracks))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.kotlindiscord.kord.extensions.commands.application.slash.EphemeralSla
import com.kotlindiscord.kord.extensions.commands.converters.impl.string
import dev.kord.core.behavior.UserBehavior
import dev.kord.core.behavior.interaction.suggestString
import dev.schlaubi.lavakord.audio.Node
import dev.schlaubi.mikbot.plugin.api.PluginContext
import dev.schlaubi.mikbot.plugin.api.module.SubCommandModule
import dev.schlaubi.mikbot.plugin.api.util.extension
Expand Down Expand Up @@ -83,6 +84,9 @@ class PlaylistModule(context: PluginContext) : SubCommandModule(context) {
val CommandContext.musicPlayer: MusicPlayer
get() = with(musicModule) { musicPlayer }

val CommandContext.node: Node
get() = with(musicModule) { node }

override suspend fun overrideSetup() {
loadCommand()
saveCommand()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.schlaubi.mikmusic.playlist.commands

import dev.schlaubi.mikmusic.checks.joinSameChannelCheck
import dev.schlaubi.mikmusic.core.musicControlContexts
import dev.schlaubi.mikmusic.player.queue.SchedulingArguments
import dev.schlaubi.mikmusic.playlist.PlaylistDatabase
import dev.schlaubi.mikmusic.util.mapToQueuedTrack
Expand All @@ -13,6 +14,8 @@ fun PlaylistModule.loadCommand() = ephemeralSubCommand(::LoadArguments) {
name = "load"
description = "commands.playlist.load.description"

musicControlContexts()

check {
joinSameChannelCheck(bot)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.kotlindiscord.kord.extensions.commands.Arguments
import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingBoolean
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString
import com.kotlindiscord.kord.extensions.commands.converters.impl.string
import dev.schlaubi.mikmusic.core.musicControlContexts
import dev.schlaubi.mikmusic.player.QueuedTrack
import dev.schlaubi.mikmusic.player.queue.QueueOptions
import dev.schlaubi.mikmusic.player.queue.findTracks
Expand Down Expand Up @@ -40,6 +41,8 @@ fun PlaylistModule.saveCommand() = ephemeralSubCommand(::PlaylistSaveArguments)
name = "create"
description = "Creates a new playlist"

musicControlContexts()

action {
if (musicPlayer.playingTrack == null && arguments.importFrom == null) {
respond {
Expand All @@ -52,7 +55,7 @@ fun PlaylistModule.saveCommand() = ephemeralSubCommand(::PlaylistSaveArguments)
val tracks = if (arguments.importFrom == null) {
(listOfNotNull(musicPlayer.playingTrack) + musicPlayer.queuedTracks).map(QueuedTrack::track)
} else {
findTracks(musicPlayer, false)?.tracks ?: return@action
findTracks(node, false)?.tracks ?: return@action
}

val playlist = Playlist(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.kotlindiscord.kord.extensions.commands.application.slash.EphemeralSla
import com.kotlindiscord.kord.extensions.extensions.Extension
import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand
import com.kotlindiscord.kord.extensions.extensions.event
import dev.kord.common.entity.ApplicationIntegrationType
import dev.kord.common.entity.InteractionContextType
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.GuildBehavior
import dev.kord.core.event.gateway.ReadyEvent
Expand Down Expand Up @@ -56,6 +58,8 @@ class MusicModule(context: PluginContext) : MikBotModule(context) {

val CommandContext.musicPlayer
get() = getMusicPlayer(safeGuild)
val CommandContext.node
get() = lavalink.newNode()

fun getMusicPlayer(guild: GuildBehavior): MusicPlayer {
return musicPlayers.computeIfAbsent(guild.id) {
Expand Down Expand Up @@ -109,6 +113,8 @@ class MusicModule(context: PluginContext) : MikBotModule(context) {
create: suspend Extension.(suspend T.() -> Unit) -> T,
body: suspend T.() -> Unit,
) = create {
musicControlContexts()

check {
musicControlCheck()
}
Expand Down Expand Up @@ -145,6 +151,11 @@ class MusicModule(context: PluginContext) : MikBotModule(context) {
}
}

fun ApplicationCommand<*>.musicControlContexts() {
allowedInstallTypes.add(ApplicationIntegrationType.GuildInstall)
allowedContexts.add(InteractionContextType.Guild)
}

/**
* Checks all [properties] to be false, and otherwise uses [confirmation] to confirm an overwrite to toggle [myProperty].
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.schlaubi.mikmusic.core.audio

import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.GuildBehavior
import dev.schlaubi.lavakord.LavaKord
import dev.schlaubi.lavakord.audio.Node
import dev.schlaubi.lavakord.audio.internal.AbstractLavakord
import dev.schlaubi.lavakord.kord.getLink
import dev.schlaubi.lavakord.kord.lavakord
import dev.schlaubi.lavakord.plugins.lavasrc.LavaSrc
Expand Down Expand Up @@ -37,6 +39,10 @@ class LavalinkManager(context: PluginContext) : MikBotModule(context) {

fun getLink(guild: GuildBehavior) = lavalink.getLink(guild.id)

@Suppress("INVISIBLE_REFERENCE")
fun newNode() = (lavalink as AbstractLavakord).loadBalancer.determineBestNode(Snowflake.min)
?: error("No node found")

override suspend fun unload() {
lavalink.nodes.forEach(Node::close)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class MusicInteractionModule(context: PluginContext) : MikBotModule(context) {
event.message.channel.withTyping {
val guild = event.getGuildOrNull() ?: error("Could not find guild")
val player = musicModule.getMusicPlayer(guild)
val track = takeFirstMatch(player, event.message.content) { event.message.reply { it() } }
val track = takeFirstMatch(player.node, event.message.content) { event.message.reply { it() } }
?: return@withTyping
player.queueTrack(
force = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.kotlindiscord.kord.extensions.types.TranslatableContext
import dev.arbjerg.lavalink.protocol.v4.Exception
import dev.arbjerg.lavalink.protocol.v4.LoadResult
import dev.kord.rest.builder.message.embed
import dev.schlaubi.lavakord.audio.Node
import dev.schlaubi.lavakord.rest.loadItem
import dev.schlaubi.mikbot.plugin.api.util.EditableMessageSender
import dev.schlaubi.mikbot.plugin.api.util.IKnowWhatIAmDoing
Expand Down Expand Up @@ -104,24 +105,24 @@ suspend fun <T : QueueArguments> EphemeralSlashCommandContext<T, *>.queueTracks(
}

suspend fun <T> EphemeralSlashCommandContext<T, *>.findTracks(
musicPlayer: MusicPlayer,
node: Node,
search: Boolean,
): QueueSearchResult?
where T : Arguments, T : QueueOptions {
return findTracks(musicPlayer, search, arguments, ::respond) {
return findTracks(node, search, arguments, ::respond) {
editingPaginator {
it()
}
}
}

internal suspend fun CommandContext.findTracks(
musicPlayer: MusicPlayer,
node: Node,
search: Boolean,
arguments: QueueOptions,
respond: EditableMessageSender,
editingPaginator: EditingPaginatorSender,
): QueueSearchResult? = findTracks(musicPlayer, arguments, respond) search@{ result ->
): QueueSearchResult? = findTracks(node, arguments, respond) search@{ result ->
if (search) {
searchSong(respond, editingPaginator, getUser()!!, result) ?: return@search null
} else {
Expand All @@ -131,16 +132,16 @@ internal suspend fun CommandContext.findTracks(
}

internal suspend fun TranslatableContext.takeFirstMatch(
musicPlayer: MusicPlayer,
node: Node,
query: String,
respond: MessageSender
): QueueSearchResult? = findTracks(musicPlayer, SearchQuery(query), respond) {
): QueueSearchResult? = findTracks(node, SearchQuery(query), respond) {
it.data.tracks.firstOrNull()?.let(::SingleTrack)
}


private suspend fun TranslatableContext.findTracks(
musicPlayer: MusicPlayer,
node: Node,
arguments: QueueOptions,
respond: MessageSender,
handleSearch: suspend (LoadResult.SearchResult) -> QueueSearchResult?,
Expand All @@ -155,7 +156,7 @@ private suspend fun TranslatableContext.findTracks(
searchPrefix + rawQuery
} else rawQuery

val searchResult: QueueSearchResult = when (val result = musicPlayer.loadItem(query)) {
val searchResult: QueueSearchResult = when (val result = node.loadItem(query)) {
is LoadResult.TrackLoaded -> SingleTrack(result.data)
is LoadResult.PlaylistLoaded ->
Playlist(result.data, result.data.tracks)
Expand Down Expand Up @@ -183,7 +184,7 @@ suspend fun CommandContext.queueTracks(
respond: EditableMessageSender,
editingPaginator: EditingPaginatorSender,
) {
val searchResult = findTracks(musicPlayer, search, arguments, respond, editingPaginator) ?: return
val searchResult = findTracks(musicPlayer.node, search, arguments, respond, editingPaginator) ?: return

val title = if (musicPlayer.nextSongIsFirst) translate("music.queue.now_playing", "music") else translate(
"music.queue.queued",
Expand Down

0 comments on commit eaa13ce

Please sign in to comment.