Skip to content

Commit 9ee092f

Browse files
RacciRacci
Racci
authored and
Racci
committed
style: formatting and readability
also hopefully fix an error
1 parent f771db9 commit 9ee092f

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

src/main/kotlin/dev/racci/elixir/extensions/StatChannels.kt

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,20 @@ import io.ktor.client.features.ServerResponseException
1313
import io.ktor.client.features.json.JsonFeature
1414
import io.ktor.client.request.get
1515
import java.util.*
16+
import kotlin.time.Duration.Companion.milliseconds
1617
import kotlin.time.Duration.Companion.seconds
17-
import kotlinx.coroutines.CoroutineScope
1818
import kotlinx.coroutines.DelicateCoroutinesApi
1919
import kotlinx.coroutines.GlobalScope
20-
import kotlinx.coroutines.async
21-
import kotlinx.coroutines.coroutineScope
2220
import kotlinx.coroutines.delay
2321
import kotlinx.coroutines.flow.count
2422
import kotlinx.coroutines.launch
23+
import kotlinx.coroutines.withTimeoutOrNull
2524
import kotlinx.serialization.Serializable
2625

2726
class StatChannels: Extension() {
2827

2928
override val name: String = "statchannels"
3029

31-
// TODO Fix this janky thing
3230
@OptIn(DelicateCoroutinesApi::class)
3331
override suspend fun setup() {
3432
GlobalScope.launch { checker() }
@@ -41,31 +39,29 @@ class StatChannels: Extension() {
4139
private suspend fun checker() {
4240
var members = 0
4341
var status = false
42+
val client = HttpClient {install(JsonFeature)}
43+
4444
while(loaded) {
45-
try {
46-
HttpClient {install(JsonFeature)}.use {client->
47-
val response: Query = client.get("https://mcapi.xdefcon.com/server/$STATUS_SERVER/status/json")
48-
val tStatus = response.online
49-
if(status != tStatus) {
50-
status = !status
51-
kord.unsafe.voiceChannel(GUILD_ID, STATUS_CHANNEL).edit {
52-
name = "Status: ${response.serverStatus.replaceFirstChar {
53-
if(it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
54-
}}"
55-
}
56-
}
45+
// Time it out in 1.5 seconds so we don't end up blocked
46+
val tStatus = withTimeoutOrNull<Query?>(1500.milliseconds) {
47+
try {
48+
return@withTimeoutOrNull client.get("https://mcapi.xdefcon.com/server/$STATUS_SERVER/status/json")
49+
} catch(ignored: ServerResponseException) {return@withTimeoutOrNull null}
50+
}
51+
// We only want to change the name if it needs changing
52+
if(tStatus != null && status != tStatus.online) {
53+
status = !status
54+
kord.unsafe.voiceChannel(GUILD_ID, STATUS_CHANNEL).edit {
55+
name = "Status: ${ tStatus.serverStatus.replaceFirstChar { if(it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } }"
5756
}
58-
} catch(ignored: ServerResponseException) {}
59-
val tmem = kord.getGuild(GUILD_ID)?.members?.count() ?: -1
60-
if(members != tmem
61-
|| kord.unsafe.guildChannel(GUILD_ID, MEMBER_COUNTER).asChannel().data.name.value?.matches(Regex("Members: [0-9]*")) != true
62-
) {
63-
members = tmem
57+
}
58+
val tMembers = kord.getGuild(GUILD_ID)?.members?.count() ?: -1
59+
if(members != tMembers) {
60+
members = tMembers
6461
kord.unsafe.voiceChannel(GUILD_ID, MEMBER_COUNTER).edit {
6562
name = "Members: $members"
6663
}
6764
}
68-
6965
delay(15.seconds)
7066
}
7167
return

0 commit comments

Comments
 (0)