feat: added PlanningRoles feature to FeatureEnum, added AvailabilityManager with Cronjob
chore: moved FeatureEnum from .extensions.component to .features.component Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
parent
03fa8a51a7
commit
2e63bee9a6
2 changed files with 148 additions and 2 deletions
|
@ -0,0 +1,146 @@
|
|||
/*
|
||||
* 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.features
|
||||
|
||||
import dev.inmo.krontab.buildSchedule
|
||||
import dev.inmo.krontab.doInfinity
|
||||
import dev.kord.common.entity.Snowflake
|
||||
import dev.kord.core.behavior.requestMembers
|
||||
import dev.kord.core.entity.channel.MessageChannel
|
||||
import dev.kord.gateway.PrivilegedIntent
|
||||
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.util.EmbedUtil
|
||||
import net.moonleay.lilJudd.util.Logger
|
||||
import net.moonleay.lilJudd.util.TimeUtil
|
||||
import org.jetbrains.exposed.sql.select
|
||||
import org.jetbrains.exposed.sql.selectAll
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
object AvailabilityManager {
|
||||
|
||||
// This runs during the cronjob.
|
||||
@OptIn(PrivilegedIntent::class)
|
||||
suspend fun runThread() {
|
||||
// ChannelID, Data
|
||||
val messageMap = mutableMapOf<Snowflake, TimePlanningMessagesData>()
|
||||
// ChannelID, Data
|
||||
val roleMap = mutableMapOf<String, PlanningNotifierRolesData>()
|
||||
|
||||
transaction {
|
||||
for (pnr in TimePlanningMessages.select {
|
||||
TimePlanningMessages.weekstamp eq (TimeUtil.getWeekStamp().toEpochSecond() * 1000).toString()
|
||||
}) {
|
||||
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]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
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.")
|
||||
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.
|
||||
val c =
|
||||
Bot.bot.kordRef.getChannelOf<MessageChannel>(Snowflake(data.channelid))!! // Get the channel as MessageChannel
|
||||
val roleData = roleMap[data.channelid]!! // Get the role data
|
||||
val g = Bot.bot.kordRef.getGuildOrThrow(Snowflake(data.serverid))
|
||||
// Get all members with the role
|
||||
val mce = g.requestMembers {
|
||||
this.requestAllMembers()
|
||||
}
|
||||
mce.collect { it1 ->
|
||||
it1.members.forEach {
|
||||
Logger.out("Checking member ${it.id.value}")
|
||||
if (it.roleIds.contains(Snowflake(roleData.hastimeroleid)))
|
||||
it.removeRole(Snowflake(roleData.hastimeroleid))
|
||||
}
|
||||
}
|
||||
|
||||
Logger.out("Got through all members")
|
||||
// This stores the ids of the messages.
|
||||
// The format is weekdaNR:ID
|
||||
// The last entry (nr 8) is empty, so we can ignore it
|
||||
val messageIdSplit = data.messageids.split(";").subList(0, 7)
|
||||
for (mid in messageIdSplit) {
|
||||
Logger.out("Checking id $mid")
|
||||
if (!mid.startsWith((TimeUtil.getDayOfMonthInt(weekday) - 1).toString(), true))
|
||||
continue // This is not the right message, check the next one
|
||||
val idFiltered = mid.split(":")[1] // This is the target message id
|
||||
val message = c.getMessageOrNull(Snowflake(idFiltered)) // Get the message from the channel
|
||||
if (message == null) {
|
||||
Logger.out("Could not find message.")
|
||||
break // This message does not exist anymore. Nothing we can do about that.
|
||||
}
|
||||
if (message.data.embeds.isEmpty()) {
|
||||
Logger.out("There are no embeds.")
|
||||
break // There are no embeds or there are not enough embeds
|
||||
}
|
||||
val targets = EmbedUtil.getAllUsersInTheFirstXTables(2, message.embeds[0])
|
||||
for (tid in targets) {
|
||||
Logger.out("Checking id $tid")
|
||||
if (Bot.bot.kordRef.getGuildOrNull(Snowflake(data.serverid))!!
|
||||
.getMemberOrNull(Snowflake(tid)) == null
|
||||
)
|
||||
continue // This member does not exist anymore.
|
||||
val member = Bot.bot.kordRef.getGuildOrThrow(Snowflake(data.serverid))
|
||||
.getMember(Snowflake(tid)) // Get the member
|
||||
if (member.roleIds.contains(Snowflake(roleData.hastimeroleid)))
|
||||
continue // This member already has the role
|
||||
member.addRole(Snowflake(roleData.hastimeroleid)) // Add the role
|
||||
Logger.out("Added role to ${member.username}")
|
||||
}
|
||||
Logger.out("Done with message. Moving on...")
|
||||
// We found the right message. We don't need to check the others.
|
||||
break
|
||||
}
|
||||
}
|
||||
Logger.out("Done! Until tomorrow! <3 ")
|
||||
}
|
||||
|
||||
suspend fun registerThread() {
|
||||
Logger.out("Adding availability scheduler...")
|
||||
val scheduler = buildSchedule("0 0 5 * * * 0o *w") // 0 0 4 * * * 0o 1w // 0o is UTC
|
||||
scheduler.doInfinity {
|
||||
|
||||
this.runThread()
|
||||
}
|
||||
Logger.out("Starting to update roles...")
|
||||
}
|
||||
}
|
|
@ -16,11 +16,11 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.moonleay.lilJudd.extensions.component
|
||||
package net.moonleay.lilJudd.features.component
|
||||
|
||||
import com.kotlindiscord.kord.extensions.commands.application.slash.converters.ChoiceEnum
|
||||
|
||||
enum class FeatureEnum(override val readableName: String) : ChoiceEnum {
|
||||
TIMEPLANNINGFEATURE("Time Planning Feature"),
|
||||
//PLANNINGNOTIFIER("Planning Notifier")
|
||||
PLANNINGROLES("Planning Roles")
|
||||
}
|
Loading…
Reference in a new issue