41 lines
1001 B
Kotlin
41 lines
1001 B
Kotlin
package nl.voidcorp.dbot
|
|
|
|
import com.google.gson.Gson
|
|
import com.google.gson.GsonBuilder
|
|
import nl.voidcorp.dbot.commands.UnityCommand
|
|
import javax.script.ScriptEngineManager
|
|
import kotlin.random.Random
|
|
|
|
|
|
val gson: Gson = GsonBuilder().setPrettyPrinting().create()
|
|
|
|
|
|
fun <E> MutableList<E>.addAll(vararg e: E) = this.addAll(listOf(*e))
|
|
|
|
fun <E> List<E>.random(): E = this[Random.nextInt(this.size)]
|
|
|
|
fun List<UnityCommand>.catMap(): Map<String, List<UnityCommand>> {
|
|
val m = mutableMapOf<String, MutableList<UnityCommand>>()
|
|
for (c in this) {
|
|
val cname = c.category.name
|
|
if (!m.containsKey(cname)) {
|
|
m[cname] = mutableListOf()
|
|
}
|
|
|
|
m[cname]!! += c
|
|
}
|
|
return m
|
|
}
|
|
|
|
fun String.emptyOr(other: String): String = if (this.isEmpty()) other else this
|
|
|
|
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)
|
|
}
|
|
|
|
|
|
|