From 4e4da2422f8b4bd0c362d7bc2d39d70f82a712f8 Mon Sep 17 00:00:00 2001 From: limited_dev Date: Sat, 13 May 2023 01:22:00 +0200 Subject: [PATCH] feat: added FeatureManageExtension, added DB, updated MessageUtil Signed-off-by: limited_dev --- src/main/kotlin/de/limited_dev/lilJudd/Bot.kt | 4 +- .../kotlin/de/limited_dev/lilJudd/data/DB.kt | 24 +-- .../data/tables/TimePlanningChannels.kt | 27 +++ .../extensions/FeatureManageExtension.kt | 188 ++++++++++++++++++ .../lilJudd/extensions/VersionExtension.kt | 5 +- .../extensions/component/EnableOrDisable.kt | 27 +++ .../lilJudd/extensions/component/Feature.kt | 25 +++ .../limited_dev/lilJudd/util/MessageUtil.kt | 16 +- 8 files changed, 297 insertions(+), 19 deletions(-) create mode 100644 src/main/kotlin/de/limited_dev/lilJudd/data/tables/TimePlanningChannels.kt create mode 100644 src/main/kotlin/de/limited_dev/lilJudd/extensions/FeatureManageExtension.kt create mode 100644 src/main/kotlin/de/limited_dev/lilJudd/extensions/component/EnableOrDisable.kt create mode 100644 src/main/kotlin/de/limited_dev/lilJudd/extensions/component/Feature.kt diff --git a/src/main/kotlin/de/limited_dev/lilJudd/Bot.kt b/src/main/kotlin/de/limited_dev/lilJudd/Bot.kt index 48db3fe..0f8a0ef 100644 --- a/src/main/kotlin/de/limited_dev/lilJudd/Bot.kt +++ b/src/main/kotlin/de/limited_dev/lilJudd/Bot.kt @@ -22,6 +22,7 @@ import com.kotlindiscord.kord.extensions.ExtensibleBot import de.limited_dev.botendo.build.BuildConstants import de.limited_dev.lilJudd.data.CredentialManager import de.limited_dev.lilJudd.data.DB +import de.limited_dev.lilJudd.extensions.FeatureManageExtension import de.limited_dev.lilJudd.extensions.VersionExtension import de.limited_dev.lilJudd.util.Logger import dev.kord.common.entity.PresenceStatus @@ -64,11 +65,12 @@ object Bot { extensions { add(::VersionExtension) + add(::FeatureManageExtension) } this.presence { this.status = PresenceStatus.DoNotDisturb - this.streaming("v" + BuildConstants.version, "https://www.twitch.tv/deutschesplbundesliga") + this.playing("v." + BuildConstants.version) } } diff --git a/src/main/kotlin/de/limited_dev/lilJudd/data/DB.kt b/src/main/kotlin/de/limited_dev/lilJudd/data/DB.kt index 93e28df..e5d253e 100644 --- a/src/main/kotlin/de/limited_dev/lilJudd/data/DB.kt +++ b/src/main/kotlin/de/limited_dev/lilJudd/data/DB.kt @@ -18,23 +18,17 @@ package de.limited_dev.lilJudd.data -import de.limited_dev.lilJudd.util.Logger -import java.sql.Connection -import java.sql.DriverManager -import kotlin.system.exitProcess + +import org.jetbrains.exposed.sql.Database object DB { - private lateinit var connection: Connection + //Connect to the provided DB; trows errors, if the DB is not avalible. 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) - } + Database.connect( + "jdbc:postgresql://$dbDomain/$dbName", + driver = "org.postgresql.Driver", + user = dbUser, + password = dbPasswd + ) } } diff --git a/src/main/kotlin/de/limited_dev/lilJudd/data/tables/TimePlanningChannels.kt b/src/main/kotlin/de/limited_dev/lilJudd/data/tables/TimePlanningChannels.kt new file mode 100644 index 0000000..fd7f3d6 --- /dev/null +++ b/src/main/kotlin/de/limited_dev/lilJudd/data/tables/TimePlanningChannels.kt @@ -0,0 +1,27 @@ +/* + * lilJudd + * 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.lilJudd.data.tables + +import org.jetbrains.exposed.dao.id.IntIdTable + +object TimePlanningChannels : IntIdTable() { + //val aid = integer("id").autoIncrement() + var serverid = varchar("serverid", 50) + val channelid = varchar("channelid", 50) +} diff --git a/src/main/kotlin/de/limited_dev/lilJudd/extensions/FeatureManageExtension.kt b/src/main/kotlin/de/limited_dev/lilJudd/extensions/FeatureManageExtension.kt new file mode 100644 index 0000000..a04e8e1 --- /dev/null +++ b/src/main/kotlin/de/limited_dev/lilJudd/extensions/FeatureManageExtension.kt @@ -0,0 +1,188 @@ +/* + * lilJudd + * 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.lilJudd.extensions + +import com.kotlindiscord.kord.extensions.commands.Arguments +import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.enumChoice +import com.kotlindiscord.kord.extensions.commands.converters.impl.channel +import com.kotlindiscord.kord.extensions.extensions.Extension +import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand +import com.kotlindiscord.kord.extensions.types.respond +import com.kotlindiscord.kord.extensions.utils.hasPermission +import de.limited_dev.lilJudd.data.tables.TimePlanningChannels +import de.limited_dev.lilJudd.data.tables.TimePlanningChannels.channelid +import de.limited_dev.lilJudd.data.tables.TimePlanningChannels.serverid +import de.limited_dev.lilJudd.extensions.component.EnableOrDisable +import de.limited_dev.lilJudd.extensions.component.Feature +import de.limited_dev.lilJudd.util.MessageUtil +import dev.kord.common.Color +import dev.kord.common.entity.Permission +import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.and +import org.jetbrains.exposed.sql.deleteWhere +import org.jetbrains.exposed.sql.insert +import org.jetbrains.exposed.sql.select +import org.jetbrains.exposed.sql.transactions.transaction + +class FeatureManageExtension : Extension() { + + override val name = "feature" + + override suspend fun setup() { + publicSlashCommand(::FeatureManagerArgs) { + name = "feature" + description = "Manage features" + + this.action { + val u = this.user + if (!u.asMember(this.guild!!.id).hasPermission(Permission.Administrator)) { + this.respond { + embeds.add( + MessageUtil.getEmbed( + Color(0xE0311A), + "403: Forbidden", + "You cannot edit features, as you don't have the Administratior permission.", + u.asUser().username + "#" + u.asUser().discriminator + ) + ) + } + return@action + } + + if (this.arguments.setStatus == EnableOrDisable.ENABLE) { + when (this.arguments.feature) { + Feature.TIMEPLANNINGFEATURE -> { + val gID = this.guild!!.id.toString() + val cID = this.arguments.channel.id.toString() + val args = this.arguments + var alreadyExists: Boolean = false + transaction { + val entryExists = TimePlanningChannels.select { + (serverid eq gID) and + (channelid eq cID) + }.count() > 0 + + alreadyExists = entryExists + } + if (!alreadyExists) { + transaction { + TimePlanningChannels.insert { + it[serverid] = gID + it[channelid] = cID + } get TimePlanningChannels.id + } + this.respond { + embeds.add( + MessageUtil.getEmbed( + Color(0x52E01A), + "200: Success", + "The feature was enabled in channel ${args.channel.data.name}", + u.asUser().username + "#" + u.asUser().discriminator + ) + ) + } + return@action + } + this.respond { + embeds.add( + MessageUtil.getEmbed( + Color(0xE0311A), + "403: Forbidden", + "The feature is already enabled in this channel.", + u.asUser().username + "#" + u.asUser().discriminator + ) + ) + } + } + } + return@action + } + //Disable + when (this.arguments.feature) { + Feature.TIMEPLANNINGFEATURE -> { + val gID = this.guild!!.id.toString() + val cID = this.arguments.channel.id.toString() + var alreadyExists = false + transaction { + val entryExists = TimePlanningChannels.select { + (serverid eq gID) and + (channelid eq cID) + }.count() > 0 + + alreadyExists = entryExists + } + if (alreadyExists) { + transaction { + val matchingEntries = TimePlanningChannels.select { + (serverid eq gID) and + (channelid eq cID) + }.toList() + + matchingEntries.forEach { entry -> + TimePlanningChannels.deleteWhere { id eq entry[id] } + } + } + this.respond { + embeds.add( + MessageUtil.getEmbed( + Color(0x52E019), + "200: Success", + "The feature was disabled.", + u.asUser().username + "#" + u.asUser().discriminator + ) + ) + } + return@action + } + this.respond { + embeds.add( + MessageUtil.getEmbed( + Color(0xE0311A), + "403: Forbidden", + "The feature is already disabled in this channel.", + u.asUser().username + "#" + u.asUser().discriminator + ) + ) + } + } + } + } + } + } + + inner class FeatureManagerArgs : Arguments() { + + val feature by enumChoice { + this.name = "feature" + this.description = "The targeted feature" + this.typeName = "en_US" + } + + val setStatus by enumChoice { + this.name = "set" + this.description = "Set enabled or disabled" + this.typeName = "en_US" + } + + val channel by channel { + this.name = "channel" + this.description = "Target Channel" + } + } +} diff --git a/src/main/kotlin/de/limited_dev/lilJudd/extensions/VersionExtension.kt b/src/main/kotlin/de/limited_dev/lilJudd/extensions/VersionExtension.kt index 94eb96e..66616d8 100644 --- a/src/main/kotlin/de/limited_dev/lilJudd/extensions/VersionExtension.kt +++ b/src/main/kotlin/de/limited_dev/lilJudd/extensions/VersionExtension.kt @@ -22,6 +22,7 @@ import com.kotlindiscord.kord.extensions.extensions.Extension import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand import de.limited_dev.botendo.build.BuildConstants import de.limited_dev.lilJudd.util.MessageUtil +import dev.kord.common.Color class VersionExtension : Extension() { override val name = "version" @@ -31,7 +32,9 @@ class VersionExtension : Extension() { description = "Show infos about the bot" this.action { MessageUtil.sendEmbedForPublicSlashCommand( - this, "Lil' Judd", + this, + Color(0x52E01A), + "Lil' Judd", "Lil' Judd ***v." + BuildConstants.version + "***\n" + "Kord-Extensions ***v." + BuildConstants.kordVersion + "***\n" + "Coroutines ***v." + BuildConstants.coroutinesVersion + "***" diff --git a/src/main/kotlin/de/limited_dev/lilJudd/extensions/component/EnableOrDisable.kt b/src/main/kotlin/de/limited_dev/lilJudd/extensions/component/EnableOrDisable.kt new file mode 100644 index 0000000..4067eb8 --- /dev/null +++ b/src/main/kotlin/de/limited_dev/lilJudd/extensions/component/EnableOrDisable.kt @@ -0,0 +1,27 @@ +/* + * lilJudd + * 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.lilJudd.extensions.component + +import com.kotlindiscord.kord.extensions.commands.application.slash.converters.ChoiceEnum + +enum class EnableOrDisable(override val readableName: String) : ChoiceEnum { + ENABLE("Enable"), + DISABLE("Disable"); + +} diff --git a/src/main/kotlin/de/limited_dev/lilJudd/extensions/component/Feature.kt b/src/main/kotlin/de/limited_dev/lilJudd/extensions/component/Feature.kt new file mode 100644 index 0000000..515ae59 --- /dev/null +++ b/src/main/kotlin/de/limited_dev/lilJudd/extensions/component/Feature.kt @@ -0,0 +1,25 @@ +/* + * lilJudd + * 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.lilJudd.extensions.component + +import com.kotlindiscord.kord.extensions.commands.application.slash.converters.ChoiceEnum + +enum class Feature(override val readableName: String) : ChoiceEnum { + TIMEPLANNINGFEATURE("Time Planning Feature"); +} diff --git a/src/main/kotlin/de/limited_dev/lilJudd/util/MessageUtil.kt b/src/main/kotlin/de/limited_dev/lilJudd/util/MessageUtil.kt index f04c757..3749b46 100644 --- a/src/main/kotlin/de/limited_dev/lilJudd/util/MessageUtil.kt +++ b/src/main/kotlin/de/limited_dev/lilJudd/util/MessageUtil.kt @@ -22,6 +22,7 @@ import com.kotlindiscord.kord.extensions.commands.Arguments import com.kotlindiscord.kord.extensions.commands.application.slash.PublicSlashCommandContext import com.kotlindiscord.kord.extensions.components.forms.ModalForm import com.kotlindiscord.kord.extensions.types.respond +import dev.kord.common.Color import dev.kord.rest.builder.message.EmbedBuilder import java.time.LocalDateTime import java.time.format.DateTimeFormatter @@ -32,12 +33,14 @@ object MessageUtil { ///Send an embedded message as a reply suspend fun sendEmbedForPublicSlashCommand( ctx: PublicSlashCommandContext, + color: Color, title: String, description: String ) { ctx.respond { embeds.add( getEmbed( + color, title, description, ctx.user.asUser().username + "#" + ctx.user.asUser().discriminator @@ -49,6 +52,7 @@ object MessageUtil { ///Send an embedded message with an image as a reply suspend fun sendEmbedForPublicSlashCommandWithImage( ctx: PublicSlashCommandContext, + color: Color, title: String, description: String, thumbnailUrl: String @@ -56,6 +60,7 @@ object MessageUtil { ctx.respond { embeds.add( getEmbedWithImage( + color, title, description, ctx.user.asUser().username + "#" + ctx.user.asUser().discriminator, @@ -66,24 +71,31 @@ object MessageUtil { } ///Get an embedded msg with title, description and a src - fun getEmbed(title: String, description: String, source: String): EmbedBuilder { + fun getEmbed( + color: Color, + title: String, + description: String, + source: String + ): EmbedBuilder { val now: LocalDateTime = LocalDateTime.now() val ebb = EmbedBuilder() ebb.title = title ebb.description = description ebb.footer = EmbedBuilder.Footer() ebb.footer!!.text = ">" + dtf.format(now) + " - $source" + ebb.color = color return ebb } ///Get an embedded msg with image, title, description and a src fun getEmbedWithImage( + color: Color, title: String, description: String, source: String, thumbnailUrl: String ): EmbedBuilder { - val ebb = getEmbed(title, description, source) + val ebb = getEmbed(color, title, description, source) ebb.thumbnail = EmbedBuilder.Thumbnail() ebb.thumbnail!!.url = thumbnailUrl return ebb