@@ -13,22 +13,20 @@ import io.ktor.client.features.ServerResponseException
13
13
import io.ktor.client.features.json.JsonFeature
14
14
import io.ktor.client.request.get
15
15
import java.util.*
16
+ import kotlin.time.Duration.Companion.milliseconds
16
17
import kotlin.time.Duration.Companion.seconds
17
- import kotlinx.coroutines.CoroutineScope
18
18
import kotlinx.coroutines.DelicateCoroutinesApi
19
19
import kotlinx.coroutines.GlobalScope
20
- import kotlinx.coroutines.async
21
- import kotlinx.coroutines.coroutineScope
22
20
import kotlinx.coroutines.delay
23
21
import kotlinx.coroutines.flow.count
24
22
import kotlinx.coroutines.launch
23
+ import kotlinx.coroutines.withTimeoutOrNull
25
24
import kotlinx.serialization.Serializable
26
25
27
26
class StatChannels : Extension () {
28
27
29
28
override val name: String = " statchannels"
30
29
31
- // TODO Fix this janky thing
32
30
@OptIn(DelicateCoroutinesApi ::class )
33
31
override suspend fun setup () {
34
32
GlobalScope .launch { checker() }
@@ -41,31 +39,29 @@ class StatChannels: Extension() {
41
39
private suspend fun checker () {
42
40
var members = 0
43
41
var status = false
42
+ val client = HttpClient {install(JsonFeature )}
43
+
44
44
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() } } "
57
56
}
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
64
61
kord.unsafe.voiceChannel(GUILD_ID , MEMBER_COUNTER ).edit {
65
62
name = " Members: $members "
66
63
}
67
64
}
68
-
69
65
delay(15 .seconds)
70
66
}
71
67
return
0 commit comments