OttoBot/src/main/kotlin/nl/voidcorp/dbot/Util.kt

41 lines
1001 B
Kotlin
Raw Normal View History

2018-10-10 23:54:01 +02:00
package nl.voidcorp.dbot
2018-10-15 22:25:51 +02:00
import com.google.gson.Gson
2018-10-10 23:54:01 +02:00
import com.google.gson.GsonBuilder
import nl.voidcorp.dbot.commands.UnityCommand
2018-11-05 17:41:18 +01:00
import javax.script.ScriptEngineManager
import kotlin.random.Random
2018-10-10 23:54:01 +02:00
2018-10-15 22:25:51 +02:00
val gson: Gson = GsonBuilder().setPrettyPrinting().create()
2018-10-10 23:54:01 +02:00
2018-10-15 22:25:51 +02:00
fun <E> MutableList<E>.addAll(vararg e: E) = this.addAll(listOf(*e))
fun <E> List<E>.random(): E = this[Random.nextInt(this.size)]
2018-10-16 13:33:15 +02:00
fun List<UnityCommand>.catMap(): Map<String, List<UnityCommand>> {
val m = mutableMapOf<String, MutableList<UnityCommand>>()
2018-10-16 13:33:15 +02:00
for (c in this) {
val cname = c.category.name
2018-10-16 13:33:15 +02:00
if (!m.containsKey(cname)) {
m[cname] = mutableListOf()
}
2018-10-25 13:47:54 +02:00
m[cname]!! += c
2018-10-16 13:33:15 +02:00
}
return m
2018-10-25 13:47:54 +02:00
}
fun String.emptyOr(other: String): String = if (this.isEmpty()) other else this
2018-11-05 17:41:18 +01:00
val sem = ScriptEngineManager()
fun run(st: String, ret: String = "x", scriptEngine: String = "luaj"): Any {
val se = sem.getEngineByName(scriptEngine)
se.eval(st)
return se.get(ret)
}