From 8ac643bbe8e64eb4cc7acfe4448567b5dfbd329d Mon Sep 17 00:00:00 2001 From: limited_dev Date: Thu, 4 May 2023 23:20:52 +0200 Subject: [PATCH] feat: started working on DB connection chore: upgrade lavalink.kt Signed-off-by: limited_dev --- build.gradle.kts | 30 +++++++++++--- src/main/kotlin/de/limited_dev/botendo/Bot.kt | 25 +++++------ .../kotlin/de/limited_dev/botendo/Main.kt | 2 +- .../{util => data}/CredentialManager.kt | 2 +- .../kotlin/de/limited_dev/botendo/data/DB.kt | 41 +++++++++++++++++++ .../music/components/GuildTrackScheduler.kt | 29 ++++++------- .../music/components/MusicManager.kt | 2 +- .../botendo/extensions/util/InfoExtension.kt | 5 ++- .../botendo/build/BuildConstants.kt | 3 ++ 9 files changed, 100 insertions(+), 39 deletions(-) rename src/main/kotlin/de/limited_dev/botendo/{util => data}/CredentialManager.kt (98%) create mode 100644 src/main/kotlin/de/limited_dev/botendo/data/DB.kt diff --git a/build.gradle.kts b/build.gradle.kts index 2413c18..97469f7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -22,22 +22,25 @@ import org.jetbrains.gradle.ext.TaskTriggersConfig import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - kotlin("jvm") version "1.8.0" + kotlin("jvm") version "1.8.21" id("com.github.johnrengelman.shadow") version "7.1.2" id("org.jetbrains.gradle.plugin.idea-ext") version "1.1.6" `maven-publish` } -//Botendo version 5 +//Botendo version 6 val ownerID = 372703841151614976L group = "de.limited_dev.botendo" version = System.getenv("CI_COMMIT_TAG")?.let { "$it-${System.getenv("CI_COMMIT_SHORT_SHA")}-prod" } ?: System.getenv("CI_COMMIT_SHORT_SHA")?.let { "$it-dev" } - ?: "6.2.0" + ?: "6.3.0" val kordver = "1.5.6" -val lavaver = "3.8.0" +val lavaver = "4.0.0" val coroutinesver = "1.1.0" +val ktor_version = "2.3.0" +val exposedver = "0.40.1" +val postgresver = "42.3.8" val mavenArtifact = "Botendo" project.base.archivesName.set(mavenArtifact) @@ -77,11 +80,25 @@ val implementation by configurations.getting implementation.extendsFrom(shadow) dependencies { + //Discord shadow("com.kotlindiscord.kord.extensions:kord-extensions:$kordver") shadow("dev.schlaubi.lavakord:kord:$lavaver") + + //Util shadow("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesver") + + //Logging shadow("org.slf4j:slf4j-api:2.0.3") shadow("org.slf4j:slf4j-simple:2.0.3") + + //Database + shadow("io.ktor:ktor-client-core:$ktor_version") + shadow("io.ktor:ktor-client-cio:$ktor_version") + shadow("org.jetbrains.exposed:exposed-core:$exposedver") + shadow("org.jetbrains.exposed:exposed-dao:$exposedver") + shadow("org.jetbrains.exposed:exposed-jdbc:$exposedver") + + shadow("org.postgresql:postgresql:$postgresver") } @@ -93,7 +110,10 @@ val templateProps = mapOf( "ownerID" to ownerID, "kordversion" to kordver, "lavaversion" to lavaver, - "coroutinesversion" to coroutinesver + "coroutinesversion" to coroutinesver, + "ktorversion" to ktor_version, + "exposedversion" to exposedver, + "postgresversion" to postgresver ) diff --git a/src/main/kotlin/de/limited_dev/botendo/Bot.kt b/src/main/kotlin/de/limited_dev/botendo/Bot.kt index 296088e..ac04a8f 100644 --- a/src/main/kotlin/de/limited_dev/botendo/Bot.kt +++ b/src/main/kotlin/de/limited_dev/botendo/Bot.kt @@ -21,10 +21,10 @@ package de.limited_dev.botendo import com.kotlindiscord.kord.extensions.ExtensibleBot import de.limited_dev.botendo.buttons.ButtonManager +import de.limited_dev.botendo.data.CredentialManager import de.limited_dev.botendo.extensions.music.* import de.limited_dev.botendo.extensions.util.InfoExtension import de.limited_dev.botendo.extensions.util.PotatoExtension -import de.limited_dev.botendo.util.CredentialManager import de.limited_dev.botendo.util.Logger import de.limited_dev.botendo.util.MessageUtil import dev.kord.common.Color @@ -84,24 +84,21 @@ object Bot { val response = inter.deferPublicResponse() val u = inter.user val g = this.interaction.getOriginalInteractionResponse().getGuild() - var flag = false for (b in ButtonManager.buttons) { - if (b.id != inter.componentId || flag) + if (b.id != inter.componentId) continue b.onInteraction(response, g, u) - flag = true + return@on } - if (!flag) { - response.respond { - this.embeds = mutableListOf( - MessageUtil.getEmbed( - Color(0xE0311A), - "Error", - "Could not find button with id \"${inter.componentId}\".\nPlease report this.", - u.asUser().username + "#" + u.asUser().discriminator - ) + response.respond { + this.embeds = mutableListOf( + MessageUtil.getEmbed( + Color(0xE0311A), + "Error", + "Could not find button with id \"${inter.componentId}\".\nPlease report this.", + u.asUser().username + "#" + u.asUser().discriminator ) - } + ) } } diff --git a/src/main/kotlin/de/limited_dev/botendo/Main.kt b/src/main/kotlin/de/limited_dev/botendo/Main.kt index 59b9dd8..de71e03 100644 --- a/src/main/kotlin/de/limited_dev/botendo/Main.kt +++ b/src/main/kotlin/de/limited_dev/botendo/Main.kt @@ -34,6 +34,6 @@ suspend fun main() { "M#########M \n" + " " ) - println("Bot v.${BuildConstants.version}, Kord Extensions v.${BuildConstants.kordVersion}, LavaKord v.${BuildConstants.lavaVersion}, Coroutines v.${BuildConstants.coroutinesVersion}") + println("Bot v.${BuildConstants.version}, Kord Extensions v.${BuildConstants.kordVersion}, LavaKord v.${BuildConstants.lavaVersion}, Coroutines v.${BuildConstants.coroutinesVersion}, Ktor v.${BuildConstants.ktorVersion}, Exposed v.${BuildConstants.exposedVersion}, Postgres v.${BuildConstants.postgresVersion}") Bot.start() } diff --git a/src/main/kotlin/de/limited_dev/botendo/util/CredentialManager.kt b/src/main/kotlin/de/limited_dev/botendo/data/CredentialManager.kt similarity index 98% rename from src/main/kotlin/de/limited_dev/botendo/util/CredentialManager.kt rename to src/main/kotlin/de/limited_dev/botendo/data/CredentialManager.kt index d46b5ca..af04e33 100644 --- a/src/main/kotlin/de/limited_dev/botendo/util/CredentialManager.kt +++ b/src/main/kotlin/de/limited_dev/botendo/data/CredentialManager.kt @@ -17,7 +17,7 @@ * */ -package de.limited_dev.botendo.util +package de.limited_dev.botendo.data import java.io.* import java.util.* diff --git a/src/main/kotlin/de/limited_dev/botendo/data/DB.kt b/src/main/kotlin/de/limited_dev/botendo/data/DB.kt new file mode 100644 index 0000000..5ebd226 --- /dev/null +++ b/src/main/kotlin/de/limited_dev/botendo/data/DB.kt @@ -0,0 +1,41 @@ +/* + * Botendo + * Copyright (C) 2023 limited_dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package de.limited_dev.botendo.data + +import de.limited_dev.botendo.util.Logger +import java.sql.Connection +import java.sql.DriverManager +import kotlin.system.exitProcess + +object DB { + private lateinit var connection: Connection + fun connect(dbDomain: String, dbName: String, dbUser: String, dbPasswd: String) { + val jdbcUrl = "jdbc:postgresql://$dbDomain/$dbName" + connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPasswd) + + if (connection.isValid(0)) { + Logger.out("DB Connection Success!") + } else { + Logger.out("Could not connect to the Database!") + Logger.out("Check credentials.nils file.") + exitProcess(4) + } + } +} diff --git a/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/GuildTrackScheduler.kt b/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/GuildTrackScheduler.kt index 420eae7..5320e33 100644 --- a/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/GuildTrackScheduler.kt +++ b/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/GuildTrackScheduler.kt @@ -20,23 +20,20 @@ package de.limited_dev.botendo.extensions.music.components import de.limited_dev.botendo.util.Logger -import dev.schlaubi.lavakord.audio.TrackEndEvent -import dev.schlaubi.lavakord.audio.TrackExceptionEvent -import dev.schlaubi.lavakord.audio.TrackStuckEvent -import dev.schlaubi.lavakord.audio.on +import dev.schlaubi.lavakord.audio.* import dev.schlaubi.lavakord.audio.player.Player -import dev.schlaubi.lavakord.rest.TrackResponse +import dev.schlaubi.lavakord.rest.models.PartialTrack import java.util.concurrent.BlockingQueue import java.util.concurrent.LinkedBlockingQueue class GuildTrackScheduler(val pl: Player) { - private var queue: BlockingQueue = LinkedBlockingQueue() + private var queue: BlockingQueue = LinkedBlockingQueue() var repeating = false private var hasRegisteredEvents = false ///Add a track to queue and start playing, if there is no song currently playing - suspend fun queue(track: TrackResponse.PartialTrack) { + suspend fun queue(track: PartialTrack) { if (this.pl.playingTrack == null) { play(track) } else { @@ -51,7 +48,7 @@ class GuildTrackScheduler(val pl: Player) { this.pl.stopTrack() } - private suspend fun play(tr: TrackResponse.PartialTrack) { + private suspend fun play(tr: PartialTrack) { this.pl.playTrack(tr) } @@ -62,15 +59,15 @@ class GuildTrackScheduler(val pl: Player) { Logger.out("Adding track events to GuildTrackScheduler...") - pl.on { + pl.on { onTrackEnd(this) } - pl.on { + pl.on { onTrackStuck(this) } - pl.on { + pl.on { onTrackExc(this) } } @@ -78,7 +75,7 @@ class GuildTrackScheduler(val pl: Player) { private suspend fun onTrackEnd(e: TrackEndEvent) { if (e.reason.mayStartNext) { if (repeating) { - this.pl.playTrack(e.track.copy()) + this.pl.playTrack(e.getTrack().copy()) return } Logger.out("Track has ended; Playing next...") @@ -88,12 +85,12 @@ class GuildTrackScheduler(val pl: Player) { private suspend fun onTrackStuck(e: TrackStuckEvent) { Logger.out("Track is stuck, retrying...") - this.pl.playTrack(e.track.copy()) + this.pl.playTrack(e.getTrack().copy()) } private suspend fun onTrackExc(e: TrackExceptionEvent) { Logger.out("Track had an exception, retrying...") - this.pl.playTrack(e.track.copy()) + this.pl.playTrack(e.getTrack().copy()) } fun clear() { @@ -102,11 +99,11 @@ class GuildTrackScheduler(val pl: Player) { queue.clear() } - fun getQueue(): List { + fun getQueue(): List { return queue.toList() } - fun getHead(): TrackResponse.PartialTrack { + fun getHead(): PartialTrack { return queue.first() } diff --git a/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/MusicManager.kt b/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/MusicManager.kt index f81b07e..8ee57ec 100644 --- a/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/MusicManager.kt +++ b/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/MusicManager.kt @@ -33,8 +33,8 @@ import dev.kord.core.entity.Guild import dev.kord.rest.builder.message.create.actionRow import dev.schlaubi.lavakord.audio.Link import dev.schlaubi.lavakord.audio.player.Player -import dev.schlaubi.lavakord.rest.TrackResponse import dev.schlaubi.lavakord.rest.loadItem +import dev.schlaubi.lavakord.rest.models.TrackResponse object MusicManager { private var musicManagerMap: MutableMap = mutableMapOf() diff --git a/src/main/kotlin/de/limited_dev/botendo/extensions/util/InfoExtension.kt b/src/main/kotlin/de/limited_dev/botendo/extensions/util/InfoExtension.kt index 347533a..8bca8d3 100644 --- a/src/main/kotlin/de/limited_dev/botendo/extensions/util/InfoExtension.kt +++ b/src/main/kotlin/de/limited_dev/botendo/extensions/util/InfoExtension.kt @@ -37,7 +37,10 @@ class InfoExtension : Extension() { "Botendo ***v." + BuildConstants.version + "***\n" + "Kord-Extensions ***v." + BuildConstants.kordVersion + "***\n" + "lavalink.kt ***v." + BuildConstants.lavaVersion + "***\n" + - "Coroutines ***v." + BuildConstants.coroutinesVersion + "***" + "Coroutines ***v." + BuildConstants.coroutinesVersion + "***\n" + + "Ktor ***v." + BuildConstants.ktorVersion + "***\n" + + "Exposed ***v." + BuildConstants.exposedVersion + "***\n" + + "PostgreSQL ***v." + BuildConstants.postgresVersion + "***" ) } } diff --git a/src/main/templates/de/limited_dev/botendo/build/BuildConstants.kt b/src/main/templates/de/limited_dev/botendo/build/BuildConstants.kt index 8e33917..f001cee 100644 --- a/src/main/templates/de/limited_dev/botendo/build/BuildConstants.kt +++ b/src/main/templates/de/limited_dev/botendo/build/BuildConstants.kt @@ -25,4 +25,7 @@ internal object BuildConstants { const val kordVersion = "${kordversion}" const val lavaVersion = "${lavaversion}" const val coroutinesVersion = "${coroutinesversion}" + const val ktorVersion = "${ktorversion}" + const val exposedVersion = "${exposedversion}" + const val postgresVersion = "${postgresversion}" }