Compare commits

..

No commits in common. "95c22c6bd0acf2c207ca0cb8f2a76bc685f97db9" and "b77271517d4aab3bdd3a4da5cba842c8a95632e3" have entirely different histories.

17 changed files with 374 additions and 510 deletions

View file

@ -103,7 +103,8 @@ object Bot {
add(::FeatureManageExtension) add(::FeatureManageExtension)
add(::SendPlannerExtension) add(::SendPlannerExtension)
add(::MatchExtension) add(::MatchExtension)
add(::UpdateRolesExtension) add(::UpdateRolesExtension) // This command is only for debugging purposes
//add(::TestExtension) // See comment in TestExtension.kt
} }
this.presence { this.presence {

View file

@ -28,10 +28,14 @@ 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.repository.MatchPlanningDataRepository import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
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"
@ -45,20 +49,33 @@ 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
val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer( lateinit var mpdd: MatchPlanningDataData
m.id.value.toLong(), var found = false
interaction.channelId.value.toLong(), transaction {
guild.id.value.toLong() 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]
) )
if (mpdd == null) { found = true
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,9 +28,12 @@ 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.repository.MatchPlanningDataRepository import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
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 org.jetbrains.exposed.sql.and
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"
@ -43,20 +46,33 @@ 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())) {
val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer( lateinit var mpdd: MatchPlanningDataData
m.id.value.toLong(), var found = false
interaction.channelId.value.toLong(), transaction {
guild.id.value.toLong() 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]
) )
if (mpdd == null) { found = true
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,10 +28,14 @@ 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.repository.MatchPlanningDataRepository import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
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"
@ -45,20 +49,33 @@ 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
val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer( lateinit var mpdd: MatchPlanningDataData
m.id.value.toLong(), var found = false
interaction.channelId.value.toLong(), transaction {
guild.id.value.toLong() 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]
) )
if (mpdd == null) { found = true
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,7 +19,6 @@
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,7 +19,6 @@
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,7 +19,6 @@
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

@ -1,112 +0,0 @@
/*
* 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

@ -1,104 +0,0 @@
/*
* 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

@ -1,69 +0,0 @@
/*
* 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

@ -1,76 +0,0 @@
/*
* 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,8 +28,7 @@ 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.entry.MatchPlanningDataData import net.moonleay.lilJudd.data.tables.MatchPlanningData
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
@ -37,6 +36,9 @@ 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() {
@ -98,28 +100,28 @@ class MatchExtension : Extension() {
} }
return@action return@action
} }
val tID = MatchPlanningDataRepository.write( var tableID by Delegates.notNull<Int>()
MatchPlanningDataData( transaction {
0, tableID = MatchPlanningData.insert {
gID.toLong(), it[MatchPlanningData.serverid] = gID.toLong()
cID.toLong(), it[MatchPlanningData.channelid] = cID.toLong()
args.matchType.readableName, it[MatchPlanningData.messageid] = msg.id.value.toLong()
m.id.value.toLong(), it[MatchPlanningData.matchtype] = args.matchType.readableName
role.id.value.toLong(), it[MatchPlanningData.roleid] = role.id.value.toLong()
opponent, it[MatchPlanningData.registererid] = m.id.value.toLong()
msg.id.value.toLong(), it[MatchPlanningData.opponentName] = opponent
(zdt.toEpochSecond() * 1000), it[MatchPlanningData.timestamp] = (zdt.toEpochSecond() * 1000)
jobString it[MatchPlanningData.jobstr] = jobString
) } get MatchPlanningData.id
) }
if (tID == null || tID <= 0) { if (tableID == null) {
return@action // Not saved to db return@action // Not saved to db
} }
JobManager.addJob( JobManager.addJob(
MatchJob( MatchJob(
jobString, jobString,
tID, tableID,
"${args.matchType.readableName}_Vs_${opponent}_[${tID}]-${gID}_${cID}", "${args.matchType.readableName}_Vs_${opponent}_[${tableID}]-${gID}_${cID}",
) )
) )
} }

View file

@ -26,9 +26,10 @@ 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.entry.TimePlanningMessagesData import net.moonleay.lilJudd.data.tables.TimePlanningMessages
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
@ -100,15 +101,14 @@ class SendPlannerExtension : Extension() {
} }
// Save the message ids // Save the message ids
TimePlanningMessagesRepository.write( transaction {
TimePlanningMessagesData( TimePlanningMessages.insert {
-1, it[TimePlanningMessages.serverid] = c.data.guildId.value?.value!!.toLong()
c.data.guildId.value?.value!!.toLong(), it[TimePlanningMessages.channelid] = c.id.value.toLong()
c.id.value.toLong(), it[TimePlanningMessages.weekstamp] = (TimeUtil.getWeekStamp().toEpochSecond() * 1000)
(TimeUtil.getWeekStamp().toEpochSecond() * 1000), it[TimePlanningMessages.messageids] = msgStr
msgStr } get TimePlanningMessages.id
) }
)
Logger.out("Finished with ${c.data.guildId.value}") Logger.out("Finished with ${c.data.guildId.value}")
} }
} }

View file

@ -32,35 +32,61 @@ 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.repository.PlanningNotifierRolesRepository import net.moonleay.lilJudd.data.tables.PlanningNotifierRoles
import net.moonleay.lilJudd.data.repository.TimePlanningMessagesRepository import net.moonleay.lilJudd.data.tables.TimePlanningMessages
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 messages = TimePlanningMessagesRepository.getWeek(TimeUtil.getWeekStamp().toEpochSecond() * 1000) val messageMap = mutableMapOf<Snowflake, TimePlanningMessagesData>()
.associateBy { it.channelID } // ChannelID, Data
val targetedRoles = PlanningNotifierRolesRepository.getAll().associateBy { it.channelID } val roleMap = mutableMapOf<Long, PlanningNotifierRolesData>()
for (id in messages.keys) { transaction {
val snf = Snowflake(id) // snf = Snowflake for (pnr in TimePlanningMessages.select {
val data = messages[id]!! // this is the data of the table 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
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 (targetedRoles.isEmpty()) { if (roleMap.isEmpty()) {
Logger.out("No saved roles. Canceling.") Logger.out("No saved roles. Canceling.")
return return
} }
val roleData = targetedRoles[data.channelID] // Get the role data val roleData = roleMap[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
@ -71,27 +97,58 @@ object AvailabilityManager : IFeature {
} }
suspend fun updateInChannel(snf: Snowflake) { suspend fun updateInChannel(snf: Snowflake) {
val messageData = TimePlanningMessagesRepository.getWeekInChannel( lateinit var data: TimePlanningMessagesData
TimeUtil.getWeekStamp().toEpochSecond() * 1000, lateinit var roleData: PlanningNotifierRolesData
snf.value.toLong() 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]
) )
val roleData = PlanningNotifierRolesRepository.getForChannel(snf.value.toLong()) found1 = true
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 ${messageData.channelID} does not exist") Logger.out("Role for channel ${data.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, messageData, roleData) updateInChannel(snf, data, 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(snf) == null) if (Bot.bot.kordRef.getChannel(Snowflake(tpmd.channelID)) == null)
return // This channel does not exist anymore. return // This channel does not exist anymore.
val c = Bot.bot.kordRef.getChannelOf<MessageChannel>(snf)!! // Get the channel as MessageChannel val c =
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.")
@ -171,8 +228,13 @@ object AvailabilityManager : IFeature {
ch: Channel, ch: Channel,
args: FeatureManageExtension.FeatureManagerArgs args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder { ): EmbedBuilder {
var alreadyExists = PlanningNotifierRolesRepository.existsInChannel(cID) var alreadyExists = false
// 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 {
@ -188,15 +250,14 @@ 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
PlanningNotifierRolesRepository.write( transaction {
PlanningNotifierRolesData( PlanningNotifierRoles.insert {
id = -1, it[PlanningNotifierRoles.serverid] = gID
serverID = gID, it[PlanningNotifierRoles.channelid] = cID
channelID = cID, it[PlanningNotifierRoles.hastimeroleid] = htr
hasTimeRoleID = htr, it[PlanningNotifierRoles.wantstobenotifiedid] = wnr
wantsToBeNotifiedID = wnr } get PlanningNotifierRoles.id
) }
)
return MessageUtil.getEmbed( return MessageUtil.getEmbed(
EmbedColor.SUCCESS, EmbedColor.SUCCESS,
@ -223,16 +284,35 @@ object AvailabilityManager : IFeature {
args: FeatureManageExtension.FeatureManagerArgs args: FeatureManageExtension.FeatureManagerArgs
): EmbedBuilder { ): EmbedBuilder {
// Check if entry exists in db // Check if entry exists in db
if (PlanningNotifierRolesRepository.existsInChannelFromSever(cID, gID)) { var alreadyExists = false
val entry = PlanningNotifierRolesRepository.getForChannelInServer(cID, gID)!! transaction {
alreadyExists = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
}.count() > 0
}
if (alreadyExists) {
var matchingEntries: List<ResultRow> = mutableListOf()
transaction {
matchingEntries = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and
(PlanningNotifierRoles.channelid eq cID)
}.toList()
}
// delete all entries for this guild and channel combo // 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(entry.hasTimeRoleID))?.delete() .getRoleOrNull(Snowflake(e[PlanningNotifierRoles.hastimeroleid]))?.delete()
Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID)) Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID))
.getRoleOrNull(Snowflake(entry.wantsToBeNotifiedID))?.delete() .getRoleOrNull(Snowflake(e[PlanningNotifierRoles.wantstobenotifiedid]))
?.delete()
}
// delete all found entries // delete all found entries
PlanningNotifierRolesRepository.delete(entry.id) transaction {
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,22 +21,46 @@ 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.repository.MatchPlanningDataRepository import net.moonleay.lilJudd.data.tables.MatchPlanningData
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 = MatchPlanningDataRepository.getAll() 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]
)
)
}
}
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 < System.currentTimeMillis()) { if (data.timestamp.toLong() < 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)
MatchPlanningDataRepository.delete(data.id) transaction {
MatchPlanningData.deleteWhere { MatchPlanningData.messageid eq data.messageID }
}
continue continue
} }
this.registerJob(data) this.registerJob(data)

View file

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

View file

@ -22,11 +22,16 @@ 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.repository.MatchPlanningDataRepository import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
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,
@ -43,7 +48,25 @@ class MatchJob(
* */ * */
override suspend fun jobFunction() { override suspend fun jobFunction() {
Logger.out("Running MatchJob \"${this.jobName}\"") Logger.out("Running MatchJob \"${this.jobName}\"")
val mpdd = MatchPlanningDataRepository.get(tableId)!! 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 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.")
@ -55,7 +78,11 @@ class MatchJob(
return return
} }
r.delete() r.delete()
MatchPlanningDataRepository.delete(tableId) transaction {
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)