From 39c88677228032b25533a0656d2d2b8c1f0195a7 Mon Sep 17 00:00:00 2001 From: moonleay Date: Fri, 15 Sep 2023 22:50:16 +0200 Subject: [PATCH] feat: moved all transactions into one package for reuse and simplicity Signed-off-by: moonleay --- .../buttons/matchplanner/AcceptEditButton.kt | 43 ++--- .../buttons/matchplanner/CancelEditButton.kt | 44 ++--- .../buttons/matchplanner/DeclineEditButton.kt | 43 ++--- .../data/entry/PlanningNotifierRolesData.kt | 1 + .../data/entry/TimePlanningChannelsData.kt | 1 + .../data/entry/TimePlanningMessagesData.kt | 1 + .../repository/MatchPlanningDataRepository.kt | 112 +++++++++++++ .../PlanningNotifierRolesRepository.kt | 104 ++++++++++++ .../TimePlanningChannelsRepository.kt | 69 ++++++++ .../TimePlanningMessagesRepository.kt | 76 +++++++++ .../lilJudd/extensions/MatchExtension.kt | 40 +++-- .../extensions/SendPlannerExtension.kt | 22 +-- .../lilJudd/features/AvailabilityManager.kt | 154 +++++------------- .../moonleay/lilJudd/features/MatchManager.kt | 32 +--- .../moonleay/lilJudd/features/TimeManager.kt | 106 ++++-------- .../net/moonleay/lilJudd/jobs/MatchJob.kt | 33 +--- 16 files changed, 509 insertions(+), 372 deletions(-) create mode 100644 src/main/kotlin/net/moonleay/lilJudd/data/repository/MatchPlanningDataRepository.kt create mode 100644 src/main/kotlin/net/moonleay/lilJudd/data/repository/PlanningNotifierRolesRepository.kt create mode 100644 src/main/kotlin/net/moonleay/lilJudd/data/repository/TimePlanningChannelsRepository.kt create mode 100644 src/main/kotlin/net/moonleay/lilJudd/data/repository/TimePlanningMessagesRepository.kt diff --git a/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/AcceptEditButton.kt b/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/AcceptEditButton.kt index 3dd02d7..115a7e5 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/AcceptEditButton.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/AcceptEditButton.kt @@ -28,14 +28,10 @@ import dev.kord.core.entity.interaction.ButtonInteraction import dev.kord.rest.builder.message.modify.embed import net.moonleay.lilJudd.Bot import net.moonleay.lilJudd.buttons.component.IEditButton -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.util.EmbedUtil import net.moonleay.lilJudd.util.Logger 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 { override val id: String = "public.edit.btn.matchmanagement.accept" @@ -49,33 +45,20 @@ class AcceptEditButton() : IEditButton { val m = interaction.message val eb = MessageUtil.getAClonedEmbed(m.embeds[0]) var shouldEditButton = false - lateinit var mpdd: MatchPlanningDataData - var found = false - transaction { - for (pnr in MatchPlanningData.select { - 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 (!found || mpdd == null) { + val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer( + m.id.value.toLong(), + interaction.channelId.value.toLong(), + guild.id.value.toLong() + ) + if (mpdd == null) { + Logger.out("mpdd is null") + return + } + val role = guild.getRoleOrNull(Snowflake(mpdd.roleID)) + if (role == null) { + Logger.out("role is null") return } - val role = guild.getRoleOrNull(Snowflake(mpdd.roleID)) ?: return val member = interaction.user.asMember(guild.id) ?: return // do the checks and update if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) { diff --git a/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/CancelEditButton.kt b/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/CancelEditButton.kt index be99232..c7cebc3 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/CancelEditButton.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/CancelEditButton.kt @@ -28,12 +28,9 @@ import dev.kord.core.entity.interaction.ButtonInteraction import dev.kord.rest.builder.message.modify.embed import net.moonleay.lilJudd.Bot import net.moonleay.lilJudd.buttons.component.IEditButton -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.util.EmbedUtil -import org.jetbrains.exposed.sql.and -import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.transactions.transaction +import net.moonleay.lilJudd.util.Logger class CancelEditButton : IEditButton { override val id: String = "public.edit.btn.matchmanagement.cancel" @@ -46,33 +43,20 @@ class CancelEditButton : IEditButton { ) { val m = interaction.message if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) { - lateinit var mpdd: MatchPlanningDataData - var found = false - transaction { - for (pnr in MatchPlanningData.select { - 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 (!found || mpdd == null) { + val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer( + m.id.value.toLong(), + interaction.channelId.value.toLong(), + guild.id.value.toLong() + ) + if (mpdd == null) { + Logger.out("mpdd is null") + return + } + val role = guild.getRoleOrNull(Snowflake(mpdd.roleID)) + if (role == null) { + Logger.out("role is null") return } - val role = guild.getRoleOrNull(Snowflake(mpdd.roleID)) ?: return val member = interaction.user.asMember(guild.id) if (member.roleIds.contains(Snowflake(mpdd.roleID))) { member.removeRole(role.id) diff --git a/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/DeclineEditButton.kt b/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/DeclineEditButton.kt index 9a3de97..d775a29 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/DeclineEditButton.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/buttons/matchplanner/DeclineEditButton.kt @@ -28,14 +28,10 @@ import dev.kord.core.entity.interaction.ButtonInteraction import dev.kord.rest.builder.message.modify.embed import net.moonleay.lilJudd.Bot import net.moonleay.lilJudd.buttons.component.IEditButton -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.util.EmbedUtil import net.moonleay.lilJudd.util.Logger 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 { override val id: String = "public.edit.btn.matchmanagement.decline" @@ -49,33 +45,20 @@ class DeclineEditButton : IEditButton { val m = interaction.message val eb = MessageUtil.getAClonedEmbed(m.embeds[0]) var shouldEditButton = false - lateinit var mpdd: MatchPlanningDataData - var found = false - transaction { - for (pnr in MatchPlanningData.select { - 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 (!found || mpdd == null) { + val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer( + m.id.value.toLong(), + interaction.channelId.value.toLong(), + guild.id.value.toLong() + ) + if (mpdd == null) { + Logger.out("mpdd is null") + return + } + val role = guild.getRoleOrNull(Snowflake(mpdd.roleID)) + if (role == null) { + Logger.out("role is null") return } - val role = guild.getRoleOrNull(Snowflake(mpdd.roleID)) ?: return val member = interaction.user.asMember(guild.id) ?: return if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) { if (member.roleIds.contains(Snowflake(mpdd.roleID))) { diff --git a/src/main/kotlin/net/moonleay/lilJudd/data/entry/PlanningNotifierRolesData.kt b/src/main/kotlin/net/moonleay/lilJudd/data/entry/PlanningNotifierRolesData.kt index a36f351..730e7fe 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/data/entry/PlanningNotifierRolesData.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/data/entry/PlanningNotifierRolesData.kt @@ -19,6 +19,7 @@ package net.moonleay.lilJudd.data.entry data class PlanningNotifierRolesData( + val id: Int, // The id of the entry val serverID: Long, // The id of the server val channelID: Long, // The id of the channel val hasTimeRoleID: Long, // The id of the role that has time today diff --git a/src/main/kotlin/net/moonleay/lilJudd/data/entry/TimePlanningChannelsData.kt b/src/main/kotlin/net/moonleay/lilJudd/data/entry/TimePlanningChannelsData.kt index 2eebba4..fba9898 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/data/entry/TimePlanningChannelsData.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/data/entry/TimePlanningChannelsData.kt @@ -19,6 +19,7 @@ package net.moonleay.lilJudd.data.entry data class TimePlanningChannelsData( + val id: Int, val serverID: Long, val channelID: Long, ) diff --git a/src/main/kotlin/net/moonleay/lilJudd/data/entry/TimePlanningMessagesData.kt b/src/main/kotlin/net/moonleay/lilJudd/data/entry/TimePlanningMessagesData.kt index 6c5b2fc..fcb283b 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/data/entry/TimePlanningMessagesData.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/data/entry/TimePlanningMessagesData.kt @@ -19,6 +19,7 @@ package net.moonleay.lilJudd.data.entry data class TimePlanningMessagesData( + val id: Int, // The id of the entry val serverID: Long, // The discord server id val channelID: Long, // The discord channel id val weekstamp: Long, // The timestamp of the monday of the week at 4am UTC diff --git a/src/main/kotlin/net/moonleay/lilJudd/data/repository/MatchPlanningDataRepository.kt b/src/main/kotlin/net/moonleay/lilJudd/data/repository/MatchPlanningDataRepository.kt new file mode 100644 index 0000000..385eb69 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/data/repository/MatchPlanningDataRepository.kt @@ -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 . + */ + +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 { + val dataList = mutableListOf() + 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 + } +} diff --git a/src/main/kotlin/net/moonleay/lilJudd/data/repository/PlanningNotifierRolesRepository.kt b/src/main/kotlin/net/moonleay/lilJudd/data/repository/PlanningNotifierRolesRepository.kt new file mode 100644 index 0000000..2603081 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/data/repository/PlanningNotifierRolesRepository.kt @@ -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 . + */ + +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 { + val dataList = mutableListOf() + 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 } + } + } +} diff --git a/src/main/kotlin/net/moonleay/lilJudd/data/repository/TimePlanningChannelsRepository.kt b/src/main/kotlin/net/moonleay/lilJudd/data/repository/TimePlanningChannelsRepository.kt new file mode 100644 index 0000000..7756802 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/data/repository/TimePlanningChannelsRepository.kt @@ -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 . + */ + +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 { + val dataList = mutableListOf() + 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 + } +} diff --git a/src/main/kotlin/net/moonleay/lilJudd/data/repository/TimePlanningMessagesRepository.kt b/src/main/kotlin/net/moonleay/lilJudd/data/repository/TimePlanningMessagesRepository.kt new file mode 100644 index 0000000..5e2f273 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/data/repository/TimePlanningMessagesRepository.kt @@ -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 . + */ + +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 { + val dataList = mutableListOf() + 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] + ) + } + } + + +} diff --git a/src/main/kotlin/net/moonleay/lilJudd/extensions/MatchExtension.kt b/src/main/kotlin/net/moonleay/lilJudd/extensions/MatchExtension.kt index 40787d4..17592e6 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/extensions/MatchExtension.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/extensions/MatchExtension.kt @@ -28,7 +28,8 @@ import com.kotlindiscord.kord.extensions.types.respond import dev.kord.core.behavior.channel.createMessage import dev.kord.core.behavior.createRole 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.jobs.MatchJob 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.MessageUtil 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() { @@ -100,28 +98,28 @@ class MatchExtension : Extension() { } return@action } - var tableID by Delegates.notNull() - transaction { - tableID = MatchPlanningData.insert { - it[MatchPlanningData.serverid] = gID.toLong() - it[MatchPlanningData.channelid] = cID.toLong() - it[MatchPlanningData.messageid] = msg.id.value.toLong() - it[MatchPlanningData.matchtype] = args.matchType.readableName - it[MatchPlanningData.roleid] = role.id.value.toLong() - it[MatchPlanningData.registererid] = m.id.value.toLong() - it[MatchPlanningData.opponentName] = opponent - it[MatchPlanningData.timestamp] = (zdt.toEpochSecond() * 1000) - it[MatchPlanningData.jobstr] = jobString - } get MatchPlanningData.id - } - if (tableID == null) { + val tID = MatchPlanningDataRepository.write( + MatchPlanningDataData( + 0, + gID.toLong(), + cID.toLong(), + args.matchType.readableName, + m.id.value.toLong(), + role.id.value.toLong(), + opponent, + msg.id.value.toLong(), + (zdt.toEpochSecond() * 1000), + jobString + ) + ) + if (tID == null || tID <= 0) { return@action // Not saved to db } JobManager.addJob( MatchJob( jobString, - tableID, - "${args.matchType.readableName}_Vs_${opponent}_[${tableID}]-${gID}_${cID}", + tID, + "${args.matchType.readableName}_Vs_${opponent}_[${tID}]-${gID}_${cID}", ) ) } diff --git a/src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt b/src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt index fd293d9..ee85372 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt @@ -26,10 +26,9 @@ import dev.kord.common.entity.Permission import dev.kord.core.behavior.channel.createMessage import dev.kord.rest.builder.message.create.actionRow 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 org.jetbrains.exposed.sql.insert -import org.jetbrains.exposed.sql.transactions.transaction import java.time.ZoneId import java.time.ZonedDateTime @@ -101,14 +100,15 @@ class SendPlannerExtension : Extension() { } // Save the message ids - transaction { - TimePlanningMessages.insert { - it[TimePlanningMessages.serverid] = c.data.guildId.value?.value!!.toLong() - it[TimePlanningMessages.channelid] = c.id.value.toLong() - it[TimePlanningMessages.weekstamp] = (TimeUtil.getWeekStamp().toEpochSecond() * 1000) - it[TimePlanningMessages.messageids] = msgStr - } get TimePlanningMessages.id - } + TimePlanningMessagesRepository.write( + TimePlanningMessagesData( + -1, + c.data.guildId.value?.value!!.toLong(), + c.id.value.toLong(), + (TimeUtil.getWeekStamp().toEpochSecond() * 1000), + msgStr + ) + ) Logger.out("Finished with ${c.data.guildId.value}") } } diff --git a/src/main/kotlin/net/moonleay/lilJudd/features/AvailabilityManager.kt b/src/main/kotlin/net/moonleay/lilJudd/features/AvailabilityManager.kt index 8683dd5..6a42046 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/features/AvailabilityManager.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/features/AvailabilityManager.kt @@ -32,61 +32,35 @@ 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.data.repository.PlanningNotifierRolesRepository +import net.moonleay.lilJudd.data.repository.TimePlanningMessagesRepository 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.* -import org.jetbrains.exposed.sql.* -import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq -import org.jetbrains.exposed.sql.transactions.transaction import java.time.ZonedDateTime object AvailabilityManager : IFeature { // This runs during the cronjob. - @OptIn(PrivilegedIntent::class) suspend fun runThread() { Logger.out("Starting to update roles...") // ChannelID, Data - val messageMap = mutableMapOf() - // ChannelID, Data - val roleMap = mutableMapOf() + val messages = TimePlanningMessagesRepository.getWeek(TimeUtil.getWeekStamp().toEpochSecond() * 1000) + .associateBy { it.channelID } + val targetedRoles = PlanningNotifierRolesRepository.getAll().associateBy { it.channelID } - transaction { - for (pnr in TimePlanningMessages.select { - TimePlanningMessages.weekstamp eq (TimeUtil.getWeekStamp().toEpochSecond() * 1000) - }) { - 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 + for (id in messages.keys) { + val snf = Snowflake(id) // snf = Snowflake + val data = messages[id]!! // this is the data of the table if (Bot.bot.kordRef.getChannel(Snowflake(data.channelID)) == null) continue // This channel does not exist anymore. - if (roleMap.isEmpty()) { + if (targetedRoles.isEmpty()) { Logger.out("No saved roles. Canceling.") return } - val roleData = roleMap[data.channelID] // Get the role data + val roleData = targetedRoles[data.channelID] // Get the role data if (roleData == null) { Logger.out("Role for channel ${data.channelID} does not exist") 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) { - lateinit var data: TimePlanningMessagesData - lateinit var roleData: PlanningNotifierRolesData - var found1 = false - 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 - } - 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) { + val messageData = TimePlanningMessagesRepository.getWeekInChannel( + TimeUtil.getWeekStamp().toEpochSecond() * 1000, + snf.value.toLong() + ) + val roleData = PlanningNotifierRolesRepository.getForChannel(snf.value.toLong()) + if (messageData == null) { Logger.out("Could not find data for channel ${snf.value}") return } - if (Bot.bot.kordRef.getChannel(Snowflake(data.channelID)) == null) - return // This channel does not exist anymore. 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 } - updateInChannel(snf, data, roleData) + updateInChannel(snf, messageData, roleData) } @OptIn(PrivilegedIntent::class) 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. - val c = - Bot.bot.kordRef.getChannelOf(Snowflake(tpmd.channelID))!! // Get the channel as MessageChannel - if (Bot.bot.kordRef.getGuildOrNull(Snowflake(tpmd.serverID)) == null) { - Logger.out("Guild not found.") - return - } + val c = Bot.bot.kordRef.getChannelOf(snf)!! // Get the channel as MessageChannel val weekday = ZonedDateTime.now().dayOfWeek // The current week day 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.") @@ -228,13 +171,8 @@ object AvailabilityManager : IFeature { ch: Channel, args: FeatureManageExtension.FeatureManagerArgs ): EmbedBuilder { - var alreadyExists = false + var alreadyExists = PlanningNotifierRolesRepository.existsInChannel(cID) // 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 { @@ -250,14 +188,15 @@ object AvailabilityManager : IFeature { val wnr = wantsNotifsRole.id.value.toLong() // 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 - } + PlanningNotifierRolesRepository.write( + PlanningNotifierRolesData( + id = -1, + serverID = gID, + channelID = cID, + hasTimeRoleID = htr, + wantsToBeNotifiedID = wnr + ) + ) return MessageUtil.getEmbed( EmbedColor.SUCCESS, @@ -284,35 +223,16 @@ object AvailabilityManager : IFeature { 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 = mutableListOf() - transaction { - matchingEntries = PlanningNotifierRoles.select { - (PlanningNotifierRoles.serverid eq gID) and - (PlanningNotifierRoles.channelid eq cID) - }.toList() - } + if (PlanningNotifierRolesRepository.existsInChannelFromSever(cID, gID)) { + val entry = PlanningNotifierRolesRepository.getForChannelInServer(cID, gID)!! // 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() - } + Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID)) + .getRoleOrNull(Snowflake(entry.hasTimeRoleID))?.delete() + Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID)) + .getRoleOrNull(Snowflake(entry.wantsToBeNotifiedID))?.delete() // delete all found entries - transaction { - matchingEntries.forEach { entry -> - PlanningNotifierRoles.deleteWhere { id eq entry[id] } - } - } + PlanningNotifierRolesRepository.delete(entry.id) return MessageUtil.getEmbed( EmbedColor.SUCCESS, "200: Success", diff --git a/src/main/kotlin/net/moonleay/lilJudd/features/MatchManager.kt b/src/main/kotlin/net/moonleay/lilJudd/features/MatchManager.kt index aeb2203..fc89dc9 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/features/MatchManager.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/features/MatchManager.kt @@ -21,46 +21,22 @@ package net.moonleay.lilJudd.features import dev.kord.common.entity.Snowflake import net.moonleay.lilJudd.Bot 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.component.JobManager 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 { suspend fun update() { Logger.out("Updating match roles...") - val dataList = mutableListOf() - 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] - ) - ) - } - } + val dataList = MatchPlanningDataRepository.getAll() for (data in dataList) { 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...") this.removeRoleFromGuild(data.serverID, data.roleID) - transaction { - MatchPlanningData.deleteWhere { MatchPlanningData.messageid eq data.messageID } - } + MatchPlanningDataRepository.delete(data.id) continue } this.registerJob(data) diff --git a/src/main/kotlin/net/moonleay/lilJudd/features/TimeManager.kt b/src/main/kotlin/net/moonleay/lilJudd/features/TimeManager.kt index 8a66ee7..d986440 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/features/TimeManager.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/features/TimeManager.kt @@ -29,17 +29,18 @@ import dev.kord.rest.builder.message.EmbedBuilder import dev.kord.rest.builder.message.create.actionRow import kotlinx.coroutines.delay import net.moonleay.lilJudd.Bot -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.data.entry.TimePlanningChannelsData +import net.moonleay.lilJudd.data.entry.TimePlanningMessagesData +import net.moonleay.lilJudd.data.repository.PlanningNotifierRolesRepository +import net.moonleay.lilJudd.data.repository.TimePlanningChannelsRepository +import net.moonleay.lilJudd.data.repository.TimePlanningMessagesRepository 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.* -import org.jetbrains.exposed.sql.* -import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq -import org.jetbrains.exposed.sql.transactions.transaction +import net.moonleay.lilJudd.util.EmbedColor +import net.moonleay.lilJudd.util.EmbedUtil +import net.moonleay.lilJudd.util.Logger +import net.moonleay.lilJudd.util.MessageUtil import java.time.ZoneId import java.time.ZonedDateTime @@ -60,41 +61,23 @@ object TimeManager : IFeature { private suspend fun runThread() { Logger.out("Starting to notify...") + // ChannelID -> Data + val targetedChannels = TimePlanningChannelsRepository.getAll().associateBy { it.channelID } + val targetedRoles = PlanningNotifierRolesRepository.getAll().associateBy { it.channelID } - // ChannelID, ServerID - val channelList = mutableMapOf() - // ChannelID, Data - val roleMap = mutableMapOf() + lateinit var msgStr: String - 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()) { - 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) { + Logger.out("${targetedChannels.count()} Channels to notify with ${targetedRoles.count()} Roles to ping!") + for (ch2 in targetedChannels.keys) { + val ch = Snowflake(ch2) if (Bot.bot.kordRef.getChannel(ch) == null) continue // TODO: Check if the channel is valid in another shard val c = Bot.bot.kordRef.getChannelOf(ch)!! msgStr = "" - if (roleMap != null && roleMap.keys.contains(ch) && roleMap[ch] != null) { + if (targetedRoles != null && targetedRoles.keys.contains(ch2) && targetedRoles[ch2] != null) { c.createMessage { this.content = - "The weekly planning starts now <@&${Snowflake(roleMap[ch]?.wantsToBeNotifiedID!!)}>" + "The weekly planning starts now <@&${Snowflake(targetedRoles[ch2]?.wantsToBeNotifiedID!!)}>" this.embeds.add( MessageUtil.getEmbed( EmbedColor.INFO, @@ -145,14 +128,15 @@ object TimeManager : IFeature { } // Save the message ids - transaction { - TimePlanningMessages.insert { - it[TimePlanningMessages.serverid] = c.data.guildId.value?.value!!.toLong() - it[TimePlanningMessages.channelid] = c.id.value.toLong() - it[TimePlanningMessages.weekstamp] = (TimeUtil.getWeekStamp().toEpochSecond() * 1000) - it[TimePlanningMessages.messageids] = msgStr - } get TimePlanningMessages.id - } + TimePlanningMessagesRepository.write( + TimePlanningMessagesData( + id = -1, + serverID = c.data.guildId.value?.value!!.toLong(), + channelID = c.data.id.value.toLong(), + weekstamp = then.minusDays(7).toEpochSecond(), + messageIDs = msgStr + ) + ) Logger.out("Finished with ${c.data.guildId.value}") } Logger.out("Done! Until next Monday! <3 ") @@ -165,20 +149,8 @@ object TimeManager : IFeature { 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 - } + if (!TimePlanningChannelsRepository.exists(cID, gID)) { + TimePlanningChannelsRepository.write(TimePlanningChannelsData(id = -1, serverID = gID, channelID = cID)) return MessageUtil.getEmbed( EmbedColor.SUCCESS, "200: Success", @@ -202,25 +174,9 @@ object TimeManager : IFeature { 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) { + if (TimePlanningChannelsRepository.exists(cID, gID)) { // 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] } - } - } + TimePlanningChannelsRepository.deleteFromChannelInServer(cID, gID) return MessageUtil.getEmbed( EmbedColor.SUCCESS, "200: Success", diff --git a/src/main/kotlin/net/moonleay/lilJudd/jobs/MatchJob.kt b/src/main/kotlin/net/moonleay/lilJudd/jobs/MatchJob.kt index f350b86..8360f62 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/jobs/MatchJob.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/jobs/MatchJob.kt @@ -22,16 +22,11 @@ import dev.inmo.krontab.KronScheduler import dev.kord.common.entity.Snowflake import kotlinx.coroutines.Job import net.moonleay.lilJudd.Bot -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.component.CronjobType import net.moonleay.lilJudd.jobs.component.ICronjob import net.moonleay.lilJudd.jobs.component.JobManager 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( override val jobIncoming: String, @@ -48,25 +43,7 @@ class MatchJob( * */ override suspend fun jobFunction() { Logger.out("Running MatchJob \"${this.jobName}\"") - lateinit var mpdd: MatchPlanningDataData - 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 mpdd = MatchPlanningDataRepository.get(tableId)!! val guild = Bot.bot.kordRef.getGuildOrNull(Snowflake(mpdd.serverID)) if (guild == null) { Logger.out("Guild not found.") @@ -78,11 +55,7 @@ class MatchJob( return } r.delete() - transaction { - MatchPlanningData.deleteWhere { - MatchPlanningData.id eq (tableId) - } - } + MatchPlanningDataRepository.delete(tableId) Logger.out("MatchJob \"${this.jobName}\" finished.") Logger.out("Killing job \"${this.jobName}\"..") JobManager.killJob(this)