fix: added try-catch to Splatoon3Api, fixed Statusupdater

Signed-off-by: moonleay <contact@moonleay.net>
This commit is contained in:
moonleay 2023-12-07 08:58:16 +01:00
parent 94149e4272
commit 984bcabd0c
Signed by: moonleay
GPG key ID: 82667543CCD715FB
4 changed files with 357 additions and 301 deletions

View file

@ -32,11 +32,17 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import net.moonleay.lilJudd.buttons.component.EditButtonManager
import net.moonleay.lilJudd.data.CredentialManager
import net.moonleay.lilJudd.data.api.Splatoon3ApiCache
import net.moonleay.lilJudd.data.api.type.ApiDataType
import net.moonleay.lilJudd.data.api.type.ApiRequestType
import net.moonleay.lilJudd.data.database.DB
import net.moonleay.lilJudd.extensions.*
import net.moonleay.lilJudd.features.AvailabilityManager
import net.moonleay.lilJudd.features.MatchManager
import net.moonleay.lilJudd.features.TimeManager
import net.moonleay.lilJudd.jobs.Splatoon3ApiScheduleUpdateScheduler
import net.moonleay.lilJudd.jobs.StatusUpdater
import net.moonleay.lilJudd.jobs.component.JobManager
import net.moonleay.lilJudd.util.EmbedColor
import net.moonleay.lilJudd.util.Logger
import net.moonleay.lilJudd.util.MessageUtil
@ -158,12 +164,12 @@ object Bot {
AvailabilityManager.runThread() // Update Availabilities
MatchManager.update() // Update Matches
// Make the bot update the status every 6 seconds
// JobManager.addJob(StatusUpdater)
JobManager.addJob(StatusUpdater)
}
// Update the Splatoon 3 api data and make sure it stays up-to-date
// Splatoon3ApiCache.updateData(ApiDataType.SCHEDULES, ApiRequestType.AUTOMATIC_CACHE_CREATION_AT_STARTUP)
// JobManager.addJob(Splatoon3ApiScheduleUpdateScheduler)
Splatoon3ApiCache.updateData(ApiDataType.SCHEDULES, ApiRequestType.AUTOMATIC_CACHE_CREATION_AT_STARTUP)
JobManager.addJob(Splatoon3ApiScheduleUpdateScheduler)
/*
Other caches will be added when implemented
its not used yet in order to reduce load on the api,

View file

@ -192,6 +192,7 @@ object Splatoon3ApiCache {
Logger.out("Error getting coop data: $apiResponse")
return
}
try {
val json = Json.parseToJsonElement(apiResponse)
val data = json.jsonObject["data"]!!.jsonObject["coopResult"]!!.jsonObject["monthlyGear"]!!.jsonObject
cachedCoopRewardsData = mutableListOf()
@ -203,6 +204,9 @@ object Splatoon3ApiCache {
)
)
Logger.out("Updated COOP data")
} catch (e: Exception) {
Logger.out("Error getting coop data: ${e.cause}")
}
}
private fun updateScheduleCache(uag: String) {
@ -214,6 +218,7 @@ object Splatoon3ApiCache {
val json = Json.decodeFromString(apiResponse) as JsonObject
val data = json["data"]!!.jsonObject
try {
val mapList = data["vsStages"]!!.jsonObject["nodes"]!!.jsonArray
cachedMapData = mutableMapOf()
mapList.forEach {
@ -227,7 +232,11 @@ object Splatoon3ApiCache {
)
}
Logger.out("Updated maplist data")
} catch (e: Exception) {
Logger.out("Error getting maplist data: ${e.cause}")
}
try {
val regularMatches = data["regularSchedules"]!!.jsonObject["nodes"]!!.jsonArray
cachedRegularModeData = mutableListOf()
regularMatches.forEach {
@ -247,7 +256,11 @@ object Splatoon3ApiCache {
)
}
Logger.out("Updated Regular match data")
} catch (e: Exception) {
Logger.out("Error getting regular match data: ${e.cause}")
}
try {
val compMatches = data["bankaraSchedules"]!!.jsonObject["nodes"]!!.jsonArray
cachedCompetitiveSeriesModeData = mutableListOf()
cachedCompetitiveOpenModeData = mutableListOf()
@ -288,6 +301,10 @@ object Splatoon3ApiCache {
}
Logger.out("Updated Competitive match data")
} catch (e: Exception) {
Logger.out("Error getting competitive match data: ${e.cause}")
}
try {
val xMatches = data["xSchedules"]!!.jsonObject["nodes"]!!.jsonArray
cachedXModeData = mutableListOf()
xMatches.forEach {
@ -307,7 +324,11 @@ object Splatoon3ApiCache {
)
}
Logger.out("Updated X match data")
} catch (e: Exception) {
Logger.out("Error getting X match data: ${e.cause}")
}
try {
val challengeData = data["eventSchedules"]!!.jsonObject["nodes"]!!.jsonArray
cachedChallengesData = mutableListOf()
challengeData.forEach {
@ -342,8 +363,13 @@ object Splatoon3ApiCache {
)
}
Logger.out("Updated Challenge data")
} catch (e: Exception) {
Logger.out("Error getting Challenge data: ${e.cause}")
}
val shiftData = data["coopGroupingSchedule"]!!.jsonObject["regularSchedules"]!!.jsonObject["nodes"]!!.jsonArray
try {
val shiftData =
data["coopGroupingSchedule"]!!.jsonObject["regularSchedules"]!!.jsonObject["nodes"]!!.jsonArray
cachedShiftData = mutableListOf()
shiftData.forEach {
val obj = it as JsonObject
@ -377,8 +403,14 @@ object Splatoon3ApiCache {
)
)
}
Logger.out("Updated shift data")
} catch (e: Exception) {
Logger.out("Error getting coopGrouping data: ${e.cause}")
}
val bigRunData = data["coopGroupingSchedule"]!!.jsonObject["bigRunSchedules"]!!.jsonObject["nodes"]!!.jsonArray
try {
val bigRunData =
data["coopGroupingSchedule"]!!.jsonObject["bigRunSchedules"]!!.jsonObject["nodes"]!!.jsonArray
cachedBigRunShiftData = mutableListOf()
bigRunData.forEach {
val obj = it as JsonObject
@ -413,6 +445,9 @@ object Splatoon3ApiCache {
)
}
Logger.out("Updated big run data")
} catch (e: Exception) {
Logger.out("Error getting big run data: ${e.cause}")
}
Logger.out("Updated all Schedules")
}
@ -423,8 +458,10 @@ object Splatoon3ApiCache {
Logger.out("Error getting splatfest data: $apiResponse")
return
}
try {
val json = Json.decodeFromString(apiResponse) as JsonObject
val festivals = json["US"]!!.jsonObject["data"]!!.jsonObject["festRecords"]!!.jsonObject["nodes"]!!.jsonArray
val festivals =
json["US"]!!.jsonObject["data"]!!.jsonObject["festRecords"]!!.jsonObject["nodes"]!!.jsonArray
cachedSplatfestData = mutableListOf()
festivals.forEach {
val fest = it as JsonObject
@ -531,5 +568,8 @@ object Splatoon3ApiCache {
)
}
Logger.out("Updated Splatfest data")
} catch (e: Exception) {
Logger.out("Error getting splatfest data: ${e.cause}")
}
}
}

View file

@ -30,7 +30,7 @@ object StatusUpdater : ICronjob {
override val jobName: String
get() = "StatusUpdater"
override val jobIncoming: String
get() = "/5 * * * * * 0o *" //Every 5 seconds
get() = "/10 * * * * * 0o *" //Every 5 seconds
override val jobType: CronjobType
get() = CronjobType.INFINITE
override val continueJob: Boolean
@ -45,7 +45,7 @@ object StatusUpdater : ICronjob {
refreshStatusList(System.currentTimeMillis())
Bot.bot.kordRef.editPresence {
this.status = PresenceStatus.DoNotDisturb
this.playing(statusList[index])
this.competing(statusList[index])
}
++index
if (index >= statusList.size) {

View file

@ -145,6 +145,16 @@ object TimeUtil {
.withMinute(0).withSecond(0)
}
fun validateDateString(input: String): Boolean {
val formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm")
return try {
LocalDateTime.parse(input, formatter)
true
} catch (e: Exception) {
false
}
}
fun getDateFromString(input: String): ZonedDateTime {
val formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm")
val localDateTime = LocalDateTime.parse(input, formatter)