feat: (re)added PlanningRolesFeature

fix: fixed being able to register TimePlanningFeature multiple times in the same channel
chore: added comments

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-06-28 22:50:34 +02:00
parent ae0d67f3fd
commit 5c488fa91d

View file

@ -27,16 +27,16 @@ 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.PlanningNotifierRoles
import net.moonleay.lilJudd.data.tables.TimePlanningChannels import net.moonleay.lilJudd.data.tables.TimePlanningChannels
import net.moonleay.lilJudd.extensions.component.EnableOrDisable import net.moonleay.lilJudd.extensions.component.EnableOrDisable
import net.moonleay.lilJudd.extensions.component.FeatureEnum import net.moonleay.lilJudd.features.component.FeatureEnum
import net.moonleay.lilJudd.util.Logger 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() {
@ -80,7 +80,7 @@ class FeatureManageExtension : Extension() {
var alreadyExists = false var alreadyExists = false
transaction { transaction {
alreadyExists = TimePlanningChannels.select { alreadyExists = TimePlanningChannels.select {
(TimePlanningChannels.channelid eq gID) and (TimePlanningChannels.serverid eq gID) and
(TimePlanningChannels.channelid eq cID) (TimePlanningChannels.channelid eq cID)
}.count() > 0 }.count() > 0
} }
@ -116,9 +116,9 @@ class FeatureManageExtension : Extension() {
} }
/* FeatureEnum.PLANNINGROLES -> {
FeatureEnum.PLANNINGNOTIFIER -> {
var alreadyExists = false var alreadyExists = false
// Check if the channel and guild already exist in the db
transaction { transaction {
alreadyExists = PlanningNotifierRoles.select { alreadyExists = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID) (PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
@ -126,6 +126,7 @@ class FeatureManageExtension : Extension() {
} }
if (!alreadyExists) { if (!alreadyExists) {
// Create the roles in Discord
val hasTimeRole = this.guild!!.createRole { val hasTimeRole = this.guild!!.createRole {
this.name = "available [${channel.data.name.value}]" this.name = "available [${channel.data.name.value}]"
this.mentionable = true this.mentionable = true
@ -138,12 +139,13 @@ class FeatureManageExtension : Extension() {
} }
val wnr = wantsNotifsRole.id.toString() val wnr = wantsNotifsRole.id.toString()
// Save the role ids to db
transaction { transaction {
PlanningNotifierRoles.insert { PlanningNotifierRoles.insert {
it[PlanningNotifierRoles.serverid] = gID it[PlanningNotifierRoles.serverid] = gID
it[PlanningNotifierRoles.channelid] = cID it[PlanningNotifierRoles.channelid] = cID
it[PlanningNotifierRoles.hastimeroleid] = htr it[PlanningNotifierRoles.hastimeroleid] = htr
it[PlanningNotifierRoles.wantstobenotifid] = wnr it[PlanningNotifierRoles.wantstobenotifiedid] = wnr
} get PlanningNotifierRoles.id } get PlanningNotifierRoles.id
} }
@ -159,6 +161,8 @@ class FeatureManageExtension : Extension() {
} }
return@action return@action
} }
// They exist, do not add them
this.respond { this.respond {
embeds.add( embeds.add(
MessageUtil.getEmbed( MessageUtil.getEmbed(
@ -169,13 +173,14 @@ class FeatureManageExtension : Extension() {
) )
) )
} }
} */ }
} }
return@action return@action
} }
//Disable //Disable
when (this.arguments.feature) { when (this.arguments.feature) {
FeatureEnum.TIMEPLANNINGFEATURE -> { FeatureEnum.TIMEPLANNINGFEATURE -> {
// Check if entry exists in db
var alreadyExists = false var alreadyExists = false
transaction { transaction {
alreadyExists = TimePlanningChannels.select { alreadyExists = TimePlanningChannels.select {
@ -184,6 +189,7 @@ class FeatureManageExtension : Extension() {
}.count() > 0 }.count() > 0
} }
if (alreadyExists) { if (alreadyExists) {
// delete all entrys for this channel
transaction { transaction {
val matchingEntries = TimePlanningChannels.select { val matchingEntries = TimePlanningChannels.select {
(TimePlanningChannels.serverid eq gID) and (TimePlanningChannels.serverid eq gID) and
@ -206,6 +212,7 @@ class FeatureManageExtension : Extension() {
} }
return@action return@action
} }
// Do nothing; not in db
this.respond { this.respond {
embeds.add( embeds.add(
MessageUtil.getEmbed( MessageUtil.getEmbed(
@ -218,8 +225,9 @@ class FeatureManageExtension : Extension() {
} }
} }
/*
FeatureEnum.PLANNINGNOTIFIER -> { FeatureEnum.PLANNINGROLES -> {
// Check if entry exists in db
var alreadyExists = false var alreadyExists = false
transaction { transaction {
alreadyExists = PlanningNotifierRoles.select { alreadyExists = PlanningNotifierRoles.select {
@ -234,12 +242,14 @@ class FeatureManageExtension : Extension() {
(PlanningNotifierRoles.channelid eq cID) (PlanningNotifierRoles.channelid eq cID)
}.toList() }.toList()
} }
// delete all entries for this guild and channel combo
for (e in matchingEntries) { for (e in matchingEntries) {
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.hastimeroleid]))?.delete() this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.hastimeroleid]))?.delete()
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.wantstobenotifid])) this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.wantstobenotifiedid]))
?.delete() ?.delete()
} }
// delete all found entries
transaction { transaction {
matchingEntries.forEach { entry -> matchingEntries.forEach { entry ->
PlanningNotifierRoles.deleteWhere { id eq entry[id] } PlanningNotifierRoles.deleteWhere { id eq entry[id] }
@ -257,6 +267,7 @@ class FeatureManageExtension : Extension() {
} }
return@action return@action
} }
// not in db, do nothing
this.respond { this.respond {
embeds.add( embeds.add(
MessageUtil.getEmbed( MessageUtil.getEmbed(
@ -267,7 +278,7 @@ class FeatureManageExtension : Extension() {
) )
) )
} }
} */ }
} }
} }
} }