Skip to content

Commit

Permalink
Add ability to specify volume for sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Aug 29, 2023
1 parent 151a9a6 commit dcb1639
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class AddSoundCommandArguments : Arguments() {
description = "commands.add_sound.arguments.public.description"
defaultValue = true
}

val volume by optionalInt {
name = "volume"
description = "commands.add_sound.arguments.volume.description"
minValue = 0
maxValue = 1000
}
}

fun SubCommandModule.addCommand() = ephemeralSubCommand(::AddSoundCommandArguments) {
Expand Down Expand Up @@ -101,7 +108,8 @@ fun SubCommandModule.addCommand() = ephemeralSubCommand(::AddSoundCommandArgumen
id, arguments.name, user.id,
arguments.description, arguments.emoji?.toEmoji(),
public = arguments.public,
tag = arguments.tag
tag = arguments.tag,
volume = arguments.volume
)
val file = Config.SOUNDS_FOLDER / sound.fileName
val soundsFolder = file.parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.schlaubi.tonbrett.bot.commands

import com.kotlindiscord.kord.extensions.commands.Arguments
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalBoolean
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalInt
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString
import com.kotlindiscord.kord.extensions.types.respond
import dev.schlaubi.mikbot.plugin.api.module.SubCommandModule
Expand Down Expand Up @@ -43,6 +44,13 @@ class UpdateSoundArguments : Arguments() {
name = "public"
description = "commands.update_sound.arguments.public.description"
}

val volume by optionalInt {
name = "public"
description = "commands.update_sound.arguments.volume.description"
minValue = 0
maxValue = 1000
}
}

fun SubCommandModule.updateCommand() = ephemeralSubCommand(::UpdateSoundArguments) {
Expand All @@ -69,7 +77,8 @@ fun SubCommandModule.updateCommand() = ephemeralSubCommand(::UpdateSoundArgument
description = arguments.description ?: sound.description,
emoji = arguments.emoji?.toEmoji() ?: sound.emoji,
public = arguments.public ?: sound.public,
tag = arguments.tag ?: sound.tag
tag = arguments.tag ?: sound.tag,
volume = arguments.volume ?: sound.volume
)

SoundBoardDatabase.sounds.save(newSound)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.single
import kotlinx.coroutines.flow.take
import kotlin.coroutines.CoroutineContext
import kotlin.io.path.fileVisitor

private val players = mutableMapOf<Snowflake, SoundPlayer>()

Expand Down Expand Up @@ -70,7 +71,13 @@ class SoundPlayer(guild: GuildBehavior) : CoroutineScope {
path("soundboard", "sounds", sound.id.toString(), "audio")
}.toString()
currentUser = user
player.injectTrack(url, noReplace = alreadyLocked)
player.injectTrack(url, noReplace = alreadyLocked) {
filters {
if (sound.volume != null) {
volume = sound.volume!!.toFloat()
}
}
}
launch {
// Wait for track to end
player.player.events
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {

allprojects {
group = "dev.schlaubi.tonbrett"
version = "1.12.12"
version = "1.12.13"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public data class Sound(
val emoji: Emoji? = null,
@EncodeDefault(EncodeDefault.Mode.ALWAYS)
val public: Boolean = true,
val tag: String? = null
val tag: String? = null,
val volume: Int? = null
) {
/**
* The file name of this sounds audio file.
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ ktor-client-auth = { group = "io.ktor", name = "ktor-client-auth", version.ref =
ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktor" }

mikbot-ktor = { group = "dev.schlaubi", name = "mikbot-ktor", version.ref = "mikbot" }
mikbot-music = { group = "dev.schlaubi", name = "mikbot-music-player", version = "3.1.0-SNAPSHOT" }
mikbot-music = { group = "dev.schlaubi", name = "mikbot-music-player", version = "3.1.8-SNAPSHOT" }

imageloader = { group = "io.github.qdsfdhvh", name = "image-loader", version = "1.6.4" }

Expand Down

0 comments on commit dcb1639

Please sign in to comment.