Added Discord settings.
This commit is contained in:
parent
5a53fd5f68
commit
be03f5fb96
3
.gitignore
vendored
3
.gitignore
vendored
@ -182,4 +182,5 @@ gradle-app.setting
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/intellij,java,gradle,kotlin
|
||||
|
||||
data.json
|
||||
data.json
|
||||
token
|
||||
22
src/main/java/Discord.kt
Normal file
22
src/main/java/Discord.kt
Normal file
@ -0,0 +1,22 @@
|
||||
import dev.kord.core.Kord
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
object Discord {
|
||||
|
||||
private lateinit var kord: Kord
|
||||
suspend fun init(token: String) {
|
||||
|
||||
kord = Kord(token)
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
kord.login()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun sendMessage(message: String) {
|
||||
kord.rest.channel.createMessage(Settings.instance.discordChannelId) {
|
||||
this.content = "<@&${Settings.instance.discordGroupId}>$message"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,15 +1,16 @@
|
||||
import dev.kord.common.entity.Snowflake
|
||||
import dev.kord.core.Kord
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.cio.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.nio.file.Files
|
||||
import kotlin.io.path.Path
|
||||
|
||||
|
||||
suspend fun main() {
|
||||
Settings.load()
|
||||
val path = Path("data.json")
|
||||
|
||||
if (Files.isRegularFile(path)) {
|
||||
@ -19,25 +20,24 @@ suspend fun main() {
|
||||
RaceHolder.save(path)
|
||||
})
|
||||
|
||||
val kord = Kord("MTE3MTIwODc1MDE5MTg5MDQ4Mw.GOUedL.i3zD6IG5B6fFRvaSOotWwJ5KBRK2whC9xr0vL8")
|
||||
Discord.init(withContext(Dispatchers.IO) {
|
||||
Files.readString(Path("token"))
|
||||
})
|
||||
|
||||
val server = embeddedServer(CIO, port = 8080){
|
||||
routing {
|
||||
get("/"){
|
||||
call.respondRedirect("/search")
|
||||
}
|
||||
settingsPage()
|
||||
searchPage()
|
||||
trackPage()
|
||||
racePage()
|
||||
get("/hi") {
|
||||
kord.rest.channel.createMessage(Snowflake("1040400994355392522")) {
|
||||
this.content = "<@&978289601506586624> Herro!"
|
||||
}
|
||||
Discord.sendMessage("Herro!")
|
||||
}
|
||||
}
|
||||
}
|
||||
println("connection to localhost:8080 now possible.")
|
||||
server.start(false)
|
||||
kord.login()
|
||||
|
||||
server.start(true)
|
||||
}
|
||||
@ -46,8 +46,8 @@ data class RaceData(var name: String, var description: String) {
|
||||
fun calculateTotalScores() {
|
||||
val scoreMap = hashMapOf<String, Long>()
|
||||
leaderboardMap.forEach { leaderboardEntry ->
|
||||
leaderboardEntry.value.tracktimes.sortedBy { it.lap_time }.forEach {
|
||||
scoreMap[it.playername] = scoreMap.getOrDefault(it.playername, 0L) + 1
|
||||
leaderboardEntry.value.tracktimes.sortedBy { it.lap_time }.forEachIndexed { index, it ->
|
||||
scoreMap[it.playername] = scoreMap.getOrDefault(it.playername, 0L) + 1 + index
|
||||
}
|
||||
}
|
||||
synchronized(totalScores) {
|
||||
|
||||
32
src/main/java/Settings.kt
Normal file
32
src/main/java/Settings.kt
Normal file
@ -0,0 +1,32 @@
|
||||
import dev.kord.common.entity.Snowflake
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.nio.file.Files
|
||||
import kotlin.io.path.Path
|
||||
|
||||
const val SETTINGS = "settings.json"
|
||||
|
||||
@Serializable
|
||||
class Settings {
|
||||
companion object {
|
||||
var instance = Settings()
|
||||
|
||||
fun load() {
|
||||
if (Files.isRegularFile(Path(SETTINGS))) {
|
||||
val reader = Files.newBufferedReader(Path(SETTINGS))
|
||||
instance = Json.decodeFromString<Settings>(reader.readText())
|
||||
reader.close()
|
||||
}
|
||||
}
|
||||
|
||||
fun save() {
|
||||
val writer = Files.newBufferedWriter(Path(SETTINGS))
|
||||
writer.write(Json.encodeToString(instance))
|
||||
writer.close()
|
||||
}
|
||||
}
|
||||
|
||||
var discordChannelId = Snowflake(0)
|
||||
var discordGroupId = Snowflake(0)
|
||||
}
|
||||
41
src/main/java/SettingsPage.kt
Normal file
41
src/main/java/SettingsPage.kt
Normal file
@ -0,0 +1,41 @@
|
||||
import dev.kord.common.entity.Snowflake
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.html.*
|
||||
|
||||
fun Routing.settingsPage() {
|
||||
get("/settings") {
|
||||
respondThemedHtml("Settings") {
|
||||
form("/settings", encType = FormEncType.applicationXWwwFormUrlEncoded, method = FormMethod.post) {
|
||||
p {
|
||||
+"GroupID:"
|
||||
textInput(name = "groupId") {
|
||||
value = Settings.instance.discordGroupId.toString()
|
||||
}
|
||||
}
|
||||
p {
|
||||
+"ChannelID:"
|
||||
textInput(name = "channelId") {
|
||||
value = Settings.instance.discordChannelId.toString()
|
||||
}
|
||||
}
|
||||
|
||||
submitInput { }
|
||||
}
|
||||
}
|
||||
}
|
||||
post("/settings") {
|
||||
val params = call.receiveParameters()
|
||||
params["groupId"]?.let { Settings.instance.discordGroupId = Snowflake(it) }
|
||||
params["channelId"]?.let { Settings.instance.discordChannelId = Snowflake(it) }
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
Settings.save()
|
||||
}
|
||||
call.respondRedirect("/settings")
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user