feat: added FeatureManageExtension, added DB, updated MessageUtil

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-05-13 01:22:00 +02:00
parent 5d1b942f36
commit 4e4da2422f
8 changed files with 297 additions and 19 deletions

View file

@ -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)
}
}

View file

@ -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
)
}
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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)
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Feature> {
this.name = "feature"
this.description = "The targeted feature"
this.typeName = "en_US"
}
val setStatus by enumChoice<EnableOrDisable> {
this.name = "set"
this.description = "Set enabled or disabled"
this.typeName = "en_US"
}
val channel by channel {
this.name = "channel"
this.description = "Target Channel"
}
}
}

View file

@ -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 + "***"

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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");
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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");
}

View file

@ -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<Arguments, ModalForm>,
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<Arguments, ModalForm>,
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