Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed May 15, 2023
0 parents commit 52fc12c
Show file tree
Hide file tree
Showing 40 changed files with 831 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store

**/.test-env
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

1 change: 1 addition & 0 deletions .idea/.name

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

8 changes: 8 additions & 0 deletions .idea/artifacts/common_js_1_0_SNAPSHOT.xml

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

8 changes: 8 additions & 0 deletions .idea/artifacts/common_jvm_1_0_SNAPSHOT.xml

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

7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

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

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

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

12 changes: 12 additions & 0 deletions .idea/dataSources.xml

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

18 changes: 18 additions & 0 deletions .idea/gradle.xml

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

7 changes: 7 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

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

15 changes: 15 additions & 0 deletions .idea/misc.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

22 changes: 22 additions & 0 deletions bot/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
alias(libs.plugins.ksp)
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.mikbot)
}

dependencies {
implementation(projects.common)
plugin(libs.mikbot.ktor)
}

mikbotPlugin {
provider = "Schlaubi"
pluginId = "tonbrett"
license = "MIT"
}

tasks {
installBot {
botVersion = "3.17.0"
}
}
24 changes: 24 additions & 0 deletions bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/Plugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.schlaubi.tonbrett.bot

import com.kotlindiscord.kord.extensions.builders.ExtensibleBotBuilder
import dev.schlaubi.mikbot.plugin.api.Plugin
import dev.schlaubi.mikbot.plugin.api.PluginContext
import dev.schlaubi.mikbot.plugin.api.PluginMain
import dev.schlaubi.mikbot.plugin.api.module.MikBotModule
import dev.schlaubi.tonbrett.bot.commands.addSoundCommand

@PluginMain
class Plugin(context: PluginContext) : Plugin(context) {
override fun ExtensibleBotBuilder.ExtensionsBuilder.addExtensions() {
add(::Module)
}
}

private class Module(context: PluginContext) : MikBotModule(context) {
override val name: String = "tonbrett"
override val bundle: String = "soundboard"

override suspend fun setup() {
addSoundCommand()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dev.schlaubi.tonbrett.bot.commands

import com.kotlindiscord.kord.extensions.commands.Arguments
import com.kotlindiscord.kord.extensions.commands.converters.impl.attachment
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalEmoji
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString
import com.kotlindiscord.kord.extensions.commands.converters.impl.string
import com.kotlindiscord.kord.extensions.extensions.Extension
import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand
import com.kotlindiscord.kord.extensions.types.respond
import dev.schlaubi.mikbot.plugin.api.util.kord
import dev.schlaubi.tonbrett.bot.config.Config
import dev.schlaubi.tonbrett.bot.io.SoundBoardDatabase
import dev.schlaubi.tonbrett.common.Sound
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.*
import org.litote.kmongo.newId
import java.nio.file.StandardOpenOption
import kotlin.io.path.absolutePathString
import kotlin.io.path.div
import kotlin.io.path.writeBytes

class AddSoundCommandArgs : Arguments() {
val sound by attachment {
name = "attachment"
description = "commands.add_sound.arguments.attachment.description"
}

val name by string {
name = "name"
description = "commands.add_sound.arguments.name.description"
}

val description by optionalString {
name = "description"
description = "commands.add_sound.arguments.description.description"
}

val emoji by optionalEmoji {
name = "emoji"
description = "commands.add_sound.arguments.emoji.description"
}
}

suspend fun Extension.addSoundCommand() = ephemeralSlashCommand(::AddSoundCommandArgs) {
name = "add-sound"
description = "commands.add_sound.description"

action {
val id = newId<Sound>()
val cdnResponse = kord.resources.httpClient.get(arguments.sound.proxyUrl)
if (!cdnResponse.status.isSuccess()) {
respond {
content = translate("commands.add_sound.invalid_cdn_response", arrayOf(cdnResponse.status))
}
return@action
}
val sound = Sound(id, arguments.name, user.id, arguments.description, null)
val file = Config.SOUNDS_FOLDER / sound.fileName
file.writeBytes(cdnResponse.body(), StandardOpenOption.CREATE)
SoundBoardDatabase.sounds.save(sound)

respond {
content = translate("commands.add_sound.success")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.schlaubi.tonbrett.bot.config

import dev.schlaubi.mikbot.plugin.api.EnvironmentConfig
import kotlin.io.path.Path

object Config : EnvironmentConfig() {
val SOUNDS_FOLDER by getEnv(Path("sounds"), transform = ::Path)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.schlaubi.tonbrett.bot.io

import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent
import dev.schlaubi.mikbot.plugin.api.io.getCollection
import dev.schlaubi.mikbot.plugin.api.util.database
import dev.schlaubi.tonbrett.common.Sound

object SoundBoardDatabase : KordExKoinComponent {
val sounds = database.getCollection<Sound>("sounds")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.schlaubi.tonbrett.bot.server

import io.ktor.server.routing.*

fun Route.files() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.schlaubi.tonbrett.bot.util

import dev.kord.core.entity.GuildEmoji
import dev.schlaubi.tonbrett.common.Sound

fun GuildEmoji.toEmoji(): Sound.Emoji = Sound.GuildEmoji(mention)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
commands.add_sound.description=Adds a new sound
commands.add_sound.arguments.attachment.description=The audio file you want to add
commands.add_sound.arguments.name.description=The name of the sound
commands.add_sound.arguments.emoji.description=An emoji to identify the sound
commands.add_sound.arguments.description.description=An optional description
commands.add_sound.success=Successfully added sound.
commands.add_sound.invalid_cdn_response=CDN responded with unexpected error code: `{0}`.
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
allprojects {
group = "dev.schlaubi"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}
}
29 changes: 29 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.kotlin.serialization)
}

kotlin {
explicitApi()

jvm()
js(IR) {
browser()
}

sourceSets {
commonMain {
dependencies {
api(libs.kotlinx.serialization)
api(libs.ktor.resources)
}
}

named("jvmMain") {
dependencies {
implementation(libs.kmongo.id.serialization)
implementation(libs.kord.common)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.schlaubi.tonbrett.common

/**
* Multiplatform MongoDB Id.
*/
public expect interface Id<T>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.schlaubi.tonbrett.common

import io.ktor.resources.*

@Resource("/soundboard")
public class Route {
@Resource("audio/{audioId}")
public data class Audio(val audioId: String, val parent: Route = Route())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.schlaubi.tonbrett.common

import kotlinx.serialization.Serializable

/**
* Multiplatform snowflake.
*
* @property value the Snowflake as a [ULong]
*/
public expect class Snowflake(value: ULong) {
public val value: ULong
}
Loading

0 comments on commit 52fc12c

Please sign in to comment.