feat: improved feature system

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-06-29 08:25:37 +02:00
parent e8667dca5b
commit e64569c748
5 changed files with 371 additions and 297 deletions

View file

@ -27,17 +27,11 @@ 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.PlanningNotifierRoles
import net.moonleay.lilJudd.data.tables.TimePlanningChannels
import net.moonleay.lilJudd.extensions.component.EnableOrDisable
import net.moonleay.lilJudd.features.component.FeatureEnum
import net.moonleay.lilJudd.features.component.FeatureManager
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.transactions.transaction
class FeatureManageExtension : Extension() {
@ -74,211 +68,29 @@ class FeatureManageExtension : Extension() {
val channel = this.arguments.channel
val args = this.arguments
Logger.out("${args.feature.readableName} ${args.setStatus.readableName} ${channel.data.name.value}")
val f = FeatureManager.getFeature(args.feature)
if (f == null) {
this.respond {
this.embeds.add(
MessageUtil.getEmbed(
Color(0xE0311A),
"404: Not Found",
"The feature you are trying to edit does not exist.",
u.asUser().username + "#" + u.asUser().discriminator
)
)
}
return@action
}
if (this.arguments.setStatus == EnableOrDisable.ENABLE) {
when (this.arguments.feature) {
FeatureEnum.TIMEPLANNINGFEATURE -> {
var alreadyExists = false
transaction {
alreadyExists = TimePlanningChannels.select {
(TimePlanningChannels.serverid eq gID) and
(TimePlanningChannels.channelid eq cID)
}.count() > 0
}
if (!alreadyExists) {
transaction {
TimePlanningChannels.insert {
it[TimePlanningChannels.serverid] = gID
it[TimePlanningChannels.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.value}",
u.asUser().username + "#" + u.asUser().discriminator
)
)
this.embeds.add(f.enable(u, gID, cID, channel, args))
}
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
)
)
}
}
FeatureEnum.PLANNINGROLES -> {
var alreadyExists = false
// Check if the channel and guild already exist in the db
transaction {
alreadyExists = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
}.count() > 0
}
if (!alreadyExists) {
// Create the roles in Discord
val hasTimeRole = this.guild!!.createRole {
this.name = "available [${channel.data.name.value}]"
this.mentionable = true
}
val htr = hasTimeRole.id.toString()
val wantsNotifsRole = this.guild!!.createRole {
this.name = "notifications [${channel.data.name.value}]"
this.mentionable = true
}
val wnr = wantsNotifsRole.id.toString()
// Save the role ids to db
transaction {
PlanningNotifierRoles.insert {
it[PlanningNotifierRoles.serverid] = gID
it[PlanningNotifierRoles.channelid] = cID
it[PlanningNotifierRoles.hastimeroleid] = htr
it[PlanningNotifierRoles.wantstobenotifiedid] = wnr
} get PlanningNotifierRoles.id
}
this.respond {
embeds.add(
MessageUtil.getEmbed(
Color(0x52E01A),
"200: Success",
"The feature was enabled in channel ${args.channel.data.name.value} with roles ${hasTimeRole.mention} & ${wantsNotifsRole.mention}.",
u.asUser().username + "#" + u.asUser().discriminator
)
)
}
return@action
}
// They exist, do not add them
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) {
FeatureEnum.TIMEPLANNINGFEATURE -> {
// Check if entry exists in db
var alreadyExists = false
transaction {
alreadyExists = TimePlanningChannels.select {
(TimePlanningChannels.serverid eq gID) and
(TimePlanningChannels.channelid eq cID)
}.count() > 0
}
if (alreadyExists) {
// delete all entrys for this channel
transaction {
val matchingEntries = TimePlanningChannels.select {
(TimePlanningChannels.serverid eq gID) and
(TimePlanningChannels.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
}
// Do nothing; not in db
this.respond {
embeds.add(
MessageUtil.getEmbed(
Color(0xE0311A),
"403: Forbidden",
"The feature is already disabled in this channel.",
u.asUser().username + "#" + u.asUser().discriminator
)
)
}
}
FeatureEnum.PLANNINGROLES -> {
// Check if entry exists in db
var alreadyExists = false
transaction {
alreadyExists = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
}.count() > 0
}
if (alreadyExists) {
var matchingEntries: List<ResultRow> = mutableListOf()
transaction {
matchingEntries = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and
(PlanningNotifierRoles.channelid eq cID)
}.toList()
}
// delete all entries for this guild and channel combo
for (e in matchingEntries) {
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.hastimeroleid]))?.delete()
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.wantstobenotifiedid]))
?.delete()
}
// delete all found entries
transaction {
matchingEntries.forEach { entry ->
PlanningNotifierRoles.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
}
// not in db, do nothing
this.respond {
embeds.add(
MessageUtil.getEmbed(
Color(0xE0311A),
"403: Forbidden",
"The feature is already disabled in this channel.",
u.asUser().username + "#" + u.asUser().discriminator
)
)
}
}
this.embeds.add(f.disable(u, gID, cID, channel, args))
}
}
}

View file

@ -20,21 +20,29 @@ package net.moonleay.lilJudd.features
import dev.inmo.krontab.buildSchedule
import dev.inmo.krontab.doInfinity
import dev.kord.common.Color
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.UserBehavior
import dev.kord.core.behavior.createRole
import dev.kord.core.behavior.requestMembers
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.MessageChannel
import dev.kord.gateway.PrivilegedIntent
import dev.kord.rest.builder.message.EmbedBuilder
import net.moonleay.lilJudd.Bot
import net.moonleay.lilJudd.data.entry.PlanningNotifierRolesData
import net.moonleay.lilJudd.data.entry.TimePlanningMessagesData
import net.moonleay.lilJudd.data.tables.PlanningNotifierRoles
import net.moonleay.lilJudd.data.tables.TimePlanningMessages
import net.moonleay.lilJudd.extensions.FeatureManageExtension
import net.moonleay.lilJudd.features.component.FeatureEnum
import net.moonleay.lilJudd.features.component.IFeature
import net.moonleay.lilJudd.util.EmbedUtil
import net.moonleay.lilJudd.util.Logger
import net.moonleay.lilJudd.util.MessageUtil
import net.moonleay.lilJudd.util.TimeUtil
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
import java.time.ZonedDateTime
@ -135,6 +143,9 @@ object AvailabilityManager : IFeature {
Logger.out("Done! Until tomorrow! <3 ")
}
override val feat: FeatureEnum
get() = FeatureEnum.PLANNINGROLES
override suspend fun registerThread() {
Logger.out("Adding availability scheduler...")
val scheduler = buildSchedule("0 0 5 * * * 0o *") // 0 0 4 * * * 0o 1w // 0o is UTC
@ -143,4 +154,113 @@ object AvailabilityManager : IFeature {
this.runThread()
}
}
override suspend fun enable(
u: UserBehavior,
gID: String,
cID: String,
ch: Channel,
args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder {
var alreadyExists = false
// Check if the channel and guild already exist in the db
transaction {
alreadyExists = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
}.count() > 0
}
if (!alreadyExists) {
// Create the roles in Discord
val hasTimeRole = Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID)).createRole {
this.name = "available [${ch.data.name.value}]"
this.mentionable = true
}
val htr = hasTimeRole.id.toString()
val wantsNotifsRole = Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID)).createRole {
this.name = "notifications [${ch.data.name.value}]"
this.mentionable = true
}
val wnr = wantsNotifsRole.id.toString()
// Save the role ids to db
transaction {
PlanningNotifierRoles.insert {
it[PlanningNotifierRoles.serverid] = gID
it[PlanningNotifierRoles.channelid] = cID
it[PlanningNotifierRoles.hastimeroleid] = htr
it[PlanningNotifierRoles.wantstobenotifiedid] = wnr
} get PlanningNotifierRoles.id
}
return MessageUtil.getEmbed(
Color(0x52E01A),
"200: Success",
"The feature was enabled in channel ${args.channel.data.name.value} with roles ${hasTimeRole.mention} & ${wantsNotifsRole.mention}.",
u.asUser().username + "#" + u.asUser().discriminator
)
}
// They exist, do not add them
return MessageUtil.getEmbed(
Color(0xE0311A),
"403: Forbidden",
"The feature is already enabled in this channel.",
u.asUser().username + "#" + u.asUser().discriminator
)
}
override suspend fun disable(
u: UserBehavior,
gID: String,
cID: String,
ch: Channel,
args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder {
// Check if entry exists in db
var alreadyExists = false
transaction {
alreadyExists = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
}.count() > 0
}
if (alreadyExists) {
var matchingEntries: List<ResultRow> = mutableListOf()
transaction {
matchingEntries = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and
(PlanningNotifierRoles.channelid eq cID)
}.toList()
}
// delete all entries for this guild and channel combo
for (e in matchingEntries) {
Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID))
.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.hastimeroleid]))?.delete()
Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID))
.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.wantstobenotifiedid]))
?.delete()
}
// delete all found entries
transaction {
matchingEntries.forEach { entry ->
PlanningNotifierRoles.deleteWhere { id eq entry[id] }
}
}
return MessageUtil.getEmbed(
Color(0x52E019),
"200: Success",
"The feature was disabled.",
u.asUser().username + "#" + u.asUser().discriminator
)
}
// not in db, do nothing
return MessageUtil.getEmbed(
Color(0xE0311A),
"403: Forbidden",
"The feature is already disabled in this channel.",
u.asUser().username + "#" + u.asUser().discriminator
)
}
}

View file

@ -22,8 +22,11 @@ import dev.inmo.krontab.buildSchedule
import dev.inmo.krontab.doInfinity
import dev.kord.common.Color
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.UserBehavior
import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.MessageChannel
import dev.kord.rest.builder.message.EmbedBuilder
import dev.kord.rest.builder.message.create.actionRow
import kotlinx.coroutines.delay
import net.moonleay.lilJudd.Bot
@ -31,19 +34,23 @@ import net.moonleay.lilJudd.data.entry.PlanningNotifierRolesData
import net.moonleay.lilJudd.data.tables.PlanningNotifierRoles
import net.moonleay.lilJudd.data.tables.TimePlanningChannels
import net.moonleay.lilJudd.data.tables.TimePlanningMessages
import net.moonleay.lilJudd.extensions.FeatureManageExtension
import net.moonleay.lilJudd.features.component.FeatureEnum
import net.moonleay.lilJudd.features.component.IFeature
import net.moonleay.lilJudd.util.EmbedUtil
import net.moonleay.lilJudd.util.Logger
import net.moonleay.lilJudd.util.MessageUtil
import net.moonleay.lilJudd.util.TimeUtil
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
import java.time.ZoneId
import java.time.ZonedDateTime
object TimeManager : IFeature {
override val feat: FeatureEnum
get() = FeatureEnum.TIMEPLANNINGFEATURE
/* /--------------- Seconds
| /------------- Minutes
| | /----------- Hours
@ -58,6 +65,11 @@ object TimeManager : IFeature {
Logger.out("Adding message scheduler...")
val scheduler = buildSchedule("0 0 4 * * * 0o 1w") // 0 0 4 * * * 0o 1w // 0o is UTC
scheduler.doInfinity {
this.runThread()
}
}
private suspend fun runThread() {
Logger.out("Starting to notify...")
@ -145,5 +157,83 @@ object TimeManager : IFeature {
}
Logger.out("Done! Until next Monday! <3 ")
}
override suspend fun enable(
u: UserBehavior,
gID: String,
cID: String,
ch: Channel,
args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder {
var alreadyExists = false
transaction {
alreadyExists = TimePlanningChannels.select {
(TimePlanningChannels.serverid eq gID) and
(TimePlanningChannels.channelid eq cID)
}.count() > 0
}
if (!alreadyExists) {
transaction {
TimePlanningChannels.insert {
it[TimePlanningChannels.serverid] = gID
it[TimePlanningChannels.channelid] = cID
} get TimePlanningChannels.id
}
return MessageUtil.getEmbed(
Color(0x52E01A),
"200: Success",
"The feature was enabled in channel ${args.channel.data.name.value}",
u.asUser().username + "#" + u.asUser().discriminator
)
}
return MessageUtil.getEmbed(
Color(0xE0311A),
"403: Forbidden",
"The feature is already enabled in this channel.",
u.asUser().username + "#" + u.asUser().discriminator
)
}
override suspend fun disable(
u: UserBehavior,
gID: String,
cID: String,
ch: Channel,
args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder {
// Check if entry exists in db
var alreadyExists = false
transaction {
alreadyExists = TimePlanningChannels.select {
(TimePlanningChannels.serverid eq gID) and
(TimePlanningChannels.channelid eq cID)
}.count() > 0
}
if (alreadyExists) {
// delete all entrys for this channel
transaction {
val matchingEntries = TimePlanningChannels.select {
(TimePlanningChannels.serverid eq gID) and
(TimePlanningChannels.channelid eq cID)
}.toList()
matchingEntries.forEach { entry ->
TimePlanningChannels.deleteWhere { id eq entry[id] }
}
}
return MessageUtil.getEmbed(
Color(0x52E019),
"200: Success",
"The feature was disabled.",
u.asUser().username + "#" + u.asUser().discriminator
)
}
// Do nothing; not in db
return MessageUtil.getEmbed(
Color(0xE0311A),
"403: Forbidden",
"The feature is already disabled in this channel.",
u.asUser().username + "#" + u.asUser().discriminator
)
}
}

View file

@ -0,0 +1,30 @@
/*
* 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.features.component
import net.moonleay.lilJudd.features.AvailabilityManager
import net.moonleay.lilJudd.features.TimeManager
object FeatureManager {
val features = mutableListOf(AvailabilityManager, TimeManager) // Stores all features
fun getFeature(feat: FeatureEnum): IFeature? {
return features.find { it.feat == feat }
}
}

View file

@ -18,6 +18,28 @@
package net.moonleay.lilJudd.features.component
import dev.kord.core.behavior.UserBehavior
import dev.kord.core.entity.channel.Channel
import dev.kord.rest.builder.message.EmbedBuilder
import net.moonleay.lilJudd.extensions.FeatureManageExtension
interface IFeature {
val feat: FeatureEnum
suspend fun registerThread()
suspend fun enable(
u: UserBehavior,
gID: String,
cID: String,
ch: Channel,
args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder
suspend fun disable(
u: UserBehavior,
gID: String,
cID: String,
ch: Channel,
args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder
}