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 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.extensions.component.EnableOrDisable import net.moonleay.lilJudd.extensions.component.EnableOrDisable
import net.moonleay.lilJudd.features.component.FeatureEnum 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.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.transactions.transaction
class FeatureManageExtension : Extension() { class FeatureManageExtension : Extension() {
@ -74,211 +68,29 @@ class FeatureManageExtension : Extension() {
val channel = this.arguments.channel val channel = this.arguments.channel
val args = this.arguments val args = this.arguments
Logger.out("${args.feature.readableName} ${args.setStatus.readableName} ${channel.data.name.value}") Logger.out("${args.feature.readableName} ${args.setStatus.readableName} ${channel.data.name.value}")
if (this.arguments.setStatus == EnableOrDisable.ENABLE) { val f = FeatureManager.getFeature(args.feature)
when (this.arguments.feature) { if (f == null) {
FeatureEnum.TIMEPLANNINGFEATURE -> { this.respond {
var alreadyExists = false this.embeds.add(
transaction { MessageUtil.getEmbed(
alreadyExists = TimePlanningChannels.select { Color(0xE0311A),
(TimePlanningChannels.serverid eq gID) and "404: Not Found",
(TimePlanningChannels.channelid eq cID) "The feature you are trying to edit does not exist.",
}.count() > 0 u.asUser().username + "#" + u.asUser().discriminator
} )
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
)
)
}
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 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 -> if (this.arguments.setStatus == EnableOrDisable.ENABLE) {
TimePlanningChannels.deleteWhere { id eq entry[id] } this.respond {
} this.embeds.add(f.enable(u, gID, cID, channel, args))
}
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
)
)
}
} }
return@action
}
this.respond {
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.buildSchedule
import dev.inmo.krontab.doInfinity import dev.inmo.krontab.doInfinity
import dev.kord.common.Color
import dev.kord.common.entity.Snowflake 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.behavior.requestMembers
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.MessageChannel import dev.kord.core.entity.channel.MessageChannel
import dev.kord.gateway.PrivilegedIntent import dev.kord.gateway.PrivilegedIntent
import dev.kord.rest.builder.message.EmbedBuilder
import net.moonleay.lilJudd.Bot import net.moonleay.lilJudd.Bot
import net.moonleay.lilJudd.data.entry.PlanningNotifierRolesData import net.moonleay.lilJudd.data.entry.PlanningNotifierRolesData
import net.moonleay.lilJudd.data.entry.TimePlanningMessagesData import net.moonleay.lilJudd.data.entry.TimePlanningMessagesData
import net.moonleay.lilJudd.data.tables.PlanningNotifierRoles import net.moonleay.lilJudd.data.tables.PlanningNotifierRoles
import net.moonleay.lilJudd.data.tables.TimePlanningMessages 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.features.component.IFeature
import net.moonleay.lilJudd.util.EmbedUtil import net.moonleay.lilJudd.util.EmbedUtil
import net.moonleay.lilJudd.util.Logger import net.moonleay.lilJudd.util.Logger
import net.moonleay.lilJudd.util.MessageUtil
import net.moonleay.lilJudd.util.TimeUtil import net.moonleay.lilJudd.util.TimeUtil
import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import java.time.ZonedDateTime import java.time.ZonedDateTime
@ -135,6 +143,9 @@ object AvailabilityManager : IFeature {
Logger.out("Done! Until tomorrow! <3 ") Logger.out("Done! Until tomorrow! <3 ")
} }
override val feat: FeatureEnum
get() = FeatureEnum.PLANNINGROLES
override suspend fun registerThread() { override suspend fun registerThread() {
Logger.out("Adding availability scheduler...") Logger.out("Adding availability scheduler...")
val scheduler = buildSchedule("0 0 5 * * * 0o *") // 0 0 4 * * * 0o 1w // 0o is UTC val scheduler = buildSchedule("0 0 5 * * * 0o *") // 0 0 4 * * * 0o 1w // 0o is UTC
@ -143,4 +154,113 @@ object AvailabilityManager : IFeature {
this.runThread() 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.inmo.krontab.doInfinity
import dev.kord.common.Color import dev.kord.common.Color
import dev.kord.common.entity.Snowflake import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.UserBehavior
import dev.kord.core.behavior.channel.createMessage import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.MessageChannel import dev.kord.core.entity.channel.MessageChannel
import dev.kord.rest.builder.message.EmbedBuilder
import dev.kord.rest.builder.message.create.actionRow import dev.kord.rest.builder.message.create.actionRow
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import net.moonleay.lilJudd.Bot 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.PlanningNotifierRoles
import net.moonleay.lilJudd.data.tables.TimePlanningChannels import net.moonleay.lilJudd.data.tables.TimePlanningChannels
import net.moonleay.lilJudd.data.tables.TimePlanningMessages 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.features.component.IFeature
import net.moonleay.lilJudd.util.EmbedUtil import net.moonleay.lilJudd.util.EmbedUtil
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 net.moonleay.lilJudd.util.TimeUtil import net.moonleay.lilJudd.util.TimeUtil
import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import java.time.ZoneId import java.time.ZoneId
import java.time.ZonedDateTime import java.time.ZonedDateTime
object TimeManager : IFeature { object TimeManager : IFeature {
override val feat: FeatureEnum
get() = FeatureEnum.TIMEPLANNINGFEATURE
/* /--------------- Seconds /* /--------------- Seconds
| /------------- Minutes | /------------- Minutes
| | /----------- Hours | | /----------- Hours
@ -58,92 +65,175 @@ object TimeManager : IFeature {
Logger.out("Adding message scheduler...") Logger.out("Adding message scheduler...")
val scheduler = buildSchedule("0 0 4 * * * 0o 1w") // 0 0 4 * * * 0o 1w // 0o is UTC val scheduler = buildSchedule("0 0 4 * * * 0o 1w") // 0 0 4 * * * 0o 1w // 0o is UTC
scheduler.doInfinity { scheduler.doInfinity {
Logger.out("Starting to notify...") this.runThread()
// ChannelID, ServerID
val channelList = mutableMapOf<Snowflake, Snowflake>()
// ChannelID, Data
val roleList = mutableMapOf<Snowflake, PlanningNotifierRolesData>()
var msgStr = ""
transaction {
for (tp in TimePlanningChannels.selectAll()) {
channelList[Snowflake(tp[TimePlanningChannels.channelid])] =
Snowflake(tp[TimePlanningChannels.serverid])
Logger.out("Have to notify channel with ID ${tp[TimePlanningChannels.channelid]}.")
}
for (pnr in PlanningNotifierRoles.selectAll()) {
roleList[Snowflake(pnr[PlanningNotifierRoles.channelid])] = PlanningNotifierRolesData(
pnr[PlanningNotifierRoles.serverid],
pnr[PlanningNotifierRoles.channelid],
pnr[PlanningNotifierRoles.hastimeroleid],
pnr[PlanningNotifierRoles.wantstobenotifiedid]
)
Logger.out("Have to ping roles: ${pnr[PlanningNotifierRoles.wantstobenotifiedid]}}")
}
}
Logger.out("${channelList.count()} Channels to notify with ${roleList.count()} Roles to ping!")
for (ch in channelList.keys) {
if (Bot.bot.kordRef.getChannel(ch) == null)
continue // TODO: Check if the channel is valid in another shard
val c = Bot.bot.kordRef.getChannelOf<MessageChannel>(ch)!!
c.createMessage {
if (roleList[ch] != null) {
this.content =
"The weekly planning starts now <@&${Snowflake(roleList[ch]?.wantstobenotifid!!)}>"
}
this.embeds.add(
MessageUtil.getEmbed(
Color(0X4C4645),
"Time Planning Feature",
"Do you have time on the following Days?",
"Automated Message"
)
)
}
delay(2000)
var then = ZonedDateTime.now(ZoneId.of("Europe/Berlin")).withHour(4).withMinute(0).withSecond(0)
repeat(7) {
val msg = c.createMessage {
this.embeds.add(
MessageUtil.getEmbedWithTable(
Color(0X4C4645),
"",
"${then.dayOfWeek.name}, ${then.dayOfMonth}.${then.monthValue}.${then.year} /${it + 1}. weekday",
mapOf(
"Is available" to listOf(),
"May be available" to listOf(),
"Is not available" to listOf()
)
)
)
this.actionRow {
this.components.addAll(EmbedUtil.getTimePlannerButtons().components)
}
}
msgStr += "${it}:${msg.id.value};"
then = then.plusDays(1).withHour(4).withMinute(0).withSecond(0)
Logger.out("Finished sending day $it")
delay(1000)
}
// Save the message ids
transaction {
TimePlanningMessages.insert {
it[TimePlanningMessages.serverid] = c.data.guildId.value.toString()
it[TimePlanningMessages.channelid] = c.id.value.toString()
it[TimePlanningMessages.weekstamp] = TimeUtil.getWeekStamp().toOffsetDateTime().toString()
it[TimePlanningMessages.messageids] = msgStr
} get PlanningNotifierRoles.id
}
Logger.out("Finished with ${c.data.guildId.value}")
}
Logger.out("Done! Until next Monday! <3 ")
} }
} }
private suspend fun runThread() {
Logger.out("Starting to notify...")
// ChannelID, ServerID
val channelList = mutableMapOf<Snowflake, Snowflake>()
// ChannelID, Data
val roleList = mutableMapOf<Snowflake, PlanningNotifierRolesData>()
var msgStr = ""
transaction {
for (tp in TimePlanningChannels.selectAll()) {
channelList[Snowflake(tp[TimePlanningChannels.channelid])] =
Snowflake(tp[TimePlanningChannels.serverid])
Logger.out("Have to notify channel with ID ${tp[TimePlanningChannels.channelid]}.")
}
for (pnr in PlanningNotifierRoles.selectAll()) {
roleList[Snowflake(pnr[PlanningNotifierRoles.channelid])] = PlanningNotifierRolesData(
pnr[PlanningNotifierRoles.serverid],
pnr[PlanningNotifierRoles.channelid],
pnr[PlanningNotifierRoles.hastimeroleid],
pnr[PlanningNotifierRoles.wantstobenotifiedid]
)
Logger.out("Have to ping roles: ${pnr[PlanningNotifierRoles.wantstobenotifiedid]}}")
}
}
Logger.out("${channelList.count()} Channels to notify with ${roleList.count()} Roles to ping!")
for (ch in channelList.keys) {
if (Bot.bot.kordRef.getChannel(ch) == null)
continue // TODO: Check if the channel is valid in another shard
val c = Bot.bot.kordRef.getChannelOf<MessageChannel>(ch)!!
c.createMessage {
if (roleList[ch] != null) {
this.content =
"The weekly planning starts now <@&${Snowflake(roleList[ch]?.wantstobenotifid!!)}>"
}
this.embeds.add(
MessageUtil.getEmbed(
Color(0X4C4645),
"Time Planning Feature",
"Do you have time on the following Days?",
"Automated Message"
)
)
}
delay(2000)
var then = ZonedDateTime.now(ZoneId.of("Europe/Berlin")).withHour(4).withMinute(0).withSecond(0)
repeat(7) {
val msg = c.createMessage {
this.embeds.add(
MessageUtil.getEmbedWithTable(
Color(0X4C4645),
"",
"${then.dayOfWeek.name}, ${then.dayOfMonth}.${then.monthValue}.${then.year} /${it + 1}. weekday",
mapOf(
"Is available" to listOf(),
"May be available" to listOf(),
"Is not available" to listOf()
)
)
)
this.actionRow {
this.components.addAll(EmbedUtil.getTimePlannerButtons().components)
}
}
msgStr += "${it}:${msg.id.value};"
then = then.plusDays(1).withHour(4).withMinute(0).withSecond(0)
Logger.out("Finished sending day $it")
delay(1000)
}
// Save the message ids
transaction {
TimePlanningMessages.insert {
it[TimePlanningMessages.serverid] = c.data.guildId.value.toString()
it[TimePlanningMessages.channelid] = c.id.value.toString()
it[TimePlanningMessages.weekstamp] = TimeUtil.getWeekStamp().toOffsetDateTime().toString()
it[TimePlanningMessages.messageids] = msgStr
} get PlanningNotifierRoles.id
}
Logger.out("Finished with ${c.data.guildId.value}")
}
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 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 { interface IFeature {
val feat: FeatureEnum
suspend fun registerThread() 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
} }