From 196bb378b4dee133ecb551def795f92b398f99ef Mon Sep 17 00:00:00 2001 From: limited_dev Date: Thu, 29 Jun 2023 09:57:36 +0200 Subject: [PATCH] feat: make the bot update the roles on startup fix: fixed the bot throwing Errors when there is no role for a channel Signed-off-by: limited_dev --- src/main/kotlin/net/moonleay/lilJudd/Bot.kt | 6 ++++++ .../lilJudd/features/AvailabilityManager.kt | 14 +++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/net/moonleay/lilJudd/Bot.kt b/src/main/kotlin/net/moonleay/lilJudd/Bot.kt index b8c138e..c12a304 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/Bot.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/Bot.kt @@ -22,6 +22,7 @@ import com.kotlindiscord.kord.extensions.ExtensibleBot import dev.kord.common.Color import dev.kord.common.entity.PresenceStatus import dev.kord.core.behavior.interaction.response.respond +import dev.kord.core.event.gateway.ReadyEvent import dev.kord.core.event.interaction.ButtonInteractionCreateEvent import dev.kord.core.on import dev.kord.gateway.Intent @@ -151,6 +152,11 @@ object Bot { } } + // Update roles + bot.kordRef.on { + AvailabilityManager.runThread() + } + //Start the bot bot.start() diff --git a/src/main/kotlin/net/moonleay/lilJudd/features/AvailabilityManager.kt b/src/main/kotlin/net/moonleay/lilJudd/features/AvailabilityManager.kt index 7d10ef7..9dd66aa 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/features/AvailabilityManager.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/features/AvailabilityManager.kt @@ -88,14 +88,22 @@ object AvailabilityManager : IFeature { continue // This channel does not exist anymore. val c = Bot.bot.kordRef.getChannelOf(Snowflake(data.channelid))!! // Get the channel as MessageChannel - val roleData = roleMap[data.channelid]!! // Get the role data + if (roleMap.size < 1) { + Logger.out("No saved roles. Canceling.") + return + } + val roleData = roleMap[data.channelid] // Get the role data + if (roleData == null) { + Logger.out("Role for this channel does not exist") + return + } 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 { + mce.collect { memberchunkevent -> + memberchunkevent.members.forEach { Logger.out("Checking member ${it.id.value}") if (it.roleIds.contains(Snowflake(roleData.hastimeroleid))) it.removeRole(Snowflake(roleData.hastimeroleid))