feat: moved all transactions into one package for reuse and simplicity

Signed-off-by: moonleay <contact@moonleay.net>
This commit is contained in:
moonleay 2023-09-15 22:50:16 +02:00
parent e27efe094c
commit 95c22c6bd0
16 changed files with 509 additions and 372 deletions

View file

@ -28,14 +28,10 @@ import dev.kord.core.entity.interaction.ButtonInteraction
import dev.kord.rest.builder.message.modify.embed import dev.kord.rest.builder.message.modify.embed
import net.moonleay.lilJudd.Bot import net.moonleay.lilJudd.Bot
import net.moonleay.lilJudd.buttons.component.IEditButton import net.moonleay.lilJudd.buttons.component.IEditButton
import net.moonleay.lilJudd.data.entry.MatchPlanningDataData import net.moonleay.lilJudd.data.repository.MatchPlanningDataRepository
import net.moonleay.lilJudd.data.tables.MatchPlanningData
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 org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
class AcceptEditButton() : IEditButton { class AcceptEditButton() : IEditButton {
override val id: String = "public.edit.btn.matchmanagement.accept" override val id: String = "public.edit.btn.matchmanagement.accept"
@ -49,33 +45,20 @@ class AcceptEditButton() : IEditButton {
val m = interaction.message val m = interaction.message
val eb = MessageUtil.getAClonedEmbed(m.embeds[0]) val eb = MessageUtil.getAClonedEmbed(m.embeds[0])
var shouldEditButton = false var shouldEditButton = false
lateinit var mpdd: MatchPlanningDataData val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer(
var found = false m.id.value.toLong(),
transaction { interaction.channelId.value.toLong(),
for (pnr in MatchPlanningData.select { guild.id.value.toLong()
MatchPlanningData.messageid eq (interaction.message.id.value.toLong()) and (
MatchPlanningData.serverid eq (guild.id.value.toLong())) and (
MatchPlanningData.channelid eq (interaction.channelId.value.toLong()))
}) {
mpdd = MatchPlanningDataData(
pnr[MatchPlanningData.id],
pnr[MatchPlanningData.serverid],
pnr[MatchPlanningData.channelid],
pnr[MatchPlanningData.matchtype],
pnr[MatchPlanningData.registererid],
pnr[MatchPlanningData.roleid],
pnr[MatchPlanningData.opponentName],
pnr[MatchPlanningData.messageid],
pnr[MatchPlanningData.timestamp],
pnr[MatchPlanningData.jobstr]
) )
found = true if (mpdd == null) {
} Logger.out("mpdd is null")
} return
if (!found || mpdd == null) { }
val role = guild.getRoleOrNull(Snowflake(mpdd.roleID))
if (role == null) {
Logger.out("role is null")
return return
} }
val role = guild.getRoleOrNull(Snowflake(mpdd.roleID)) ?: return
val member = interaction.user.asMember(guild.id) ?: return val member = interaction.user.asMember(guild.id) ?: return
// do the checks and update // do the checks and update
if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) { if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) {

View file

@ -28,12 +28,9 @@ import dev.kord.core.entity.interaction.ButtonInteraction
import dev.kord.rest.builder.message.modify.embed import dev.kord.rest.builder.message.modify.embed
import net.moonleay.lilJudd.Bot import net.moonleay.lilJudd.Bot
import net.moonleay.lilJudd.buttons.component.IEditButton import net.moonleay.lilJudd.buttons.component.IEditButton
import net.moonleay.lilJudd.data.entry.MatchPlanningDataData import net.moonleay.lilJudd.data.repository.MatchPlanningDataRepository
import net.moonleay.lilJudd.data.tables.MatchPlanningData
import net.moonleay.lilJudd.util.EmbedUtil import net.moonleay.lilJudd.util.EmbedUtil
import org.jetbrains.exposed.sql.and import net.moonleay.lilJudd.util.Logger
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
class CancelEditButton : IEditButton { class CancelEditButton : IEditButton {
override val id: String = "public.edit.btn.matchmanagement.cancel" override val id: String = "public.edit.btn.matchmanagement.cancel"
@ -46,33 +43,20 @@ class CancelEditButton : IEditButton {
) { ) {
val m = interaction.message val m = interaction.message
if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) { if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) {
lateinit var mpdd: MatchPlanningDataData val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer(
var found = false m.id.value.toLong(),
transaction { interaction.channelId.value.toLong(),
for (pnr in MatchPlanningData.select { guild.id.value.toLong()
MatchPlanningData.messageid eq (interaction.message.id.value.toLong()) and (
MatchPlanningData.serverid eq (guild.id.value.toLong())) and (
MatchPlanningData.channelid eq (interaction.channelId.value.toLong()))
}) {
mpdd = MatchPlanningDataData(
pnr[MatchPlanningData.id],
pnr[MatchPlanningData.serverid],
pnr[MatchPlanningData.channelid],
pnr[MatchPlanningData.matchtype],
pnr[MatchPlanningData.registererid],
pnr[MatchPlanningData.roleid],
pnr[MatchPlanningData.opponentName],
pnr[MatchPlanningData.messageid],
pnr[MatchPlanningData.timestamp],
pnr[MatchPlanningData.jobstr]
) )
found = true if (mpdd == null) {
} Logger.out("mpdd is null")
} return
if (!found || mpdd == null) { }
val role = guild.getRoleOrNull(Snowflake(mpdd.roleID))
if (role == null) {
Logger.out("role is null")
return return
} }
val role = guild.getRoleOrNull(Snowflake(mpdd.roleID)) ?: return
val member = interaction.user.asMember(guild.id) val member = interaction.user.asMember(guild.id)
if (member.roleIds.contains(Snowflake(mpdd.roleID))) { if (member.roleIds.contains(Snowflake(mpdd.roleID))) {
member.removeRole(role.id) member.removeRole(role.id)

View file

@ -28,14 +28,10 @@ import dev.kord.core.entity.interaction.ButtonInteraction
import dev.kord.rest.builder.message.modify.embed import dev.kord.rest.builder.message.modify.embed
import net.moonleay.lilJudd.Bot import net.moonleay.lilJudd.Bot
import net.moonleay.lilJudd.buttons.component.IEditButton import net.moonleay.lilJudd.buttons.component.IEditButton
import net.moonleay.lilJudd.data.entry.MatchPlanningDataData import net.moonleay.lilJudd.data.repository.MatchPlanningDataRepository
import net.moonleay.lilJudd.data.tables.MatchPlanningData
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 org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
class DeclineEditButton : IEditButton { class DeclineEditButton : IEditButton {
override val id: String = "public.edit.btn.matchmanagement.decline" override val id: String = "public.edit.btn.matchmanagement.decline"
@ -49,33 +45,20 @@ class DeclineEditButton : IEditButton {
val m = interaction.message val m = interaction.message
val eb = MessageUtil.getAClonedEmbed(m.embeds[0]) val eb = MessageUtil.getAClonedEmbed(m.embeds[0])
var shouldEditButton = false var shouldEditButton = false
lateinit var mpdd: MatchPlanningDataData val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer(
var found = false m.id.value.toLong(),
transaction { interaction.channelId.value.toLong(),
for (pnr in MatchPlanningData.select { guild.id.value.toLong()
MatchPlanningData.messageid eq (interaction.message.id.value.toLong()) and (
MatchPlanningData.serverid eq (guild.id.value.toLong())) and (
MatchPlanningData.channelid eq (interaction.channelId.value.toLong()))
}) {
mpdd = MatchPlanningDataData(
pnr[MatchPlanningData.id],
pnr[MatchPlanningData.serverid],
pnr[MatchPlanningData.channelid],
pnr[MatchPlanningData.matchtype],
pnr[MatchPlanningData.registererid],
pnr[MatchPlanningData.roleid],
pnr[MatchPlanningData.opponentName],
pnr[MatchPlanningData.messageid],
pnr[MatchPlanningData.timestamp],
pnr[MatchPlanningData.jobstr]
) )
found = true if (mpdd == null) {
} Logger.out("mpdd is null")
} return
if (!found || mpdd == null) { }
val role = guild.getRoleOrNull(Snowflake(mpdd.roleID))
if (role == null) {
Logger.out("role is null")
return return
} }
val role = guild.getRoleOrNull(Snowflake(mpdd.roleID)) ?: return
val member = interaction.user.asMember(guild.id) ?: return val member = interaction.user.asMember(guild.id) ?: return
if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) { if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) {
if (member.roleIds.contains(Snowflake(mpdd.roleID))) { if (member.roleIds.contains(Snowflake(mpdd.roleID))) {

View file

@ -19,6 +19,7 @@
package net.moonleay.lilJudd.data.entry package net.moonleay.lilJudd.data.entry
data class PlanningNotifierRolesData( data class PlanningNotifierRolesData(
val id: Int, // The id of the entry
val serverID: Long, // The id of the server val serverID: Long, // The id of the server
val channelID: Long, // The id of the channel val channelID: Long, // The id of the channel
val hasTimeRoleID: Long, // The id of the role that has time today val hasTimeRoleID: Long, // The id of the role that has time today

View file

@ -19,6 +19,7 @@
package net.moonleay.lilJudd.data.entry package net.moonleay.lilJudd.data.entry
data class TimePlanningChannelsData( data class TimePlanningChannelsData(
val id: Int,
val serverID: Long, val serverID: Long,
val channelID: Long, val channelID: Long,
) )

View file

@ -19,6 +19,7 @@
package net.moonleay.lilJudd.data.entry package net.moonleay.lilJudd.data.entry
data class TimePlanningMessagesData( data class TimePlanningMessagesData(
val id: Int, // The id of the entry
val serverID: Long, // The discord server id val serverID: Long, // The discord server id
val channelID: Long, // The discord channel id val channelID: Long, // The discord channel id
val weekstamp: Long, // The timestamp of the monday of the week at 4am UTC val weekstamp: Long, // The timestamp of the monday of the week at 4am UTC

View file

@ -0,0 +1,112 @@
/*
* 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.repository
import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
import net.moonleay.lilJudd.data.tables.MatchPlanningData
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
object MatchPlanningDataRepository {
fun getAll(): List<MatchPlanningDataData> {
val dataList = mutableListOf<MatchPlanningDataData>()
transaction {
MatchPlanningData.selectAll().forEach {
dataList.add(
MatchPlanningDataData(
it[MatchPlanningData.id],
it[MatchPlanningData.serverid],
it[MatchPlanningData.channelid],
it[MatchPlanningData.matchtype],
it[MatchPlanningData.registererid],
it[MatchPlanningData.roleid],
it[MatchPlanningData.opponentName],
it[MatchPlanningData.messageid],
it[MatchPlanningData.timestamp],
it[MatchPlanningData.jobstr]
)
)
}
}
return dataList
}
fun get(id: Int): MatchPlanningDataData? =
transaction {
MatchPlanningData.select { MatchPlanningData.id eq id }.firstOrNull()?.let {
MatchPlanningDataData(
it[MatchPlanningData.id],
it[MatchPlanningData.serverid],
it[MatchPlanningData.channelid],
it[MatchPlanningData.matchtype],
it[MatchPlanningData.registererid],
it[MatchPlanningData.roleid],
it[MatchPlanningData.opponentName],
it[MatchPlanningData.messageid],
it[MatchPlanningData.timestamp],
it[MatchPlanningData.jobstr]
)
}
}
fun getFromMessageInChannelInServer(messageID: Long, channelID: Long, serverID: Long): MatchPlanningDataData? =
transaction {
MatchPlanningData.select {
MatchPlanningData.messageid eq (messageID) and (
MatchPlanningData.serverid eq (serverID)) and (
MatchPlanningData.channelid eq (channelID))
}.firstOrNull()?.let {
MatchPlanningDataData(
it[MatchPlanningData.id],
it[MatchPlanningData.serverid],
it[MatchPlanningData.channelid],
it[MatchPlanningData.matchtype],
it[MatchPlanningData.registererid],
it[MatchPlanningData.roleid],
it[MatchPlanningData.opponentName],
it[MatchPlanningData.messageid],
it[MatchPlanningData.timestamp],
it[MatchPlanningData.jobstr]
)
}
}
fun delete(id: Int) {
transaction {
MatchPlanningData.deleteWhere { MatchPlanningData.id eq id }
}
}
fun write(data: MatchPlanningDataData): Int =
transaction {
MatchPlanningData.insert {
it[MatchPlanningData.serverid] = data.serverID
it[MatchPlanningData.channelid] = data.channelID
it[MatchPlanningData.matchtype] = data.matchType
it[MatchPlanningData.registererid] = data.registererID
it[MatchPlanningData.roleid] = data.roleID
it[MatchPlanningData.opponentName] = data.opponentName
it[MatchPlanningData.messageid] = data.messageID
it[MatchPlanningData.timestamp] = data.timestamp
it[MatchPlanningData.jobstr] = data.jobString
} get MatchPlanningData.id
}
}

View file

@ -0,0 +1,104 @@
/*
* 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.repository
import net.moonleay.lilJudd.data.entry.PlanningNotifierRolesData
import net.moonleay.lilJudd.data.tables.PlanningNotifierRoles
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
object PlanningNotifierRolesRepository {
fun getAll(): List<PlanningNotifierRolesData> {
val dataList = mutableListOf<PlanningNotifierRolesData>()
transaction {
for (pnr in PlanningNotifierRoles.selectAll()) {
dataList.add(
PlanningNotifierRolesData(
pnr[PlanningNotifierRoles.id],
pnr[PlanningNotifierRoles.serverid],
pnr[PlanningNotifierRoles.channelid],
pnr[PlanningNotifierRoles.hastimeroleid],
pnr[PlanningNotifierRoles.wantstobenotifiedid]
)
)
}
}
return dataList
}
fun getForChannel(channelID: Long): PlanningNotifierRolesData? =
transaction {
PlanningNotifierRoles.select {
PlanningNotifierRoles.channelid eq channelID
}.firstOrNull()?.let {
PlanningNotifierRolesData(
it[PlanningNotifierRoles.id],
it[PlanningNotifierRoles.serverid],
it[PlanningNotifierRoles.channelid],
it[PlanningNotifierRoles.hastimeroleid],
it[PlanningNotifierRoles.wantstobenotifiedid]
)
}
}
fun getForChannelInServer(channelID: Long, serverID: Long): PlanningNotifierRolesData? =
transaction {
PlanningNotifierRoles.select {
PlanningNotifierRoles.channelid eq channelID and (PlanningNotifierRoles.serverid eq serverID)
}.firstOrNull()?.let {
PlanningNotifierRolesData(
it[PlanningNotifierRoles.id],
it[PlanningNotifierRoles.serverid],
it[PlanningNotifierRoles.channelid],
it[PlanningNotifierRoles.hastimeroleid],
it[PlanningNotifierRoles.wantstobenotifiedid]
)
}
}
fun existsInChannel(channelID: Long): Boolean =
transaction {
PlanningNotifierRoles.select { PlanningNotifierRoles.channelid eq channelID }.count() > 0
}
fun existsInChannelFromSever(channelID: Long, serverID: Long): Boolean =
transaction {
PlanningNotifierRoles.select { PlanningNotifierRoles.channelid eq channelID and (PlanningNotifierRoles.serverid eq serverID) }
.count() > 0
}
fun write(data: PlanningNotifierRolesData) {
transaction {
PlanningNotifierRoles.insert {
it[PlanningNotifierRoles.serverid] = data.serverID
it[PlanningNotifierRoles.channelid] = data.channelID
it[PlanningNotifierRoles.hastimeroleid] = data.hasTimeRoleID
it[PlanningNotifierRoles.wantstobenotifiedid] = data.wantsToBeNotifiedID
} get PlanningNotifierRoles.id
}
}
fun delete(id: Int) {
transaction {
PlanningNotifierRoles.deleteWhere { PlanningNotifierRoles.id eq id }
}
}
}

View file

@ -0,0 +1,69 @@
/*
* 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.repository
import net.moonleay.lilJudd.data.entry.TimePlanningChannelsData
import net.moonleay.lilJudd.data.tables.TimePlanningChannels
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
object TimePlanningChannelsRepository {
fun getAll(): List<TimePlanningChannelsData> {
val dataList = mutableListOf<TimePlanningChannelsData>()
transaction {
for (tp in TimePlanningChannels.selectAll())
dataList.add(
TimePlanningChannelsData(
id = tp[TimePlanningChannels.id],
serverID = tp[TimePlanningChannels.serverid],
channelID = tp[TimePlanningChannels.channelid],
)
)
}
return dataList
}
fun exists(channelID: Long, serverID: Long): Boolean =
transaction {
TimePlanningChannels.select { TimePlanningChannels.channelid eq channelID and (TimePlanningChannels.serverid eq serverID) }
.firstOrNull() != null
}
fun delete(id: Int) {
transaction {
TimePlanningChannels.deleteWhere { TimePlanningChannels.id eq id }
}
}
fun deleteFromChannelInServer(channelID: Long, serverID: Long) {
transaction {
TimePlanningChannels.deleteWhere { TimePlanningChannels.channelid eq channelID and (TimePlanningChannels.serverid eq serverID) }
}
}
fun write(data: TimePlanningChannelsData): Int =
transaction {
TimePlanningChannels.insert {
it[TimePlanningChannels.serverid] = data.serverID
it[TimePlanningChannels.channelid] = data.channelID
} get TimePlanningChannels.id
}
}

View file

@ -0,0 +1,76 @@
/*
* 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.repository
import net.moonleay.lilJudd.data.entry.TimePlanningMessagesData
import net.moonleay.lilJudd.data.tables.TimePlanningMessages
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
object TimePlanningMessagesRepository {
fun write(data: TimePlanningMessagesData): Int =
transaction {
TimePlanningMessages.insert {
it[serverid] = data.serverID
it[channelid] = data.channelID
it[weekstamp] = data.weekstamp
it[messageids] = data.messageIDs
} get TimePlanningMessages.id
}
fun getWeek(stamp: Long): List<TimePlanningMessagesData> {
val dataList = mutableListOf<TimePlanningMessagesData>()
transaction {
for (pnr in TimePlanningMessages.select {
TimePlanningMessages.weekstamp eq (stamp)
}) {
dataList.add(
TimePlanningMessagesData(
pnr[TimePlanningMessages.id],
pnr[TimePlanningMessages.serverid],
pnr[TimePlanningMessages.channelid],
pnr[TimePlanningMessages.weekstamp],
pnr[TimePlanningMessages.messageids]
)
)
}
}
return dataList
}
fun getWeekInChannel(stamp: Long, channelID: Long): TimePlanningMessagesData? =
transaction {
TimePlanningMessages.select {
TimePlanningMessages.weekstamp eq (stamp) and (TimePlanningMessages.channelid eq channelID)
}.firstOrNull()?.let {
TimePlanningMessagesData(
it[TimePlanningMessages.id],
it[TimePlanningMessages.serverid],
it[TimePlanningMessages.channelid],
it[TimePlanningMessages.weekstamp],
it[TimePlanningMessages.messageids]
)
}
}
}

View file

@ -28,7 +28,8 @@ import com.kotlindiscord.kord.extensions.types.respond
import dev.kord.core.behavior.channel.createMessage import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.behavior.createRole import dev.kord.core.behavior.createRole
import dev.kord.rest.builder.message.create.actionRow import dev.kord.rest.builder.message.create.actionRow
import net.moonleay.lilJudd.data.tables.MatchPlanningData import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
import net.moonleay.lilJudd.data.repository.MatchPlanningDataRepository
import net.moonleay.lilJudd.extensions.component.MatchTypes import net.moonleay.lilJudd.extensions.component.MatchTypes
import net.moonleay.lilJudd.jobs.MatchJob import net.moonleay.lilJudd.jobs.MatchJob
import net.moonleay.lilJudd.jobs.component.JobManager import net.moonleay.lilJudd.jobs.component.JobManager
@ -36,9 +37,6 @@ import net.moonleay.lilJudd.util.EmbedColor
import net.moonleay.lilJudd.util.EmbedUtil import net.moonleay.lilJudd.util.EmbedUtil
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.transactions.transaction
import kotlin.properties.Delegates
class MatchExtension : Extension() { class MatchExtension : Extension() {
@ -100,28 +98,28 @@ class MatchExtension : Extension() {
} }
return@action return@action
} }
var tableID by Delegates.notNull<Int>() val tID = MatchPlanningDataRepository.write(
transaction { MatchPlanningDataData(
tableID = MatchPlanningData.insert { 0,
it[MatchPlanningData.serverid] = gID.toLong() gID.toLong(),
it[MatchPlanningData.channelid] = cID.toLong() cID.toLong(),
it[MatchPlanningData.messageid] = msg.id.value.toLong() args.matchType.readableName,
it[MatchPlanningData.matchtype] = args.matchType.readableName m.id.value.toLong(),
it[MatchPlanningData.roleid] = role.id.value.toLong() role.id.value.toLong(),
it[MatchPlanningData.registererid] = m.id.value.toLong() opponent,
it[MatchPlanningData.opponentName] = opponent msg.id.value.toLong(),
it[MatchPlanningData.timestamp] = (zdt.toEpochSecond() * 1000) (zdt.toEpochSecond() * 1000),
it[MatchPlanningData.jobstr] = jobString jobString
} get MatchPlanningData.id )
} )
if (tableID == null) { if (tID == null || tID <= 0) {
return@action // Not saved to db return@action // Not saved to db
} }
JobManager.addJob( JobManager.addJob(
MatchJob( MatchJob(
jobString, jobString,
tableID, tID,
"${args.matchType.readableName}_Vs_${opponent}_[${tableID}]-${gID}_${cID}", "${args.matchType.readableName}_Vs_${opponent}_[${tID}]-${gID}_${cID}",
) )
) )
} }

View file

@ -26,10 +26,9 @@ import dev.kord.common.entity.Permission
import dev.kord.core.behavior.channel.createMessage import dev.kord.core.behavior.channel.createMessage
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.data.tables.TimePlanningMessages import net.moonleay.lilJudd.data.entry.TimePlanningMessagesData
import net.moonleay.lilJudd.data.repository.TimePlanningMessagesRepository
import net.moonleay.lilJudd.util.* import net.moonleay.lilJudd.util.*
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction
import java.time.ZoneId import java.time.ZoneId
import java.time.ZonedDateTime import java.time.ZonedDateTime
@ -101,14 +100,15 @@ class SendPlannerExtension : Extension() {
} }
// Save the message ids // Save the message ids
transaction { TimePlanningMessagesRepository.write(
TimePlanningMessages.insert { TimePlanningMessagesData(
it[TimePlanningMessages.serverid] = c.data.guildId.value?.value!!.toLong() -1,
it[TimePlanningMessages.channelid] = c.id.value.toLong() c.data.guildId.value?.value!!.toLong(),
it[TimePlanningMessages.weekstamp] = (TimeUtil.getWeekStamp().toEpochSecond() * 1000) c.id.value.toLong(),
it[TimePlanningMessages.messageids] = msgStr (TimeUtil.getWeekStamp().toEpochSecond() * 1000),
} get TimePlanningMessages.id msgStr
} )
)
Logger.out("Finished with ${c.data.guildId.value}") Logger.out("Finished with ${c.data.guildId.value}")
} }
} }

View file

@ -32,61 +32,35 @@ 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.repository.PlanningNotifierRolesRepository
import net.moonleay.lilJudd.data.tables.TimePlanningMessages import net.moonleay.lilJudd.data.repository.TimePlanningMessagesRepository
import net.moonleay.lilJudd.extensions.FeatureManageExtension import net.moonleay.lilJudd.extensions.FeatureManageExtension
import net.moonleay.lilJudd.features.component.FeatureEnum 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.* import net.moonleay.lilJudd.util.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
import java.time.ZonedDateTime import java.time.ZonedDateTime
object AvailabilityManager : IFeature { object AvailabilityManager : IFeature {
// This runs during the cronjob. // This runs during the cronjob.
@OptIn(PrivilegedIntent::class)
suspend fun runThread() { suspend fun runThread() {
Logger.out("Starting to update roles...") Logger.out("Starting to update roles...")
// ChannelID, Data // ChannelID, Data
val messageMap = mutableMapOf<Snowflake, TimePlanningMessagesData>() val messages = TimePlanningMessagesRepository.getWeek(TimeUtil.getWeekStamp().toEpochSecond() * 1000)
// ChannelID, Data .associateBy { it.channelID }
val roleMap = mutableMapOf<Long, PlanningNotifierRolesData>() val targetedRoles = PlanningNotifierRolesRepository.getAll().associateBy { it.channelID }
transaction { for (id in messages.keys) {
for (pnr in TimePlanningMessages.select { val snf = Snowflake(id) // snf = Snowflake
TimePlanningMessages.weekstamp eq (TimeUtil.getWeekStamp().toEpochSecond() * 1000) val data = messages[id]!! // this is the data of the table
}) {
messageMap[Snowflake(pnr[TimePlanningMessages.channelid])] =
TimePlanningMessagesData(
pnr[TimePlanningMessages.serverid],
pnr[TimePlanningMessages.channelid],
pnr[TimePlanningMessages.weekstamp],
pnr[TimePlanningMessages.messageids]
)
}
for (pnr in PlanningNotifierRoles.selectAll()) {
roleMap[pnr[PlanningNotifierRoles.channelid]] =
PlanningNotifierRolesData(
pnr[PlanningNotifierRoles.serverid],
pnr[PlanningNotifierRoles.channelid],
pnr[PlanningNotifierRoles.hastimeroleid],
pnr[PlanningNotifierRoles.wantstobenotifiedid]
)
}
}
for (snf in messageMap.keys) { // snf = Snowflake
val data = messageMap[snf]!! // this is the data of the table
if (Bot.bot.kordRef.getChannel(Snowflake(data.channelID)) == null) if (Bot.bot.kordRef.getChannel(Snowflake(data.channelID)) == null)
continue // This channel does not exist anymore. continue // This channel does not exist anymore.
if (roleMap.isEmpty()) { if (targetedRoles.isEmpty()) {
Logger.out("No saved roles. Canceling.") Logger.out("No saved roles. Canceling.")
return return
} }
val roleData = roleMap[data.channelID] // Get the role data val roleData = targetedRoles[data.channelID] // Get the role data
if (roleData == null) { if (roleData == null) {
Logger.out("Role for channel ${data.channelID} does not exist") Logger.out("Role for channel ${data.channelID} does not exist")
continue // this took way to long to find out that this was the issue continue // this took way to long to find out that this was the issue
@ -97,58 +71,27 @@ object AvailabilityManager : IFeature {
} }
suspend fun updateInChannel(snf: Snowflake) { suspend fun updateInChannel(snf: Snowflake) {
lateinit var data: TimePlanningMessagesData val messageData = TimePlanningMessagesRepository.getWeekInChannel(
lateinit var roleData: PlanningNotifierRolesData TimeUtil.getWeekStamp().toEpochSecond() * 1000,
var found1 = false snf.value.toLong()
var found2 = false
for (pnr in TimePlanningMessages.select {
TimePlanningMessages.weekstamp eq (TimeUtil.getWeekStamp()
.toEpochSecond() * 1000) and (TimePlanningMessages.channelid eq (snf.value.toLong()))
}) {
data =
TimePlanningMessagesData(
pnr[TimePlanningMessages.serverid],
pnr[TimePlanningMessages.channelid],
pnr[TimePlanningMessages.weekstamp],
pnr[TimePlanningMessages.messageids]
) )
found1 = true val roleData = PlanningNotifierRolesRepository.getForChannel(snf.value.toLong())
} if (messageData == null) {
for (pnr in PlanningNotifierRoles.select {
PlanningNotifierRoles.channelid eq (snf.value.toLong())
}) {
roleData =
PlanningNotifierRolesData(
pnr[PlanningNotifierRoles.serverid],
pnr[PlanningNotifierRoles.channelid],
pnr[PlanningNotifierRoles.hastimeroleid],
pnr[PlanningNotifierRoles.wantstobenotifiedid]
)
found2 = true
}
if (!found1 || !found2) {
Logger.out("Could not find data for channel ${snf.value}") Logger.out("Could not find data for channel ${snf.value}")
return return
} }
if (Bot.bot.kordRef.getChannel(Snowflake(data.channelID)) == null)
return // This channel does not exist anymore.
if (roleData == null) { if (roleData == null) {
Logger.out("Role for channel ${data.channelID} does not exist") Logger.out("Role for channel ${messageData.channelID} does not exist")
return // this took way to long to find out that this was the issue return // this took way to long to find out that this was the issue
} }
updateInChannel(snf, data, roleData) updateInChannel(snf, messageData, roleData)
} }
@OptIn(PrivilegedIntent::class) @OptIn(PrivilegedIntent::class)
suspend fun updateInChannel(snf: Snowflake, tpmd: TimePlanningMessagesData, pnrd: PlanningNotifierRolesData) { suspend fun updateInChannel(snf: Snowflake, tpmd: TimePlanningMessagesData, pnrd: PlanningNotifierRolesData) {
if (Bot.bot.kordRef.getChannel(Snowflake(tpmd.channelID)) == null) if (Bot.bot.kordRef.getChannel(snf) == null)
return // This channel does not exist anymore. return // This channel does not exist anymore.
val c = val c = Bot.bot.kordRef.getChannelOf<MessageChannel>(snf)!! // Get the channel as MessageChannel
Bot.bot.kordRef.getChannelOf<MessageChannel>(Snowflake(tpmd.channelID))!! // Get the channel as MessageChannel
if (Bot.bot.kordRef.getGuildOrNull(Snowflake(tpmd.serverID)) == null) {
Logger.out("Guild not found.")
return
}
val weekday = ZonedDateTime.now().dayOfWeek // The current week day val weekday = ZonedDateTime.now().dayOfWeek // The current week day
val weekStamp = TimeUtil.getWeekStamp().toEpochSecond() * 1000 // The current week time stamp val weekStamp = TimeUtil.getWeekStamp().toEpochSecond() * 1000 // The current week time stamp
Logger.out("It is week ${weekStamp} and day ${weekday}/${TimeUtil.getDayOfMonthInt(weekday)} of the week.") Logger.out("It is week ${weekStamp} and day ${weekday}/${TimeUtil.getDayOfMonthInt(weekday)} of the week.")
@ -228,13 +171,8 @@ object AvailabilityManager : IFeature {
ch: Channel, ch: Channel,
args: FeatureManageExtension.FeatureManagerArgs args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder { ): EmbedBuilder {
var alreadyExists = false var alreadyExists = PlanningNotifierRolesRepository.existsInChannel(cID)
// Check if the channel and guild already exist in the db // 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) { if (!alreadyExists) {
// Create the roles in Discord // Create the roles in Discord
val hasTimeRole = Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID)).createRole { val hasTimeRole = Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID)).createRole {
@ -250,14 +188,15 @@ object AvailabilityManager : IFeature {
val wnr = wantsNotifsRole.id.value.toLong() val wnr = wantsNotifsRole.id.value.toLong()
// Save the role ids to db // Save the role ids to db
transaction { PlanningNotifierRolesRepository.write(
PlanningNotifierRoles.insert { PlanningNotifierRolesData(
it[PlanningNotifierRoles.serverid] = gID id = -1,
it[PlanningNotifierRoles.channelid] = cID serverID = gID,
it[PlanningNotifierRoles.hastimeroleid] = htr channelID = cID,
it[PlanningNotifierRoles.wantstobenotifiedid] = wnr hasTimeRoleID = htr,
} get PlanningNotifierRoles.id wantsToBeNotifiedID = wnr
} )
)
return MessageUtil.getEmbed( return MessageUtil.getEmbed(
EmbedColor.SUCCESS, EmbedColor.SUCCESS,
@ -284,35 +223,16 @@ object AvailabilityManager : IFeature {
args: FeatureManageExtension.FeatureManagerArgs args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder { ): EmbedBuilder {
// Check if entry exists in db // Check if entry exists in db
var alreadyExists = false if (PlanningNotifierRolesRepository.existsInChannelFromSever(cID, gID)) {
transaction { val entry = PlanningNotifierRolesRepository.getForChannelInServer(cID, gID)!!
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 // delete all entries for this guild and channel combo
for (e in matchingEntries) {
Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID)) Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID))
.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.hastimeroleid]))?.delete() .getRoleOrNull(Snowflake(entry.hasTimeRoleID))?.delete()
Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID)) Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID))
.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.wantstobenotifiedid])) .getRoleOrNull(Snowflake(entry.wantsToBeNotifiedID))?.delete()
?.delete()
}
// delete all found entries // delete all found entries
transaction { PlanningNotifierRolesRepository.delete(entry.id)
matchingEntries.forEach { entry ->
PlanningNotifierRoles.deleteWhere { id eq entry[id] }
}
}
return MessageUtil.getEmbed( return MessageUtil.getEmbed(
EmbedColor.SUCCESS, EmbedColor.SUCCESS,
"200: Success", "200: Success",

View file

@ -21,46 +21,22 @@ package net.moonleay.lilJudd.features
import dev.kord.common.entity.Snowflake import dev.kord.common.entity.Snowflake
import net.moonleay.lilJudd.Bot import net.moonleay.lilJudd.Bot
import net.moonleay.lilJudd.data.entry.MatchPlanningDataData import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
import net.moonleay.lilJudd.data.tables.MatchPlanningData import net.moonleay.lilJudd.data.repository.MatchPlanningDataRepository
import net.moonleay.lilJudd.jobs.MatchJob import net.moonleay.lilJudd.jobs.MatchJob
import net.moonleay.lilJudd.jobs.component.JobManager import net.moonleay.lilJudd.jobs.component.JobManager
import net.moonleay.lilJudd.util.Logger import net.moonleay.lilJudd.util.Logger
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
object MatchManager { object MatchManager {
suspend fun update() { suspend fun update() {
Logger.out("Updating match roles...") Logger.out("Updating match roles...")
val dataList = mutableListOf<MatchPlanningDataData>() val dataList = MatchPlanningDataRepository.getAll()
transaction {
MatchPlanningData.selectAll().forEach {
dataList.add(
MatchPlanningDataData(
it[MatchPlanningData.id],
it[MatchPlanningData.serverid],
it[MatchPlanningData.channelid],
it[MatchPlanningData.matchtype],
it[MatchPlanningData.registererid],
it[MatchPlanningData.roleid],
it[MatchPlanningData.opponentName],
it[MatchPlanningData.messageid],
it[MatchPlanningData.timestamp],
it[MatchPlanningData.jobstr]
)
)
}
}
for (data in dataList) { for (data in dataList) {
Logger.out("Checking match role ${data.id}...") Logger.out("Checking match role ${data.id}...")
if (data.timestamp.toLong() < System.currentTimeMillis()) { if (data.timestamp < System.currentTimeMillis()) {
Logger.out("Match role ${data.id} is expired, removing...") Logger.out("Match role ${data.id} is expired, removing...")
this.removeRoleFromGuild(data.serverID, data.roleID) this.removeRoleFromGuild(data.serverID, data.roleID)
transaction { MatchPlanningDataRepository.delete(data.id)
MatchPlanningData.deleteWhere { MatchPlanningData.messageid eq data.messageID }
}
continue continue
} }
this.registerJob(data) this.registerJob(data)

View file

@ -29,17 +29,18 @@ 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
import net.moonleay.lilJudd.data.entry.PlanningNotifierRolesData import net.moonleay.lilJudd.data.entry.TimePlanningChannelsData
import net.moonleay.lilJudd.data.tables.PlanningNotifierRoles import net.moonleay.lilJudd.data.entry.TimePlanningMessagesData
import net.moonleay.lilJudd.data.tables.TimePlanningChannels import net.moonleay.lilJudd.data.repository.PlanningNotifierRolesRepository
import net.moonleay.lilJudd.data.tables.TimePlanningMessages import net.moonleay.lilJudd.data.repository.TimePlanningChannelsRepository
import net.moonleay.lilJudd.data.repository.TimePlanningMessagesRepository
import net.moonleay.lilJudd.extensions.FeatureManageExtension import net.moonleay.lilJudd.extensions.FeatureManageExtension
import net.moonleay.lilJudd.features.component.FeatureEnum 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.* import net.moonleay.lilJudd.util.EmbedColor
import org.jetbrains.exposed.sql.* import net.moonleay.lilJudd.util.EmbedUtil
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import net.moonleay.lilJudd.util.Logger
import org.jetbrains.exposed.sql.transactions.transaction import net.moonleay.lilJudd.util.MessageUtil
import java.time.ZoneId import java.time.ZoneId
import java.time.ZonedDateTime import java.time.ZonedDateTime
@ -60,41 +61,23 @@ object TimeManager : IFeature {
private suspend fun runThread() { private suspend fun runThread() {
Logger.out("Starting to notify...") Logger.out("Starting to notify...")
// ChannelID -> Data
val targetedChannels = TimePlanningChannelsRepository.getAll().associateBy { it.channelID }
val targetedRoles = PlanningNotifierRolesRepository.getAll().associateBy { it.channelID }
// ChannelID, ServerID lateinit var msgStr: String
val channelList = mutableMapOf<Snowflake, Snowflake>()
// ChannelID, Data
val roleMap = mutableMapOf<Snowflake, PlanningNotifierRolesData>()
var msgStr = "" Logger.out("${targetedChannels.count()} Channels to notify with ${targetedRoles.count()} Roles to ping!")
for (ch2 in targetedChannels.keys) {
transaction { val ch = Snowflake(ch2)
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()) {
roleMap[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 ${roleMap.count()} Roles to ping!")
for (ch in channelList.keys) {
if (Bot.bot.kordRef.getChannel(ch) == null) if (Bot.bot.kordRef.getChannel(ch) == null)
continue // TODO: Check if the channel is valid in another shard continue // TODO: Check if the channel is valid in another shard
val c = Bot.bot.kordRef.getChannelOf<MessageChannel>(ch)!! val c = Bot.bot.kordRef.getChannelOf<MessageChannel>(ch)!!
msgStr = "" msgStr = ""
if (roleMap != null && roleMap.keys.contains(ch) && roleMap[ch] != null) { if (targetedRoles != null && targetedRoles.keys.contains(ch2) && targetedRoles[ch2] != null) {
c.createMessage { c.createMessage {
this.content = this.content =
"The weekly planning starts now <@&${Snowflake(roleMap[ch]?.wantsToBeNotifiedID!!)}>" "The weekly planning starts now <@&${Snowflake(targetedRoles[ch2]?.wantsToBeNotifiedID!!)}>"
this.embeds.add( this.embeds.add(
MessageUtil.getEmbed( MessageUtil.getEmbed(
EmbedColor.INFO, EmbedColor.INFO,
@ -145,14 +128,15 @@ object TimeManager : IFeature {
} }
// Save the message ids // Save the message ids
transaction { TimePlanningMessagesRepository.write(
TimePlanningMessages.insert { TimePlanningMessagesData(
it[TimePlanningMessages.serverid] = c.data.guildId.value?.value!!.toLong() id = -1,
it[TimePlanningMessages.channelid] = c.id.value.toLong() serverID = c.data.guildId.value?.value!!.toLong(),
it[TimePlanningMessages.weekstamp] = (TimeUtil.getWeekStamp().toEpochSecond() * 1000) channelID = c.data.id.value.toLong(),
it[TimePlanningMessages.messageids] = msgStr weekstamp = then.minusDays(7).toEpochSecond(),
} get TimePlanningMessages.id messageIDs = msgStr
} )
)
Logger.out("Finished with ${c.data.guildId.value}") Logger.out("Finished with ${c.data.guildId.value}")
} }
Logger.out("Done! Until next Monday! <3 ") Logger.out("Done! Until next Monday! <3 ")
@ -165,20 +149,8 @@ object TimeManager : IFeature {
ch: Channel, ch: Channel,
args: FeatureManageExtension.FeatureManagerArgs args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder { ): EmbedBuilder {
var alreadyExists = false if (!TimePlanningChannelsRepository.exists(cID, gID)) {
transaction { TimePlanningChannelsRepository.write(TimePlanningChannelsData(id = -1, serverID = gID, channelID = cID))
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( return MessageUtil.getEmbed(
EmbedColor.SUCCESS, EmbedColor.SUCCESS,
"200: Success", "200: Success",
@ -202,25 +174,9 @@ object TimeManager : IFeature {
args: FeatureManageExtension.FeatureManagerArgs args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder { ): EmbedBuilder {
// Check if entry exists in db // Check if entry exists in db
var alreadyExists = false if (TimePlanningChannelsRepository.exists(cID, gID)) {
transaction {
alreadyExists = TimePlanningChannels.select {
(TimePlanningChannels.serverid eq gID) and
(TimePlanningChannels.channelid eq cID)
}.count() > 0
}
if (alreadyExists) {
// delete all entrys for this channel // delete all entrys for this channel
transaction { TimePlanningChannelsRepository.deleteFromChannelInServer(cID, gID)
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( return MessageUtil.getEmbed(
EmbedColor.SUCCESS, EmbedColor.SUCCESS,
"200: Success", "200: Success",

View file

@ -22,16 +22,11 @@ import dev.inmo.krontab.KronScheduler
import dev.kord.common.entity.Snowflake import dev.kord.common.entity.Snowflake
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import net.moonleay.lilJudd.Bot import net.moonleay.lilJudd.Bot
import net.moonleay.lilJudd.data.entry.MatchPlanningDataData import net.moonleay.lilJudd.data.repository.MatchPlanningDataRepository
import net.moonleay.lilJudd.data.tables.MatchPlanningData
import net.moonleay.lilJudd.jobs.component.CronjobType import net.moonleay.lilJudd.jobs.component.CronjobType
import net.moonleay.lilJudd.jobs.component.ICronjob import net.moonleay.lilJudd.jobs.component.ICronjob
import net.moonleay.lilJudd.jobs.component.JobManager import net.moonleay.lilJudd.jobs.component.JobManager
import net.moonleay.lilJudd.util.Logger import net.moonleay.lilJudd.util.Logger
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
class MatchJob( class MatchJob(
override val jobIncoming: String, override val jobIncoming: String,
@ -48,25 +43,7 @@ class MatchJob(
* */ * */
override suspend fun jobFunction() { override suspend fun jobFunction() {
Logger.out("Running MatchJob \"${this.jobName}\"") Logger.out("Running MatchJob \"${this.jobName}\"")
lateinit var mpdd: MatchPlanningDataData val mpdd = MatchPlanningDataRepository.get(tableId)!!
transaction {
for (pnr in MatchPlanningData.select {
MatchPlanningData.id eq (tableId)
}) {
mpdd = MatchPlanningDataData(
pnr[MatchPlanningData.id],
pnr[MatchPlanningData.serverid],
pnr[MatchPlanningData.channelid],
pnr[MatchPlanningData.matchtype],
pnr[MatchPlanningData.registererid],
pnr[MatchPlanningData.roleid],
pnr[MatchPlanningData.opponentName],
pnr[MatchPlanningData.messageid],
pnr[MatchPlanningData.timestamp],
pnr[MatchPlanningData.jobstr]
)
}
}
val guild = Bot.bot.kordRef.getGuildOrNull(Snowflake(mpdd.serverID)) val guild = Bot.bot.kordRef.getGuildOrNull(Snowflake(mpdd.serverID))
if (guild == null) { if (guild == null) {
Logger.out("Guild not found.") Logger.out("Guild not found.")
@ -78,11 +55,7 @@ class MatchJob(
return return
} }
r.delete() r.delete()
transaction { MatchPlanningDataRepository.delete(tableId)
MatchPlanningData.deleteWhere {
MatchPlanningData.id eq (tableId)
}
}
Logger.out("MatchJob \"${this.jobName}\" finished.") Logger.out("MatchJob \"${this.jobName}\" finished.")
Logger.out("Killing job \"${this.jobName}\"..") Logger.out("Killing job \"${this.jobName}\"..")
JobManager.killJob(this) JobManager.killJob(this)