forked from DiscordBots/lilJudd
feat: added PlanningNotifyerRoles to db loader, added PlanningNotifyerRoles to feature command, renamed Feature to FeatureEnum
Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
parent
6354b79f30
commit
daa6e17c04
3 changed files with 172 additions and 33 deletions
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
|
@ -27,23 +27,26 @@ import com.kotlindiscord.kord.extensions.types.respond
|
||||||
import com.kotlindiscord.kord.extensions.utils.hasPermission
|
import com.kotlindiscord.kord.extensions.utils.hasPermission
|
||||||
import dev.kord.common.Color
|
import dev.kord.common.Color
|
||||||
import dev.kord.common.entity.Permission
|
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
|
||||||
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.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 net.moonleay.lilJudd.util.MessageUtil
|
||||||
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
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
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
class FeatureManageExtension : Extension() {
|
class FeatureManageExtension : Extension() {
|
||||||
|
|
||||||
override val name = "feature"
|
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() {
|
override suspend fun setup() {
|
||||||
publicSlashCommand(::FeatureManagerArgs) {
|
publicSlashCommand(::FeatureManagerArgs) {
|
||||||
name = "feature"
|
name = "feature"
|
||||||
|
@ -64,27 +67,26 @@ class FeatureManageExtension : Extension() {
|
||||||
}
|
}
|
||||||
return@action
|
return@action
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.arguments.setStatus == EnableOrDisable.ENABLE) {
|
|
||||||
when (this.arguments.feature) {
|
|
||||||
Feature.TIMEPLANNINGFEATURE -> {
|
|
||||||
val gID = this.guild!!.id.toString()
|
val gID = this.guild!!.id.toString()
|
||||||
val cID = this.arguments.channel.id.toString()
|
val cID = this.arguments.channel.id.toString()
|
||||||
|
val channel = this.arguments.channel
|
||||||
val args = this.arguments
|
val args = this.arguments
|
||||||
var alreadyExists: Boolean = false
|
Logger.out("${args.feature.readableName} ${args.setStatus.readableName} ${channel.data.name.value}")
|
||||||
|
if (this.arguments.setStatus == EnableOrDisable.ENABLE) {
|
||||||
|
when (this.arguments.feature) {
|
||||||
|
FeatureEnum.TIMEPLANNINGFEATURE -> {
|
||||||
|
var alreadyExists = false
|
||||||
transaction {
|
transaction {
|
||||||
val entryExists = TimePlanningChannels.select {
|
alreadyExists = TimePlanningChannels.select {
|
||||||
(serverid eq gID) and
|
(TimePlanningChannels.channelid eq gID) and
|
||||||
(channelid eq cID)
|
(TimePlanningChannels.channelid eq cID)
|
||||||
}.count() > 0
|
}.count() > 0
|
||||||
|
|
||||||
alreadyExists = entryExists
|
|
||||||
}
|
}
|
||||||
if (!alreadyExists) {
|
if (!alreadyExists) {
|
||||||
transaction {
|
transaction {
|
||||||
TimePlanningChannels.insert {
|
TimePlanningChannels.insert {
|
||||||
it[serverid] = gID
|
it[TimePlanningChannels.serverid] = gID
|
||||||
it[channelid] = cID
|
it[TimePlanningChannels.channelid] = cID
|
||||||
} get TimePlanningChannels.id
|
} get TimePlanningChannels.id
|
||||||
}
|
}
|
||||||
this.respond {
|
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
|
return@action
|
||||||
}
|
}
|
||||||
//Disable
|
//Disable
|
||||||
when (this.arguments.feature) {
|
when (this.arguments.feature) {
|
||||||
Feature.TIMEPLANNINGFEATURE -> {
|
FeatureEnum.TIMEPLANNINGFEATURE -> {
|
||||||
val gID = this.guild!!.id.toString()
|
|
||||||
val cID = this.arguments.channel.id.toString()
|
|
||||||
var alreadyExists = false
|
var alreadyExists = false
|
||||||
transaction {
|
transaction {
|
||||||
val entryExists = TimePlanningChannels.select {
|
alreadyExists = TimePlanningChannels.select {
|
||||||
(serverid eq gID) and
|
(TimePlanningChannels.serverid eq gID) and
|
||||||
(channelid eq cID)
|
(TimePlanningChannels.channelid eq cID)
|
||||||
}.count() > 0
|
}.count() > 0
|
||||||
|
|
||||||
alreadyExists = entryExists
|
|
||||||
}
|
}
|
||||||
if (alreadyExists) {
|
if (alreadyExists) {
|
||||||
transaction {
|
transaction {
|
||||||
val matchingEntries = TimePlanningChannels.select {
|
val matchingEntries = TimePlanningChannels.select {
|
||||||
(serverid eq gID) and
|
(TimePlanningChannels.serverid eq gID) and
|
||||||
(channelid eq cID)
|
(TimePlanningChannels.channelid eq cID)
|
||||||
}.toList()
|
}.toList()
|
||||||
|
|
||||||
matchingEntries.forEach { entry ->
|
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<ResultRow> = 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() {
|
inner class FeatureManagerArgs : Arguments() {
|
||||||
|
|
||||||
val feature by enumChoice<Feature> {
|
val feature by enumChoice<FeatureEnum> {
|
||||||
this.name = "feature"
|
this.name = "feature"
|
||||||
this.description = "The targeted feature"
|
this.description = "The targeted feature"
|
||||||
this.typeName = "en_US"
|
this.typeName = "en_US"
|
||||||
|
|
|
@ -20,6 +20,7 @@ package net.moonleay.lilJudd.extensions.component
|
||||||
|
|
||||||
import com.kotlindiscord.kord.extensions.commands.application.slash.converters.ChoiceEnum
|
import com.kotlindiscord.kord.extensions.commands.application.slash.converters.ChoiceEnum
|
||||||
|
|
||||||
enum class Feature(override val readableName: String) : ChoiceEnum {
|
enum class FeatureEnum(override val readableName: String) : ChoiceEnum {
|
||||||
TIMEPLANNINGFEATURE("Time Planning Feature");
|
TIMEPLANNINGFEATURE("Time Planning Feature"),
|
||||||
|
PLANNINGNOTIFIER("Planning Notifier")
|
||||||
}
|
}
|
Loading…
Reference in a new issue