Skip to content

Commit f771db9

Browse files
RacciRacci
Racci
authored and
Racci
committed
feat: Add Server status checker and member counter
1 parent 9bca553 commit f771db9

File tree

6 files changed

+40
-29
lines changed

6 files changed

+40
-29
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
### Why?
44
Lily bot while being a fantastically clean and easy to use bot has some sort comings on features that i am expanding upon for my own use.
55

6+
### Features
7+
* Member count channel
8+
* Channel to represent if a minecraft server is online
9+
610
## Credits
711
* [LilyBot](https://github.com/IrisShaders/LilyBot): The base of this bot was forked from lily.
812
* [Kord](https://github.com/kordlib/kord): The Kotlin API for Discord.

libs.versions.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ logback = "1.2.8"
88

99
kotlinx-serialization = "1.3.2"
1010
kaml = "0.39.0"
11+
ktor = "1.3.2"
1112

1213
[libraries]
1314
groovy = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" }
@@ -17,4 +18,5 @@ kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8" }
1718
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
1819
logging = { module = "io.github.microutils:kotlin-logging", version.ref = "logging" }
1920
kotlinx-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
20-
kaml = { module = "com.charleskorn.kaml:kaml", version.ref = "kaml" }
21+
kaml = { module = "com.charleskorn.kaml:kaml", version.ref = "kaml" }
22+
ktor-json = { module = "io.ktor:ktor-client-serialization", version.ref = "ktor"}

src/main/kotlin/dev/racci/elixir/ElixirBot.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ suspend fun main() {
4444
applicationCommands {
4545
defaultGuild(GUILD_ID)
4646
}
47-
intents {
48-
+Intent.GuildMembers
49-
+Intent.GuildMessageReactions
50-
}
5147
members {
5248
fill(GUILD_ID)
5349
}
50+
intents {
51+
+Intent.GuildMembers
52+
// +Intent.GuildMessageReactions
53+
}
5454

5555
chatCommands {
5656
// Enable chat command handling

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,51 @@ import dev.kord.core.behavior.channel.edit
77
import dev.racci.elixir.utils.GUILD_ID
88
import dev.racci.elixir.utils.MEMBER_COUNTER
99
import dev.racci.elixir.utils.STATUS_CHANNEL
10+
import dev.racci.elixir.utils.STATUS_SERVER
1011
import io.ktor.client.HttpClient
1112
import io.ktor.client.features.ServerResponseException
12-
import io.ktor.client.request.request
13-
import io.ktor.client.request.url
14-
import io.ktor.client.statement.HttpResponse
15-
import io.ktor.http.HttpMethod
13+
import io.ktor.client.features.json.JsonFeature
14+
import io.ktor.client.request.get
15+
import java.util.*
1616
import kotlin.time.Duration.Companion.seconds
17+
import kotlinx.coroutines.CoroutineScope
18+
import kotlinx.coroutines.DelicateCoroutinesApi
19+
import kotlinx.coroutines.GlobalScope
20+
import kotlinx.coroutines.async
21+
import kotlinx.coroutines.coroutineScope
1722
import kotlinx.coroutines.delay
1823
import kotlinx.coroutines.flow.count
24+
import kotlinx.coroutines.launch
25+
import kotlinx.serialization.Serializable
1926

2027
class StatChannels: Extension() {
2128

2229
override val name: String = "statchannels"
23-
private var loaded2: Boolean = false
24-
private var members: Int = 0
25-
private var status: Boolean = false
2630

31+
// TODO Fix this janky thing
32+
@OptIn(DelicateCoroutinesApi::class)
2733
override suspend fun setup() {
28-
loaded2 = true
29-
checker()
34+
GlobalScope.launch { checker() }
3035
}
3136

32-
override suspend fun unload() {
33-
loaded2 = false
34-
}
37+
@Serializable
38+
data class Query(val online: Boolean = false, val serverStatus: String = "Online")
3539

3640
@OptIn(KordUnsafe::class, KordExperimental::class)
3741
private suspend fun checker() {
38-
while(loaded2) {
42+
var members = 0
43+
var status = false
44+
while(loaded) {
3945
try {
40-
HttpClient().use {client->
41-
val response: HttpResponse = client.request {
42-
method = HttpMethod.Get
43-
url("https://eu.mc-api.net/v3/server/status-http/mc.racci.dev")
44-
}
45-
status = response.status.value == 200
46-
if(status != (response.status.value == 200)) {
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) {
4750
status = !status
4851
kord.unsafe.voiceChannel(GUILD_ID, STATUS_CHANNEL).edit {
49-
name = "Status: $status"
52+
name = "Status: ${response.serverStatus.replaceFirstChar {
53+
if(it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
54+
}}"
5055
}
5156
}
5257
}
@@ -65,5 +70,4 @@ class StatChannels: Extension() {
6570
}
6671
return
6772
}
68-
6973
}

src/main/kotlin/dev/racci/elixir/extensions/commands/util/Ping.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class Ping: Extension() {
2020
override val name = "ping"
2121

2222
override suspend fun setup() {
23-
// val actionLog = kord.getGuild(GUILD_ID)?.getChannel(MOD_ACTION_LOG) as GuildMessageChannelBehavior
23+
val actionLog = kord.getGuild(GUILD_ID)?.getChannel(MOD_ACTION_LOG) as GuildMessageChannelBehavior
2424

25-
// ResponseHelper.responseEmbedInChannel(actionLog, "Elixir is now online!", null, DISCORD_GREEN, null)
25+
ResponseHelper.responseEmbedInChannel(actionLog, "Elixir is now online!", null, DISCORD_GREEN, null)
2626

2727
publicSlashCommand {
2828
name = "ping"

src/main/kotlin/dev/racci/elixir/utils/_Constants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ val CONFIG_PATH = env("CONFIG_PATH")
2121
val SENTRY_DSN = envOrNull("SENTRY_DSN")
2222
val MEMBER_COUNTER = Snowflake(env("MEMBER_COUNTER"))
2323
val STATUS_CHANNEL = Snowflake(env("STATUS_CHANNEL"))
24+
val STATUS_SERVER = env("STATUS_SERVER")
2425
const val JDBC_URL = "jdbc:sqlite:database.db"

0 commit comments

Comments
 (0)