Skip to content

Commit

Permalink
Key data collection state by bot ID
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Aug 17, 2024
1 parent ba7e250 commit 469b40d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ public open class ExtensibleBot(
logger.warn(e) { "Unable to add shutdown hook." }
}

dataCollector.start()

getKoin().get<Kord>().login {
this.presence(settings.presenceBuilder)
this.intents = Intents(settings.intentsBuilder!!)
Expand Down Expand Up @@ -196,7 +194,7 @@ public open class ExtensibleBot(
@Suppress("TooGenericExceptionCaught")
try {
Runtime.getRuntime().removeShutdownHook(shutdownHook)
} catch (e: IllegalStateException) {
} catch (_: IllegalStateException) {
logger.debug { "Shutdown in progress, unable to remove shutdown hook." }
} catch (e: Exception) {
logger.warn(e) { "Failed to remove shutdown hook." }
Expand Down Expand Up @@ -224,6 +222,10 @@ public open class ExtensibleBot(
ignoreUnknownKeys = true
}

on<ReadyEvent> {
dataCollector.start()
}

on<GuildCreateEvent> {
withLock { // If configured, this won't be concurrent, saving larger bots from spammy rate limits
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import dev.kordex.data.api.types.impl.MinimalDataEntity
import dev.kordex.data.api.types.impl.StandardDataEntity
import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.client.plugins.*
import io.ktor.utils.io.reader
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.launch
import org.koin.core.component.inject
Expand Down Expand Up @@ -69,8 +68,7 @@ public class DataCollector(public val level: DataCollection) : KordExKoinCompone
current.lastLevel = props.getProperty("lastLevel")?.let { DataCollection.fromDB(it) }
current.uuid = props.getProperty("uuid")?.let { UUID.fromString(it) }

storageUnit.save()

saveState()
deleteOldState()

logger.info { "Migration complete!" }
Expand Down Expand Up @@ -309,19 +307,25 @@ public class DataCollector(public val level: DataCollection) : KordExKoinCompone
}

private suspend fun getState(): State {
var current = storageUnit.get()
var current = storageUnit
.withUser(bot.kordRef.selfId)
.get()

if (current == null) {
current = State()

storageUnit.save(current)
storageUnit
.withUser(bot.kordRef.selfId)
.save(current)
}

return current
}

private suspend fun saveState() {
storageUnit.save()
storageUnit
.withUser(bot.kordRef.selfId)
.save()
}

/** Get the stored "last" data collection level. **/
Expand Down

0 comments on commit 469b40d

Please sign in to comment.