feat: moved update logic into "updateInChannel" function, updated the function in Timeplanner buttons

Signed-off-by: moonleay <contact@moonleay.net>
This commit is contained in:
moonleay 2023-09-14 07:44:43 +02:00
parent 5ce99c904e
commit e0e522f561
Signed by: moonleay
GPG key ID: 82667543CCD715FB
4 changed files with 114 additions and 60 deletions

View file

@ -63,6 +63,6 @@ class IsAvailableEditButton : IEditButton {
} }
} }
} }
AvailabilityManager.runThread() AvailabilityManager.updateInChannel(interaction.channelId)
} }
} }

View file

@ -63,6 +63,6 @@ class MaybeAvailableEditButton : IEditButton {
} }
} }
} }
AvailabilityManager.runThread() AvailabilityManager.updateInChannel(interaction.channelId)
} }
} }

View file

@ -63,6 +63,6 @@ class NotAvailableEditButton : IEditButton {
} }
} }
} }
AvailabilityManager.runThread() AvailabilityManager.updateInChannel(interaction.channelId)
} }
} }

View file

@ -81,15 +81,10 @@ object AvailabilityManager : IFeature {
} }
} }
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 for (snf in messageMap.keys) { // snf = Snowflake
val data = messageMap[snf]!! // this is the data of the table val data = messageMap[snf]!! // this is the data of the table
if (Bot.bot.kordRef.getChannel(Snowflake(data.channelid)) == null) if (Bot.bot.kordRef.getChannel(Snowflake(data.channelid)) == null)
continue // This channel does not exist anymore. continue // This channel does not exist anymore.
val c =
Bot.bot.kordRef.getChannelOf<MessageChannel>(Snowflake(data.channelid))!! // Get the channel as MessageChannel
if (roleMap.isEmpty()) { if (roleMap.isEmpty()) {
Logger.out("No saved roles. Canceling.") Logger.out("No saved roles. Canceling.")
return return
@ -99,7 +94,68 @@ object AvailabilityManager : IFeature {
Logger.out("Role for channel ${data.channelid} does not exist") Logger.out("Role for channel ${data.channelid} does not exist")
continue // this took way to long to find out that this was the issue continue // this took way to long to find out that this was the issue
} }
val g = Bot.bot.kordRef.getGuildOrThrow(Snowflake(data.serverid)) this.updateInChannel(snf, data, roleData)
}
Logger.out("Done! Until tomorrow! <3 ")
}
suspend fun updateInChannel(snf: Snowflake) {
lateinit var data: TimePlanningMessagesData
lateinit var roleData: PlanningNotifierRolesData
var found1 = false
var found2 = false
for (pnr in TimePlanningMessages.select {
TimePlanningMessages.weekstamp eq (TimeUtil.getWeekStamp()
.toEpochSecond() * 1000) and (TimePlanningMessages.channelid eq (snf.value.toLong()))
}) {
data =
TimePlanningMessagesData(
pnr[TimePlanningMessages.serverid],
pnr[TimePlanningMessages.channelid],
pnr[TimePlanningMessages.weekstamp],
pnr[TimePlanningMessages.messageids]
)
found1 = true
}
for (pnr in PlanningNotifierRoles.select {
PlanningNotifierRoles.channelid eq (snf.value.toLong())
}) {
roleData =
PlanningNotifierRolesData(
pnr[PlanningNotifierRoles.serverid],
pnr[PlanningNotifierRoles.channelid],
pnr[PlanningNotifierRoles.hastimeroleid],
pnr[PlanningNotifierRoles.wantstobenotifiedid]
)
found2 = true
}
if (!found1 || !found2) {
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")
return // this took way to long to find out that this was the issue
}
updateInChannel(snf, data, roleData)
}
@OptIn(PrivilegedIntent::class)
suspend fun updateInChannel(snf: Snowflake, tpmd: TimePlanningMessagesData, pnrd: PlanningNotifierRolesData) {
if (Bot.bot.kordRef.getChannel(Snowflake(tpmd.channelid)) == 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 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.")
val g = Bot.bot.kordRef.getGuildOrThrow(Snowflake(tpmd.serverid))
// Get all members with the role // Get all members with the role
val mce = g.requestMembers { val mce = g.requestMembers {
this.requestAllMembers() this.requestAllMembers()
@ -107,8 +163,8 @@ object AvailabilityManager : IFeature {
mce.collect { memberchunkevent -> mce.collect { memberchunkevent ->
memberchunkevent.members.forEach { memberchunkevent.members.forEach {
Logger.out("Checking member ${it.id.value} (${it.username})") Logger.out("Checking member ${it.id.value} (${it.username})")
if (it.roleIds.contains(Snowflake(roleData.hastimeroleid))) { if (it.roleIds.contains(Snowflake(pnrd.hastimeroleid))) {
it.removeRole(Snowflake(roleData.hastimeroleid)) it.removeRole(Snowflake(pnrd.hastimeroleid))
Logger.out("Removed role from ${it.username}") // Removed the role Logger.out("Removed role from ${it.username}") // Removed the role
} }
} }
@ -118,33 +174,33 @@ object AvailabilityManager : IFeature {
// This stores the ids of the messages. // This stores the ids of the messages.
// The format is weekdaNR:ID // The format is weekdaNR:ID
// The last entry (nr 8) is empty, so we can ignore it // The last entry (nr 8) is empty, so we can ignore it
val messageIdSplit = data.messageids.split(";").subList(0, 7) val messageIdSplit = tpmd.messageids.split(";").subList(0, 7)
for (mid in messageIdSplit) { for (mid in messageIdSplit) {
Logger.out("Checking id $mid") Logger.out("Checking id $mid")
if (!mid.startsWith((TimeUtil.getDayOfMonthInt(weekday) - 1).toString(), true)) if (!mid.startsWith((TimeUtil.getDayOfMonthInt(weekday) - 1).toString(), true))
continue // This is not the right message, check the next one continue// This is not the right message, check the next one
val idFiltered = mid.split(":")[1] // This is the target message id val idFiltered = mid.split(":")[1] // This is the target message id
val message = c.getMessageOrNull(Snowflake(idFiltered)) // Get the message from the channel val message = c.getMessageOrNull(Snowflake(idFiltered)) // Get the message from the channel
if (message == null) { if (message == null) {
Logger.out("Could not find message.") Logger.out("Could not find message.")
break // This message does not exist anymore. Nothing we can do about that. return // This message does not exist anymore. Nothing we can do about that.
} }
if (message.data.embeds.isEmpty()) { if (message.data.embeds.isEmpty()) {
Logger.out("There are no embeds.") Logger.out("There are no embeds.")
break // There are no embeds or there are not enough embeds return // There are no embeds or there are not enough embeds
} }
val targets = EmbedUtil.getAllUsersInTheFirstXTables(2, message.embeds[0]) val targets = EmbedUtil.getAllUsersInTheFirstXTables(2, message.embeds[0])
for (tid in targets) { for (tid in targets) {
Logger.out("Checking id $tid") Logger.out("Checking id $tid")
if (Bot.bot.kordRef.getGuildOrNull(Snowflake(data.serverid))!! if (Bot.bot.kordRef.getGuildOrNull(Snowflake(tpmd.serverid))!!
.getMemberOrNull(Snowflake(tid)) == null .getMemberOrNull(Snowflake(tid)) == null
) )
continue // This member does not exist anymore. continue// This member does not exist anymore.
val member = Bot.bot.kordRef.getGuildOrThrow(Snowflake(data.serverid)) val member = Bot.bot.kordRef.getGuildOrThrow(Snowflake(tpmd.serverid))
.getMember(Snowflake(tid)) // Get the member .getMember(Snowflake(tid)) // Get the member
if (member.roleIds.contains(Snowflake(roleData.hastimeroleid))) if (member.roleIds.contains(Snowflake(pnrd.hastimeroleid)))
continue // This member already has the role continue // This member already has the role
member.addRole(Snowflake(roleData.hastimeroleid)) // Add the role member.addRole(Snowflake(pnrd.hastimeroleid)) // Add the role
Logger.out("Added role to ${member.username}") Logger.out("Added role to ${member.username}")
} }
Logger.out("Done with message. Moving on...") Logger.out("Done with message. Moving on...")
@ -152,8 +208,6 @@ object AvailabilityManager : IFeature {
break break
} }
} }
Logger.out("Done! Until tomorrow! <3 ")
}
override val feat: FeatureEnum override val feat: FeatureEnum
get() = FeatureEnum.PLANNINGROLES get() = FeatureEnum.PLANNINGROLES