OttoBot/src/main/kotlin/nl/voidcorp/dbot/commands/GSM.kt

48 lines
1.2 KiB
Kotlin

package nl.voidcorp.dbot.commands
import com.github.salomonbrys.kotson.fromJson
import net.dv8tion.jda.core.entities.Guild
import nl.voidcorp.dbot.gson
import nl.voidcorp.dbot.storage.GuildSettings
import java.io.File
import kotlin.concurrent.fixedRateTimer
object GSM {
private val f = File("settings.json")
private var lastHC = -1
private val gmap = mutableMapOf<Long, GuildSettings>()
fun getSettings(it: Guild): GuildSettings {
val res = gmap[it.idLong]
return if (res != null) {
res
} else {
val gm = GuildSettings()
gmap[it.idLong] = gm
gm
}
}
fun init() {
if (f.exists()) {
try {
val m = gson.fromJson<Map<Long, GuildSettings>>(f.readText())
gmap.putAll(m)
} catch (ex: IllegalStateException) {
}
}
fixedRateTimer("FileSaveThing", period = 1000 * 30, initialDelay = 1000 * 30) {
if (lastHC != gmap.hashCode()) {
lastHC = gmap.hashCode()
val br = f.bufferedWriter()
gson.toJson(gmap, br)
br.close()
}
}
}
}