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 de1d8cd3a6
commit 7e0c8e5a9f
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
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
if (roleMap.isEmpty()) {
Logger.out("No saved roles. Canceling.")
return
@ -99,62 +94,121 @@ object AvailabilityManager : IFeature {
Logger.out("Role for channel ${data.channelid} does not exist")
continue // this took way to long to find out that this was the issue
}
val g = Bot.bot.kordRef.getGuildOrThrow(Snowflake(data.serverid))
// Get all members with the role
val mce = g.requestMembers {
this.requestAllMembers()
}
mce.collect { memberchunkevent ->
memberchunkevent.members.forEach {
Logger.out("Checking member ${it.id.value} (${it.username})")
if (it.roleIds.contains(Snowflake(roleData.hastimeroleid))) {
it.removeRole(Snowflake(roleData.hastimeroleid))
Logger.out("Removed role from ${it.username}") // Removed the role
}
}
}
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
}
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
val mce = g.requestMembers {
this.requestAllMembers()
}
mce.collect { memberchunkevent ->
memberchunkevent.members.forEach {
Logger.out("Checking member ${it.id.value} (${it.username})")
if (it.roleIds.contains(Snowflake(pnrd.hastimeroleid))) {
it.removeRole(Snowflake(pnrd.hastimeroleid))
Logger.out("Removed role from ${it.username}") // Removed the role
}
}
}
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 = tpmd.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.")
return // This message does not exist anymore. Nothing we can do about that.
}
if (message.data.embeds.isEmpty()) {
Logger.out("There are no embeds.")
return // 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(tpmd.serverid))!!
.getMemberOrNull(Snowflake(tid)) == null
)
continue// This member does not exist anymore.
val member = Bot.bot.kordRef.getGuildOrThrow(Snowflake(tpmd.serverid))
.getMember(Snowflake(tid)) // Get the member
if (member.roleIds.contains(Snowflake(pnrd.hastimeroleid)))
continue // This member already has the role
member.addRole(Snowflake(pnrd.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
}
}
override val feat: FeatureEnum
get() = FeatureEnum.PLANNINGROLES