Compare commits
No commits in common. "95c22c6bd0acf2c207ca0cb8f2a76bc685f97db9" and "b77271517d4aab3bdd3a4da5cba842c8a95632e3" have entirely different histories.
95c22c6bd0
...
b77271517d
17 changed files with 374 additions and 510 deletions
|
@ -103,7 +103,8 @@ object Bot {
|
|||
add(::FeatureManageExtension)
|
||||
add(::SendPlannerExtension)
|
||||
add(::MatchExtension)
|
||||
add(::UpdateRolesExtension)
|
||||
add(::UpdateRolesExtension) // This command is only for debugging purposes
|
||||
//add(::TestExtension) // See comment in TestExtension.kt
|
||||
}
|
||||
|
||||
this.presence {
|
||||
|
|
|
@ -28,10 +28,14 @@ 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.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.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"
|
||||
|
@ -45,20 +49,33 @@ class AcceptEditButton() : IEditButton {
|
|||
val m = interaction.message
|
||||
val eb = MessageUtil.getAClonedEmbed(m.embeds[0])
|
||||
var shouldEditButton = false
|
||||
val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer(
|
||||
m.id.value.toLong(),
|
||||
interaction.channelId.value.toLong(),
|
||||
guild.id.value.toLong()
|
||||
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]
|
||||
)
|
||||
if (mpdd == null) {
|
||||
Logger.out("mpdd is null")
|
||||
return
|
||||
}
|
||||
val role = guild.getRoleOrNull(Snowflake(mpdd.roleID))
|
||||
if (role == null) {
|
||||
Logger.out("role is null")
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if (!found || mpdd == 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,9 +28,12 @@ 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.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.Logger
|
||||
import org.jetbrains.exposed.sql.and
|
||||
import org.jetbrains.exposed.sql.select
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
class CancelEditButton : IEditButton {
|
||||
override val id: String = "public.edit.btn.matchmanagement.cancel"
|
||||
|
@ -43,20 +46,33 @@ class CancelEditButton : IEditButton {
|
|||
) {
|
||||
val m = interaction.message
|
||||
if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) {
|
||||
val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer(
|
||||
m.id.value.toLong(),
|
||||
interaction.channelId.value.toLong(),
|
||||
guild.id.value.toLong()
|
||||
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]
|
||||
)
|
||||
if (mpdd == null) {
|
||||
Logger.out("mpdd is null")
|
||||
return
|
||||
}
|
||||
val role = guild.getRoleOrNull(Snowflake(mpdd.roleID))
|
||||
if (role == null) {
|
||||
Logger.out("role is null")
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if (!found || mpdd == 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,10 +28,14 @@ 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.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.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"
|
||||
|
@ -45,20 +49,33 @@ class DeclineEditButton : IEditButton {
|
|||
val m = interaction.message
|
||||
val eb = MessageUtil.getAClonedEmbed(m.embeds[0])
|
||||
var shouldEditButton = false
|
||||
val mpdd = MatchPlanningDataRepository.getFromMessageInChannelInServer(
|
||||
m.id.value.toLong(),
|
||||
interaction.channelId.value.toLong(),
|
||||
guild.id.value.toLong()
|
||||
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]
|
||||
)
|
||||
if (mpdd == null) {
|
||||
Logger.out("mpdd is null")
|
||||
return
|
||||
}
|
||||
val role = guild.getRoleOrNull(Snowflake(mpdd.roleID))
|
||||
if (role == null) {
|
||||
Logger.out("role is null")
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if (!found || mpdd == 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,7 +19,6 @@
|
|||
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,7 +19,6 @@
|
|||
package net.moonleay.lilJudd.data.entry
|
||||
|
||||
data class TimePlanningChannelsData(
|
||||
val id: Int,
|
||||
val serverID: Long,
|
||||
val channelID: Long,
|
||||
)
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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 }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -28,8 +28,7 @@ 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.entry.MatchPlanningDataData
|
||||
import net.moonleay.lilJudd.data.repository.MatchPlanningDataRepository
|
||||
import net.moonleay.lilJudd.data.tables.MatchPlanningData
|
||||
import net.moonleay.lilJudd.extensions.component.MatchTypes
|
||||
import net.moonleay.lilJudd.jobs.MatchJob
|
||||
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.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() {
|
||||
|
||||
|
@ -98,28 +100,28 @@ class MatchExtension : Extension() {
|
|||
}
|
||||
return@action
|
||||
}
|
||||
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) {
|
||||
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) {
|
||||
return@action // Not saved to db
|
||||
}
|
||||
JobManager.addJob(
|
||||
MatchJob(
|
||||
jobString,
|
||||
tID,
|
||||
"${args.matchType.readableName}_Vs_${opponent}_[${tID}]-${gID}_${cID}",
|
||||
tableID,
|
||||
"${args.matchType.readableName}_Vs_${opponent}_[${tableID}]-${gID}_${cID}",
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
|
@ -26,9 +26,10 @@ 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.entry.TimePlanningMessagesData
|
||||
import net.moonleay.lilJudd.data.repository.TimePlanningMessagesRepository
|
||||
import net.moonleay.lilJudd.data.tables.TimePlanningMessages
|
||||
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
|
||||
|
||||
|
@ -100,15 +101,14 @@ class SendPlannerExtension : Extension() {
|
|||
}
|
||||
|
||||
// Save the message ids
|
||||
TimePlanningMessagesRepository.write(
|
||||
TimePlanningMessagesData(
|
||||
-1,
|
||||
c.data.guildId.value?.value!!.toLong(),
|
||||
c.id.value.toLong(),
|
||||
(TimeUtil.getWeekStamp().toEpochSecond() * 1000),
|
||||
msgStr
|
||||
)
|
||||
)
|
||||
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
|
||||
}
|
||||
Logger.out("Finished with ${c.data.guildId.value}")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,35 +32,61 @@ 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.repository.PlanningNotifierRolesRepository
|
||||
import net.moonleay.lilJudd.data.repository.TimePlanningMessagesRepository
|
||||
import net.moonleay.lilJudd.data.tables.PlanningNotifierRoles
|
||||
import net.moonleay.lilJudd.data.tables.TimePlanningMessages
|
||||
import net.moonleay.lilJudd.extensions.FeatureManageExtension
|
||||
import net.moonleay.lilJudd.features.component.FeatureEnum
|
||||
import net.moonleay.lilJudd.features.component.IFeature
|
||||
import net.moonleay.lilJudd.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 messages = TimePlanningMessagesRepository.getWeek(TimeUtil.getWeekStamp().toEpochSecond() * 1000)
|
||||
.associateBy { it.channelID }
|
||||
val targetedRoles = PlanningNotifierRolesRepository.getAll().associateBy { it.channelID }
|
||||
val messageMap = mutableMapOf<Snowflake, TimePlanningMessagesData>()
|
||||
// ChannelID, Data
|
||||
val roleMap = mutableMapOf<Long, PlanningNotifierRolesData>()
|
||||
|
||||
for (id in messages.keys) {
|
||||
val snf = Snowflake(id) // snf = Snowflake
|
||||
val data = messages[id]!! // this is the data of the table
|
||||
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
|
||||
if (Bot.bot.kordRef.getChannel(Snowflake(data.channelID)) == null)
|
||||
continue // This channel does not exist anymore.
|
||||
if (targetedRoles.isEmpty()) {
|
||||
if (roleMap.isEmpty()) {
|
||||
Logger.out("No saved roles. Canceling.")
|
||||
return
|
||||
}
|
||||
val roleData = targetedRoles[data.channelID] // Get the role data
|
||||
val roleData = roleMap[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
|
||||
|
@ -71,27 +97,58 @@ object AvailabilityManager : IFeature {
|
|||
}
|
||||
|
||||
suspend fun updateInChannel(snf: Snowflake) {
|
||||
val messageData = TimePlanningMessagesRepository.getWeekInChannel(
|
||||
TimeUtil.getWeekStamp().toEpochSecond() * 1000,
|
||||
snf.value.toLong()
|
||||
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 roleData = PlanningNotifierRolesRepository.getForChannel(snf.value.toLong())
|
||||
if (messageData == null) {
|
||||
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) {
|
||||
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 ${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
|
||||
}
|
||||
updateInChannel(snf, messageData, roleData)
|
||||
updateInChannel(snf, data, roleData)
|
||||
}
|
||||
|
||||
@OptIn(PrivilegedIntent::class)
|
||||
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.
|
||||
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 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.")
|
||||
|
@ -171,8 +228,13 @@ object AvailabilityManager : IFeature {
|
|||
ch: Channel,
|
||||
args: FeatureManageExtension.FeatureManagerArgs
|
||||
): EmbedBuilder {
|
||||
var alreadyExists = PlanningNotifierRolesRepository.existsInChannel(cID)
|
||||
var alreadyExists = false
|
||||
// Check if the channel and guild already exist in the db
|
||||
transaction {
|
||||
alreadyExists = PlanningNotifierRoles.select {
|
||||
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
|
||||
}.count() > 0
|
||||
}
|
||||
if (!alreadyExists) {
|
||||
// Create the roles in Discord
|
||||
val hasTimeRole = Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID)).createRole {
|
||||
|
@ -188,15 +250,14 @@ object AvailabilityManager : IFeature {
|
|||
val wnr = wantsNotifsRole.id.value.toLong()
|
||||
|
||||
// Save the role ids to db
|
||||
PlanningNotifierRolesRepository.write(
|
||||
PlanningNotifierRolesData(
|
||||
id = -1,
|
||||
serverID = gID,
|
||||
channelID = cID,
|
||||
hasTimeRoleID = htr,
|
||||
wantsToBeNotifiedID = wnr
|
||||
)
|
||||
)
|
||||
transaction {
|
||||
PlanningNotifierRoles.insert {
|
||||
it[PlanningNotifierRoles.serverid] = gID
|
||||
it[PlanningNotifierRoles.channelid] = cID
|
||||
it[PlanningNotifierRoles.hastimeroleid] = htr
|
||||
it[PlanningNotifierRoles.wantstobenotifiedid] = wnr
|
||||
} get PlanningNotifierRoles.id
|
||||
}
|
||||
|
||||
return MessageUtil.getEmbed(
|
||||
EmbedColor.SUCCESS,
|
||||
|
@ -223,16 +284,35 @@ object AvailabilityManager : IFeature {
|
|||
args: FeatureManageExtension.FeatureManagerArgs
|
||||
): EmbedBuilder {
|
||||
// Check if entry exists in db
|
||||
if (PlanningNotifierRolesRepository.existsInChannelFromSever(cID, gID)) {
|
||||
val entry = PlanningNotifierRolesRepository.getForChannelInServer(cID, gID)!!
|
||||
var alreadyExists = false
|
||||
transaction {
|
||||
alreadyExists = PlanningNotifierRoles.select {
|
||||
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
|
||||
}.count() > 0
|
||||
}
|
||||
if (alreadyExists) {
|
||||
var matchingEntries: List<ResultRow> = mutableListOf()
|
||||
transaction {
|
||||
matchingEntries = PlanningNotifierRoles.select {
|
||||
(PlanningNotifierRoles.serverid eq gID) and
|
||||
(PlanningNotifierRoles.channelid eq cID)
|
||||
}.toList()
|
||||
}
|
||||
// delete all entries for this guild and channel combo
|
||||
for (e in matchingEntries) {
|
||||
Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID))
|
||||
.getRoleOrNull(Snowflake(entry.hasTimeRoleID))?.delete()
|
||||
.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.hastimeroleid]))?.delete()
|
||||
Bot.bot.kordRef.getGuildOrThrow(Snowflake(gID))
|
||||
.getRoleOrNull(Snowflake(entry.wantsToBeNotifiedID))?.delete()
|
||||
.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.wantstobenotifiedid]))
|
||||
?.delete()
|
||||
}
|
||||
|
||||
// delete all found entries
|
||||
PlanningNotifierRolesRepository.delete(entry.id)
|
||||
transaction {
|
||||
matchingEntries.forEach { entry ->
|
||||
PlanningNotifierRoles.deleteWhere { id eq entry[id] }
|
||||
}
|
||||
}
|
||||
return MessageUtil.getEmbed(
|
||||
EmbedColor.SUCCESS,
|
||||
"200: Success",
|
||||
|
|
|
@ -21,22 +21,46 @@ 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.repository.MatchPlanningDataRepository
|
||||
import net.moonleay.lilJudd.data.tables.MatchPlanningData
|
||||
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 = 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) {
|
||||
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...")
|
||||
this.removeRoleFromGuild(data.serverID, data.roleID)
|
||||
MatchPlanningDataRepository.delete(data.id)
|
||||
transaction {
|
||||
MatchPlanningData.deleteWhere { MatchPlanningData.messageid eq data.messageID }
|
||||
}
|
||||
continue
|
||||
}
|
||||
this.registerJob(data)
|
||||
|
|
|
@ -29,18 +29,17 @@ 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.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.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.extensions.FeatureManageExtension
|
||||
import net.moonleay.lilJudd.features.component.FeatureEnum
|
||||
import net.moonleay.lilJudd.features.component.IFeature
|
||||
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 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.ZoneId
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
|
@ -61,23 +60,41 @@ 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 }
|
||||
|
||||
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!")
|
||||
for (ch2 in targetedChannels.keys) {
|
||||
val ch = Snowflake(ch2)
|
||||
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) {
|
||||
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 (targetedRoles != null && targetedRoles.keys.contains(ch2) && targetedRoles[ch2] != null) {
|
||||
if (roleMap != null && roleMap.keys.contains(ch) && roleMap[ch] != null) {
|
||||
c.createMessage {
|
||||
this.content =
|
||||
"The weekly planning starts now <@&${Snowflake(targetedRoles[ch2]?.wantsToBeNotifiedID!!)}>"
|
||||
"The weekly planning starts now <@&${Snowflake(roleMap[ch]?.wantsToBeNotifiedID!!)}>"
|
||||
this.embeds.add(
|
||||
MessageUtil.getEmbed(
|
||||
EmbedColor.INFO,
|
||||
|
@ -128,15 +145,14 @@ object TimeManager : IFeature {
|
|||
}
|
||||
|
||||
// Save the message ids
|
||||
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
|
||||
)
|
||||
)
|
||||
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
|
||||
}
|
||||
Logger.out("Finished with ${c.data.guildId.value}")
|
||||
}
|
||||
Logger.out("Done! Until next Monday! <3 ")
|
||||
|
@ -149,8 +165,20 @@ object TimeManager : IFeature {
|
|||
ch: Channel,
|
||||
args: FeatureManageExtension.FeatureManagerArgs
|
||||
): EmbedBuilder {
|
||||
if (!TimePlanningChannelsRepository.exists(cID, gID)) {
|
||||
TimePlanningChannelsRepository.write(TimePlanningChannelsData(id = -1, serverID = gID, channelID = cID))
|
||||
var alreadyExists = false
|
||||
transaction {
|
||||
alreadyExists = TimePlanningChannels.select {
|
||||
(TimePlanningChannels.serverid eq gID) and
|
||||
(TimePlanningChannels.channelid eq cID)
|
||||
}.count() > 0
|
||||
}
|
||||
if (!alreadyExists) {
|
||||
transaction {
|
||||
TimePlanningChannels.insert {
|
||||
it[TimePlanningChannels.serverid] = gID
|
||||
it[TimePlanningChannels.channelid] = cID
|
||||
} get TimePlanningChannels.id
|
||||
}
|
||||
return MessageUtil.getEmbed(
|
||||
EmbedColor.SUCCESS,
|
||||
"200: Success",
|
||||
|
@ -174,9 +202,25 @@ object TimeManager : IFeature {
|
|||
args: FeatureManageExtension.FeatureManagerArgs
|
||||
): EmbedBuilder {
|
||||
// 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
|
||||
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(
|
||||
EmbedColor.SUCCESS,
|
||||
"200: Success",
|
||||
|
|
|
@ -22,11 +22,16 @@ 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.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.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,
|
||||
|
@ -43,7 +48,25 @@ class MatchJob(
|
|||
* */
|
||||
override suspend fun jobFunction() {
|
||||
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))
|
||||
if (guild == null) {
|
||||
Logger.out("Guild not found.")
|
||||
|
@ -55,7 +78,11 @@ class MatchJob(
|
|||
return
|
||||
}
|
||||
r.delete()
|
||||
MatchPlanningDataRepository.delete(tableId)
|
||||
transaction {
|
||||
MatchPlanningData.deleteWhere {
|
||||
MatchPlanningData.id eq (tableId)
|
||||
}
|
||||
}
|
||||
Logger.out("MatchJob \"${this.jobName}\" finished.")
|
||||
Logger.out("Killing job \"${this.jobName}\"..")
|
||||
JobManager.killJob(this)
|
||||
|
|
Loading…
Reference in a new issue