From daa6e17c04f268d9675c232f89e58e932b0ea340 Mon Sep 17 00:00:00 2001 From: limited_dev Date: Mon, 29 May 2023 13:57:04 +0200 Subject: [PATCH] feat: added PlanningNotifyerRoles to db loader, added PlanningNotifyerRoles to feature command, renamed Feature to FeatureEnum Signed-off-by: limited_dev --- .../data/tables/PlanningNotifyerRoles.kt | 29 +++ .../extensions/FeatureManageExtension.kt | 171 ++++++++++++++---- .../component/{Feature.kt => FeatureEnum.kt} | 5 +- 3 files changed, 172 insertions(+), 33 deletions(-) create mode 100644 src/main/kotlin/net/moonleay/lilJudd/data/tables/PlanningNotifyerRoles.kt rename src/main/kotlin/net/moonleay/lilJudd/extensions/component/{Feature.kt => FeatureEnum.kt} (83%) diff --git a/src/main/kotlin/net/moonleay/lilJudd/data/tables/PlanningNotifyerRoles.kt b/src/main/kotlin/net/moonleay/lilJudd/data/tables/PlanningNotifyerRoles.kt new file mode 100644 index 0000000..069bcc0 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/data/tables/PlanningNotifyerRoles.kt @@ -0,0 +1,29 @@ +/* + * lilJudd + * Copyright (C) 2023 moonleay + * + * 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 net.moonleay.lilJudd.data.tables + +import org.jetbrains.exposed.dao.id.IntIdTable + +object PlanningNotifyerRoles : IntIdTable() { + var serverid = varchar("serverid", 50) + var channelid = varchar("channelid", 50) + var hastimeroleid = varchar("hastimeroleid", 50) + var hastovoteid = varchar("hastovoteid", 50) + var wantstobenotifid = varchar("wantstobenotifid", 50) +} diff --git a/src/main/kotlin/net/moonleay/lilJudd/extensions/FeatureManageExtension.kt b/src/main/kotlin/net/moonleay/lilJudd/extensions/FeatureManageExtension.kt index 2bdd9e4..43cfbd4 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/extensions/FeatureManageExtension.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/extensions/FeatureManageExtension.kt @@ -27,23 +27,26 @@ import com.kotlindiscord.kord.extensions.types.respond import com.kotlindiscord.kord.extensions.utils.hasPermission import dev.kord.common.Color import dev.kord.common.entity.Permission +import dev.kord.common.entity.Snowflake +import dev.kord.core.behavior.createRole +import net.moonleay.lilJudd.data.tables.PlanningNotifyerRoles import net.moonleay.lilJudd.data.tables.TimePlanningChannels -import net.moonleay.lilJudd.data.tables.TimePlanningChannels.channelid -import net.moonleay.lilJudd.data.tables.TimePlanningChannels.serverid import net.moonleay.lilJudd.extensions.component.EnableOrDisable -import net.moonleay.lilJudd.extensions.component.Feature +import net.moonleay.lilJudd.extensions.component.FeatureEnum +import net.moonleay.lilJudd.util.Logger import net.moonleay.lilJudd.util.MessageUtil +import org.jetbrains.exposed.sql.* 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" + /* + * Note: This has to be rewritten at some point to better support more features + * and remove this mess of a class. + * */ override suspend fun setup() { publicSlashCommand(::FeatureManagerArgs) { name = "feature" @@ -64,27 +67,26 @@ class FeatureManageExtension : Extension() { } return@action } - + val gID = this.guild!!.id.toString() + val cID = this.arguments.channel.id.toString() + val channel = this.arguments.channel + val args = this.arguments + Logger.out("${args.feature.readableName} ${args.setStatus.readableName} ${channel.data.name.value}") 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 + FeatureEnum.TIMEPLANNINGFEATURE -> { + var alreadyExists = false transaction { - val entryExists = TimePlanningChannels.select { - (serverid eq gID) and - (channelid eq cID) + alreadyExists = TimePlanningChannels.select { + (TimePlanningChannels.channelid eq gID) and + (TimePlanningChannels.channelid eq cID) }.count() > 0 - - alreadyExists = entryExists } if (!alreadyExists) { transaction { TimePlanningChannels.insert { - it[serverid] = gID - it[channelid] = cID + it[TimePlanningChannels.serverid] = gID + it[TimePlanningChannels.channelid] = cID } get TimePlanningChannels.id } this.respond { @@ -110,28 +112,84 @@ class FeatureManageExtension : Extension() { ) } } + + FeatureEnum.PLANNINGNOTIFIER -> { + var alreadyExists = false + transaction { + alreadyExists = PlanningNotifyerRoles.select { + (PlanningNotifyerRoles.serverid eq gID) and (PlanningNotifyerRoles.channelid eq cID) + }.count() > 0 + } + if (!alreadyExists) { + val hasToVoteRole = this.guild!!.createRole { + this.name = "Hasn't voted [${channel.data.name.value}]" + this.mentionable = true + } + val htvr = hasToVoteRole.id.toString() + + val hasTimeRole = this.guild!!.createRole { + this.name = "Has time [${channel.data.name.value}]" + this.mentionable = true + } + val htr = hasTimeRole.id.toString() + + val wantsNotifsRole = this.guild!!.createRole { + this.name = "Wants to be notified [${channel.data.name.value}]" + this.mentionable = true + } + val wnr = wantsNotifsRole.id.toString() + + transaction { + PlanningNotifyerRoles.insert { + it[PlanningNotifyerRoles.serverid] = gID + it[PlanningNotifyerRoles.channelid] = cID + it[PlanningNotifyerRoles.hastovoteid] = htvr + it[PlanningNotifyerRoles.hastimeroleid] = htr + it[PlanningNotifyerRoles.wantstobenotifid] = wnr + } get PlanningNotifyerRoles.id + } + + this.respond { + embeds.add( + MessageUtil.getEmbed( + Color(0x52E01A), + "200: Success", + "The feature was enabled in channel ${args.channel.data.name.value} with roles ${hasToVoteRole.mention}, ${hasTimeRole.mention} & ${wantsNotifsRole.mention}.", + 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() + FeatureEnum.TIMEPLANNINGFEATURE -> { var alreadyExists = false transaction { - val entryExists = TimePlanningChannels.select { - (serverid eq gID) and - (channelid eq cID) + alreadyExists = TimePlanningChannels.select { + (TimePlanningChannels.serverid eq gID) and + (TimePlanningChannels.channelid eq cID) }.count() > 0 - - alreadyExists = entryExists } if (alreadyExists) { transaction { val matchingEntries = TimePlanningChannels.select { - (serverid eq gID) and - (channelid eq cID) + (TimePlanningChannels.serverid eq gID) and + (TimePlanningChannels.channelid eq cID) }.toList() matchingEntries.forEach { entry -> @@ -161,6 +219,57 @@ class FeatureManageExtension : Extension() { ) } } + + FeatureEnum.PLANNINGNOTIFIER -> { + var alreadyExists = false + transaction { + alreadyExists = PlanningNotifyerRoles.select { + (PlanningNotifyerRoles.serverid eq gID) and (PlanningNotifyerRoles.channelid eq cID) + }.count() > 0 + } + if (alreadyExists) { + var matchingEntries: List = mutableListOf() + transaction { + matchingEntries = PlanningNotifyerRoles.select { + (PlanningNotifyerRoles.serverid eq gID) and + (PlanningNotifyerRoles.channelid eq cID) + }.toList() + } + for (e in matchingEntries) { + this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifyerRoles.hastovoteid]))?.delete() + this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifyerRoles.hastimeroleid]))?.delete() + this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifyerRoles.wantstobenotifid])) + ?.delete() + } + + transaction { + matchingEntries.forEach { entry -> + PlanningNotifyerRoles.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 + ) + ) + } + } } } } @@ -168,7 +277,7 @@ class FeatureManageExtension : Extension() { inner class FeatureManagerArgs : Arguments() { - val feature by enumChoice { + val feature by enumChoice { this.name = "feature" this.description = "The targeted feature" this.typeName = "en_US" diff --git a/src/main/kotlin/net/moonleay/lilJudd/extensions/component/Feature.kt b/src/main/kotlin/net/moonleay/lilJudd/extensions/component/FeatureEnum.kt similarity index 83% rename from src/main/kotlin/net/moonleay/lilJudd/extensions/component/Feature.kt rename to src/main/kotlin/net/moonleay/lilJudd/extensions/component/FeatureEnum.kt index a55c812..7766f39 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/extensions/component/Feature.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/extensions/component/FeatureEnum.kt @@ -20,6 +20,7 @@ package net.moonleay.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"); +enum class FeatureEnum(override val readableName: String) : ChoiceEnum { + TIMEPLANNINGFEATURE("Time Planning Feature"), + PLANNINGNOTIFIER("Planning Notifier") }