feat: moved all transactions into one package for reuse and simplicity
Signed-off-by: moonleay <contact@moonleay.net>
This commit is contained in:
parent
47a703156c
commit
39c8867722
16 changed files with 509 additions and 372 deletions
|
@ -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]
|
||||
val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer(
|
||||
m.id.value.toLong(),
|
||||
interaction.channelId.value.toLong(),
|
||||
guild.id.value.toLong()
|
||||
)
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if (!found || mpdd == null) {
|
||||
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())) {
|
||||
|
|
|
@ -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]
|
||||
val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer(
|
||||
m.id.value.toLong(),
|
||||
interaction.channelId.value.toLong(),
|
||||
guild.id.value.toLong()
|
||||
)
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if (!found || mpdd == null) {
|
||||
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)
|
||||
|
|
|
@ -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]
|
||||
val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer(
|
||||
m.id.value.toLong(),
|
||||
interaction.channelId.value.toLong(),
|
||||
guild.id.value.toLong()
|
||||
)
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if (!found || mpdd == null) {
|
||||
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))) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
package net.moonleay.lilJudd.data.entry
|
||||
|
||||
data class TimePlanningChannelsData(
|
||||
val id: Int,
|
||||
val serverID: Long,
|
||||
val channelID: Long,
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* lilJudd
|
||||
* Copyright (C) 2023 moonleay
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.moonleay.lilJudd.data.repository
|
||||
|
||||
import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
|
||||
import net.moonleay.lilJudd.data.tables.MatchPlanningData
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
object MatchPlanningDataRepository {
|
||||
|
||||
fun getAll(): List<MatchPlanningDataData> {
|
||||
val dataList = mutableListOf<MatchPlanningDataData>()
|
||||
transaction {
|
||||
MatchPlanningData.selectAll().forEach {
|
||||
dataList.add(
|
||||
MatchPlanningDataData(
|
||||
it[MatchPlanningData.id],
|
||||
it[MatchPlanningData.serverid],
|
||||
it[MatchPlanningData.channelid],
|
||||
it[MatchPlanningData.matchtype],
|
||||
it[MatchPlanningData.registererid],
|
||||
it[MatchPlanningData.roleid],
|
||||
it[MatchPlanningData.opponentName],
|
||||
it[MatchPlanningData.messageid],
|
||||
it[MatchPlanningData.timestamp],
|
||||
it[MatchPlanningData.jobstr]
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
return dataList
|
||||
}
|
||||
|
||||
fun get(id: Int): MatchPlanningDataData? =
|
||||
transaction {
|
||||
MatchPlanningData.select { MatchPlanningData.id eq id }.firstOrNull()?.let {
|
||||
MatchPlanningDataData(
|
||||
it[MatchPlanningData.id],
|
||||
it[MatchPlanningData.serverid],
|
||||
it[MatchPlanningData.channelid],
|
||||
it[MatchPlanningData.matchtype],
|
||||
it[MatchPlanningData.registererid],
|
||||
it[MatchPlanningData.roleid],
|
||||
it[MatchPlanningData.opponentName],
|
||||
it[MatchPlanningData.messageid],
|
||||
it[MatchPlanningData.timestamp],
|
||||
it[MatchPlanningData.jobstr]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun getFromMessageInChannelInServer(messageID: Long, channelID: Long, serverID: Long): MatchPlanningDataData? =
|
||||
transaction {
|
||||
MatchPlanningData.select {
|
||||
MatchPlanningData.messageid eq (messageID) and (
|
||||
MatchPlanningData.serverid eq (serverID)) and (
|
||||
MatchPlanningData.channelid eq (channelID))
|
||||
}.firstOrNull()?.let {
|
||||
MatchPlanningDataData(
|
||||
it[MatchPlanningData.id],
|
||||
it[MatchPlanningData.serverid],
|
||||
it[MatchPlanningData.channelid],
|
||||
it[MatchPlanningData.matchtype],
|
||||
it[MatchPlanningData.registererid],
|
||||
it[MatchPlanningData.roleid],
|
||||
it[MatchPlanningData.opponentName],
|
||||
it[MatchPlanningData.messageid],
|
||||
it[MatchPlanningData.timestamp],
|
||||
it[MatchPlanningData.jobstr]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun delete(id: Int) {
|
||||
transaction {
|
||||
MatchPlanningData.deleteWhere { MatchPlanningData.id eq id }
|
||||
}
|
||||
}
|
||||
|
||||
fun write(data: MatchPlanningDataData): Int =
|
||||
transaction {
|
||||
MatchPlanningData.insert {
|
||||
it[MatchPlanningData.serverid] = data.serverID
|
||||
it[MatchPlanningData.channelid] = data.channelID
|
||||
it[MatchPlanningData.matchtype] = data.matchType
|
||||
it[MatchPlanningData.registererid] = data.registererID
|
||||
it[MatchPlanningData.roleid] = data.roleID
|
||||
it[MatchPlanningData.opponentName] = data.opponentName
|
||||
it[MatchPlanningData.messageid] = data.messageID
|
||||
it[MatchPlanningData.timestamp] = data.timestamp
|
||||
it[MatchPlanningData.jobstr] = data.jobString
|
||||
} get MatchPlanningData.id
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* lilJudd
|
||||
* Copyright (C) 2023 moonleay
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.moonleay.lilJudd.data.repository
|
||||
|
||||
import net.moonleay.lilJudd.data.entry.PlanningNotifierRolesData
|
||||
import net.moonleay.lilJudd.data.tables.PlanningNotifierRoles
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
object PlanningNotifierRolesRepository {
|
||||
|
||||
fun getAll(): List<PlanningNotifierRolesData> {
|
||||
val dataList = mutableListOf<PlanningNotifierRolesData>()
|
||||
transaction {
|
||||
for (pnr in PlanningNotifierRoles.selectAll()) {
|
||||
dataList.add(
|
||||
PlanningNotifierRolesData(
|
||||
pnr[PlanningNotifierRoles.id],
|
||||
pnr[PlanningNotifierRoles.serverid],
|
||||
pnr[PlanningNotifierRoles.channelid],
|
||||
pnr[PlanningNotifierRoles.hastimeroleid],
|
||||
pnr[PlanningNotifierRoles.wantstobenotifiedid]
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
return dataList
|
||||
}
|
||||
|
||||
fun getForChannel(channelID: Long): PlanningNotifierRolesData? =
|
||||
transaction {
|
||||
PlanningNotifierRoles.select {
|
||||
PlanningNotifierRoles.channelid eq channelID
|
||||
}.firstOrNull()?.let {
|
||||
PlanningNotifierRolesData(
|
||||
it[PlanningNotifierRoles.id],
|
||||
it[PlanningNotifierRoles.serverid],
|
||||
it[PlanningNotifierRoles.channelid],
|
||||
it[PlanningNotifierRoles.hastimeroleid],
|
||||
it[PlanningNotifierRoles.wantstobenotifiedid]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun getForChannelInServer(channelID: Long, serverID: Long): PlanningNotifierRolesData? =
|
||||
transaction {
|
||||
PlanningNotifierRoles.select {
|
||||
PlanningNotifierRoles.channelid eq channelID and (PlanningNotifierRoles.serverid eq serverID)
|
||||
}.firstOrNull()?.let {
|
||||
PlanningNotifierRolesData(
|
||||
it[PlanningNotifierRoles.id],
|
||||
it[PlanningNotifierRoles.serverid],
|
||||
it[PlanningNotifierRoles.channelid],
|
||||
it[PlanningNotifierRoles.hastimeroleid],
|
||||
it[PlanningNotifierRoles.wantstobenotifiedid]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun existsInChannel(channelID: Long): Boolean =
|
||||
transaction {
|
||||
PlanningNotifierRoles.select { PlanningNotifierRoles.channelid eq channelID }.count() > 0
|
||||
}
|
||||
|
||||
fun existsInChannelFromSever(channelID: Long, serverID: Long): Boolean =
|
||||
transaction {
|
||||
PlanningNotifierRoles.select { PlanningNotifierRoles.channelid eq channelID and (PlanningNotifierRoles.serverid eq serverID) }
|
||||
.count() > 0
|
||||
}
|
||||
|
||||
fun write(data: PlanningNotifierRolesData) {
|
||||
transaction {
|
||||
PlanningNotifierRoles.insert {
|
||||
it[PlanningNotifierRoles.serverid] = data.serverID
|
||||
it[PlanningNotifierRoles.channelid] = data.channelID
|
||||
it[PlanningNotifierRoles.hastimeroleid] = data.hasTimeRoleID
|
||||
it[PlanningNotifierRoles.wantstobenotifiedid] = data.wantsToBeNotifiedID
|
||||
} get PlanningNotifierRoles.id
|
||||
}
|
||||
}
|
||||
|
||||
fun delete(id: Int) {
|
||||
transaction {
|
||||
PlanningNotifierRoles.deleteWhere { PlanningNotifierRoles.id eq id }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* lilJudd
|
||||
* Copyright (C) 2023 moonleay
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.moonleay.lilJudd.data.repository
|
||||
|
||||
import net.moonleay.lilJudd.data.entry.TimePlanningChannelsData
|
||||
import net.moonleay.lilJudd.data.tables.TimePlanningChannels
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
object TimePlanningChannelsRepository {
|
||||
|
||||
fun getAll(): List<TimePlanningChannelsData> {
|
||||
val dataList = mutableListOf<TimePlanningChannelsData>()
|
||||
transaction {
|
||||
for (tp in TimePlanningChannels.selectAll())
|
||||
dataList.add(
|
||||
TimePlanningChannelsData(
|
||||
id = tp[TimePlanningChannels.id],
|
||||
serverID = tp[TimePlanningChannels.serverid],
|
||||
channelID = tp[TimePlanningChannels.channelid],
|
||||
)
|
||||
)
|
||||
}
|
||||
return dataList
|
||||
}
|
||||
|
||||
fun exists(channelID: Long, serverID: Long): Boolean =
|
||||
transaction {
|
||||
TimePlanningChannels.select { TimePlanningChannels.channelid eq channelID and (TimePlanningChannels.serverid eq serverID) }
|
||||
.firstOrNull() != null
|
||||
}
|
||||
|
||||
fun delete(id: Int) {
|
||||
transaction {
|
||||
TimePlanningChannels.deleteWhere { TimePlanningChannels.id eq id }
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteFromChannelInServer(channelID: Long, serverID: Long) {
|
||||
transaction {
|
||||
TimePlanningChannels.deleteWhere { TimePlanningChannels.channelid eq channelID and (TimePlanningChannels.serverid eq serverID) }
|
||||
}
|
||||
}
|
||||
|
||||
fun write(data: TimePlanningChannelsData): Int =
|
||||
transaction {
|
||||
TimePlanningChannels.insert {
|
||||
it[TimePlanningChannels.serverid] = data.serverID
|
||||
it[TimePlanningChannels.channelid] = data.channelID
|
||||
} get TimePlanningChannels.id
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* lilJudd
|
||||
* Copyright (C) 2023 moonleay
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.moonleay.lilJudd.data.repository
|
||||
|
||||
import net.moonleay.lilJudd.data.entry.TimePlanningMessagesData
|
||||
import net.moonleay.lilJudd.data.tables.TimePlanningMessages
|
||||
import org.jetbrains.exposed.sql.and
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.select
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
object TimePlanningMessagesRepository {
|
||||
|
||||
fun write(data: TimePlanningMessagesData): Int =
|
||||
transaction {
|
||||
TimePlanningMessages.insert {
|
||||
it[serverid] = data.serverID
|
||||
it[channelid] = data.channelID
|
||||
it[weekstamp] = data.weekstamp
|
||||
it[messageids] = data.messageIDs
|
||||
} get TimePlanningMessages.id
|
||||
}
|
||||
|
||||
fun getWeek(stamp: Long): List<TimePlanningMessagesData> {
|
||||
val dataList = mutableListOf<TimePlanningMessagesData>()
|
||||
transaction {
|
||||
for (pnr in TimePlanningMessages.select {
|
||||
TimePlanningMessages.weekstamp eq (stamp)
|
||||
}) {
|
||||
dataList.add(
|
||||
TimePlanningMessagesData(
|
||||
pnr[TimePlanningMessages.id],
|
||||
pnr[TimePlanningMessages.serverid],
|
||||
pnr[TimePlanningMessages.channelid],
|
||||
pnr[TimePlanningMessages.weekstamp],
|
||||
pnr[TimePlanningMessages.messageids]
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
return dataList
|
||||
}
|
||||
|
||||
fun getWeekInChannel(stamp: Long, channelID: Long): TimePlanningMessagesData? =
|
||||
transaction {
|
||||
TimePlanningMessages.select {
|
||||
TimePlanningMessages.weekstamp eq (stamp) and (TimePlanningMessages.channelid eq channelID)
|
||||
}.firstOrNull()?.let {
|
||||
TimePlanningMessagesData(
|
||||
it[TimePlanningMessages.id],
|
||||
it[TimePlanningMessages.serverid],
|
||||
it[TimePlanningMessages.channelid],
|
||||
it[TimePlanningMessages.weekstamp],
|
||||
it[TimePlanningMessages.messageids]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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<Int>()
|
||||
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}",
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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}")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Snowflake, TimePlanningMessagesData>()
|
||||
// ChannelID, Data
|
||||
val roleMap = mutableMapOf<Long, PlanningNotifierRolesData>()
|
||||
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]
|
||||
val messageData = TimePlanningMessagesRepository.getWeekInChannel(
|
||||
TimeUtil.getWeekStamp().toEpochSecond() * 1000,
|
||||
snf.value.toLong()
|
||||
)
|
||||
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 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<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 c = Bot.bot.kordRef.getChannelOf<MessageChannel>(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<ResultRow> = 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()
|
||||
.getRoleOrNull(Snowflake(entry.hasTimeRoleID))?.delete()
|
||||
Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID))
|
||||
.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.wantstobenotifiedid]))
|
||||
?.delete()
|
||||
}
|
||||
.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",
|
||||
|
|
|
@ -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<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]
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
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)
|
||||
|
|
|
@ -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<Snowflake, Snowflake>()
|
||||
// ChannelID, Data
|
||||
val roleMap = mutableMapOf<Snowflake, PlanningNotifierRolesData>()
|
||||
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<MessageChannel>(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",
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue